Monday 2 September 2013

CPU Scheduling Programs combined in as a Project Code

//Cpu scheduling program in c++

//The program will help "HOW TO INTERACT Cpu scheduling program in c++"

//Code :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void SJFS_premptive();
void display(int ,int p[],float b[],float wt[],float tat[],float awt,float atat);
void disp(int,int p[],float B[],float Wt[],float at[],float tat[], float Awt,float avgtat);
void displayPri(int ,int p[],float Bt[],float wt[],float tat[],int Pri[],float Awt,float avgtat);
class cpu
{
int n,*process;
float Twt,Awt,*A,*Wt,w,*Bu,*tat,atat,avgtat;
public:
struct Process{
float burstTime;
float arr_time;
unsigned p_number;
float waitingTime;
float turnAroundTime;
unsigned priority;
int complete;
};
void priority();
void priority_premptive();
void Menu();
//Getting the No of processes & burst time
void Getdata();
//First come First served Algorithm
void Fcfs();
void simple();
void order();
//Shortest job First Algorithm
void Sjf();
//Shortest job First Algorithm with Preemption
  // void Sjfp();
//Shortest job First Algorithm with NonPreemption
  // void SjfP();
 // void SjfNp();
//Round Robin Algorithm
void RoundRobin();
//Priority Algorithm
void Priority();
//Display

};
// Implementation file for Cpu scheduling





//Main Menu
void cpu::Menu()
{
int ch,cho;
clrscr();
cout<<"\n\n\t\t\tWelcome To Cpu Scheduling"<<endl;

do
{
cout<<"\n\t\t\tMENU"<<endl;
cout<<"\t\t\t--------------------------------"<<endl;
// cout<<"\t\t\t1.Getting BurstTime"<<endl;
cout<<"\t\t\t1.FirstComeFirstServed"<<endl;
cout<<"\t\t\t2.ShortestJobFirst"<<endl;
cout<<"\t\t\t3.RoundRobin"<<endl;
cout<<"\t\t\t4.Priority"<<endl;
cout<<"\t\t\t5.EXIT"<<endl;
cout<<"\t\t\t--------------------------------"<<endl;
cout<<"Enter your choice=";
cin>>ch;


switch(ch)
{
//case 1:
// Getdata();
// break;
case 1:
cout<<endl<<"FIRST COME FIRST SERVED SCHEDULING"<<endl;
Fcfs();
break;

case 2:
cout<<"SHORTEST JOB FIRST SCHEDULING"<<endl;


Sjf();
break;
// case 2:
//c.SjfP();
// break;
// case 3:
// c.SjfNp();
// break;

case 3:
cout<<"ROUND ROBIN SCHEDULING"<<endl;
RoundRobin();
break;

case 4:

cout<<"PRIORITY SCHEDULING"<<endl;
priority();
break;

case 5:
exit(0);
break;

default:
cout<<"invalid option:"<<endl;
Menu();
break;
}


}while(ch<=5);



}

//Getting no of processes and Burst time
void cpu::Getdata()
{
int i,*proces;
float *bt;
clrscr();
cout<<"Enter the no of processes:";
cin>>n;

if (n>0)
{
process=new int[n];
proces=new int[n];
A=new float[n];
Wt=new float[n];
Bu=new float[n];
tat=new float[n];
bt=new float[n];


for(i=1;i<=n;i++)
{
cout<<"Enter process name:"<<"P";
cin>>proces[i];
if (proces[i]>0)
{
process[i]=proces[i];
}
else
{
cout<<":(process name should be more than 0"<<endl;
continue;
}
} //end of for loop


for (i=1;i<=n;i++)
{
cout<<"\nEnter The BurstTime for Process p"<<process[i]<<"= ";
cin>>bt[i];
if (bt[i]<0)
{cout<<":(burst tym should be positive"<<endl;
i--;
continue;
}
else
{
Bu[i]=bt[i];
}
 }
 }
else
{
cout<<"No of process should be more than 0"<<endl;
getch();
Getdata();
}

}
//First come First served Algorithm
void cpu::Fcfs()
{
int ch;
clrscr();
Getdata();
cout<<"\n1:Simple"<<endl;
cout<<"2:Order"<<endl;
cout<<"3:Exit"<<endl;
cout<<"Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
simple();
break;

case 2:
order();
break;
case 3:
exit(0);

default:
cout<<"Select valid option:";
break;
}

}
//  simple FsFc algo
void cpu::simple()
{
int i;
float *B;
clrscr();
B=new float[n];
Twt=0.0;
atat=0.0,avgtat=0.0;

for(i=1;i<=n;i++)
{
B[i]=Bu[i];
//cout<<"Burst time for process p"<<i<<"= ";
//cout<<B[i];
}
Wt[1]=0;
for(i=2;i<=n;i++)
{
Wt[i]=B[i-1]+Wt[i-1];
}
for (i=1;i<=n;i++)
{
tat[i]=Wt[i]+B[i];
}


//Calculating Average Weighting Time
for(i=1;i<=n;i++)
{
Twt=Twt+Wt[i];
atat+=tat[i];
 }
Awt=Twt/n;
avgtat=atat/n;


display(n,process,B,Wt,tat,Awt,avgtat);
}

//order FcFs algo
void cpu::order()
{
int i;
int *order,proces1,proces2,temp;
float *B,bt1,bt2,*bt;
clrscr();
B=new float[n];
  order=new int[n];
bt=new float[n];
Twt=0.0;
atat=0.0,avgtat;
for (i=1;i<=n;i++)
{
B[i]=Bu[i];
}

for (i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (process[j]>process[j+1])
{
proces1=process[j];
proces2=process[j+1];
process[j]=proces2;
process[j+1]=proces1;



//cout<<"process"<<process[j]<<endl;


bt1=B[j];
bt2=B[j+1];
B[j]=bt2;
B[j+1]=bt1;

//cout<<"Burst time for process p"<<i<<"= ";
//cout<<B[i]<<endl;
}
}
}


Wt[1]=0;
for (int x=2;x<=n;x++)
{
Wt[x]=B[x-1]+Wt[x-1];
}
for (i=1;i<=n;i++)
{
tat[i]=Wt[i]+B[i];
}


//Calculating Average Weighting Time
for(i=1;i<=n;i++)
{
order[i]=process[i];

Twt=Twt+Wt[i];
atat+=tat[i];
 }
Awt=Twt/n;
avgtat=atat/n;


display(n,order,B,Wt,tat,Awt,avgtat);
}


//Shortest job First Algorithm
void cpu::Sjf()
{
float temp,*B;
int i,j,pro,*ordr;
clrscr();
Getdata();
Twt=0.0;  atat=0.0;
B=new float[n];
ordr=new int[n];
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
// cout<<"Burst time for process p"<<i<<"= ";
 // cout<<B[i];
}
for(i=n;i>=1;i--)
{
for(j=2;j<=n;j++)
{
if(B[j-1]>B[j])
{
temp=B[j-1];
B[j-1]=B[j];
B[j]=temp;
pro=process[j-1];
process[j-1]=process[j];
process[j]=pro;
}
}
}

Wt[1]=0;
for(i=2;i<=n;i++)
{
Wt[i]=B[i-1]+Wt[i-1];
}
//calculating Average Weighting Time
for(i=1;i<=n;i++)
{   ordr[i]=process[i];
Twt=Twt+Wt[i];
}
Awt=Twt/n;
for (i=1;i<=n;i++)
{
tat[i]=Wt[i]+B[i];
}
for(i=1;i<=n;i++)
{
atat+=tat[i];
}
avgtat=atat/n;

display(n,ordr,B,Wt,tat,Awt,avgtat);

}

//Shortest job First Algorithm with Preemption

void SJFS_Premptive()
{


int num;
float *b,*tempBT,t=0.0,*at,*w,*tat;
clrscr();
cout<<endl<<"Enter No Of Processes : ";
cin>>num;
b=new float[num];
at=new float[num];
tempBT=new float[num];
w=new float[num];
tat=new float[num];


for(int i=1;i<=num;i++){
cout<<endl<<"Enter Brust Time For Process"<<i<<" : ";
cin>>b[i];
tempBT[i]=b[i];
t+=b[i];
}
for(i=1;i<=num;i++){
cout<<endl<<"Enter Arival Time For Process # "<<i<<" : ";
cin>>at[i];
w[i]=0;
}
i=0;
float j=1;
int iii=1;
i=1;
float minBT=b[1];
while(j<=t){

for(i=1;i<=num && at[i] != j;i++);
if(i<num){
while(1){//j<=totalBT+50){
XY:
int chck=0;
int chck1=0;
for(int f=1;f<=num;f++){
if(b[f] <= 0){
chck++;
}
}
if(chck >=num){
goto AB;
}
if(b[i]>0){
chck1++;
if(b[i] > 0.15)
b[i]-=0.1;
else
b[i] = 0.0;
j+=0.1;

}
for(int e=1;e<=num;e++){
if(e != i && at[e] < j-0.05  && b[e] > 0 ){
if(at[i] < j-0.05){
w[e]+=0.1;
}
}

}
if(b[i] <= 0.05){
chck1 = 0;
for(int k=1;k<=num;k++){
if(b[k] > 0 && at[k] <= j+0.1){
chck1++;
goto CC;
}
}
if(chck1 <= 0 ){
j+=0.1;
}

}
iii=1;
while(iii<num){
if(at[iii] >= j-0.05 && at[iii] <= j+0.05){
CC:
for(int k=1;k<=num;k++){
if(b[k] != 0 && at[k]<=j+0.05){
minBT=b[k];
break;
}
}
for(k=1;k<=num;k++){
if(b[k] != 0 && b[k] < minBT  && at[k]<=j+0.05){
minBT = b[k];
}
}
for(k=1;k<=num;k++){
if(b[k] >= minBT-0.05 && b[k] <= minBT+0.05){
i=k;
goto XY;
}
}
}
iii++;
}

}
}
j+=0.1;
}
AB:
cout<<"\n\tProcess\t\tBurst Time\tWating Time\tARIVAL TIME";
float avg=0,avg1=0;
for(int k=1;k<=num;k++){
cout<<endl<<"\t   "<<k+1<<"\t\t"<<tempBT[k]<<"\t\t"<<w[k]<<"\t\t"<<at[k];
avg+=w[k];
avg1+=w[k]+b[k];
}
cout<<endl;
/*for(k=0;k<a;k++){
cout<<"   "<<wb[k];
} */
cout<<endl<<"Average Wating Time : "<<avg/num;
cout<<endl<<"Average Turn Around Time : "<<avg1/num;
}


//Priority Algorithm
void cpu::priority()
{
int opt;
cout<<"\n1:Priority non_premptive"<<endl;
cout<<"2:Priority premptive"<<endl;
cout<<"3:back to menu"<<endl;
cout<<"4:exit"<<endl;
cout<"\n\t\tEnter ur choice=";
cin>>opt;
switch(opt)
{
case 1:
Priority();
break;
case 2:
priority_premptive();
break;
case 3:
Menu();
break;
case 4:
exit(0);
break;
default:
cout<<"invalid choice:"<<endl;
Menu();
getch();
break;
}
}
void cpu::Priority()
{
int i,j,proces,*ordr,pro;
float *B;int *P,bt,pri;
w=0.0;
int max;
clrscr();
Twt=0.0;atat=0.0;
max=1;
B=new float[n];
P=new int[n];
ordr=new int[n];
Getdata();
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
cout<<"Enter the priority for process P"<<process[i]<<"= "<<endl;
cin>>P[i];
}
for (i=n;i>=1;i++)
{

for (int jj=1;jj<=n;jj++)
{

if(P[jj]>P[jj+1])
{

 pri=P[jj];
 P[jj]=P[jj+1];
 P[jj+1]=pri;
//proces=process[j];
pro=process[jj];
process[jj]=process[jj+1];
process[jj+1]=pro;

bt=B[jj];
B[jj]=B[jj+1];
B[jj+1]=bt;
}

}

}



Wt[1]=0.0;
for (i=2;i<=n;i++)
{

Wt[i]=B[i-1]+Wt[i-1];
}
for (i=1;i<=n;i++)
{
tat[i]=Wt[i]+B[i];

}

for (i=1;i<=n;i++)
{
ordr[i]=process[i];
Twt+=Wt[i];
atat+=tat[i];
}

//calculating average weighting Time
Awt=Twt/n;
avgtat=atat/n;

displayPri(n,ordr,B,Wt,tat,P,Awt,avgtat);

}

//priority premptive


   void cpu:: priority_premptive(){
clrscr();
float input;
float x=1;
int amount,i,j;
cout<<endl;
cout<<"Enter No. of Processes:";
cin>>amount;
Process * p = new Process[amount];

for(i=0;i<amount;i++)
{
cout<<"Enter Burst Time of P"<<i+1<<" :";
cin>>input;
while(input<0)
{
cout<<"Re-enter the burst time:";
cin>>input;
}
p[i].burstTime=input;
}
for (i=0;i<amount;i++)
{
cout<<"Enter Arrival time of P"<<i+1<<" :";
cin>>input;
while(input<0)
{
cout<<"Re-enter the Arrival time:";
cin>>input;

}
 p[i].arr_time=input;
}
for (i=0;i<amount;i++)
 {
cout<<"Enter Priority of P"<<i+1<<" :";
cin>>p[i].priority;


p[i].p_number=i+1;
if(p[i].burstTime==0)
p[i].complete=1;
else
p[i].complete=0;
p[i].waitingTime=0;
p[i].turnAroundTime=0;
}
for(i=0;i<amount;i++)
{
for(int j=i+1;j<amount;j++)
{
if(p[i].arr_time>p[j].arr_time)
{
Process help=p[i];
p[i]=p[j];
p[j]=help;
}
}
}
for(i=0;i<amount;i++)
{
for(int j=i+1;j<amount;j++)
{
if(p[i].arr_time==p[j].arr_time)
{
if(p[i].priority>p[j].priority)
{
Process help=p[i];
p[i]=p[j];
p[j]=help;
}
}
}
}
int c=0;
float r_time=p[0].arr_time;
Process * temp=new Process[amount];
for(i=0;i<amount;i++)
temp[i]=p[i];
int first=0;
while(1)
{
int flag;
for(i=0;i<amount;i++)
{
if(temp[i].burstTime<=0)
{
flag=0;
}
else if(temp[i].burstTime>0){
flag=1;
if(first!=0)
{
int small=i;;
for(int j=0;j<amount;j++)
{
if(temp[small].priority>temp[j].priority && temp[j].arr_time<=r_time && temp[small].arr_time<r_time && temp[j].burstTime>0 &&temp[small].burstTime>0&&i!=j)
{
small=j;
}
}
c=small;
break;
}
}
}
if(flag==0){
break;
}
while(temp[c].burstTime>0)
{
for(i=0;i<amount;i++)
{
if(temp[i].arr_time==r_time && i!=c)
{
if(temp[c].priority>temp[i].priority)
{
c=i;
break;
}
}
}
temp[c].burstTime-=x;
for(i=0;i<amount;i++)
{
if(temp[i].arr_time<=r_time && temp[i].complete==0 && i!=c)
{
temp[i].waitingTime+=x;
}
}
r_time+=x;
first=1;
}
if(temp[c].burstTime<=0)
{
temp[c].burstTime=0;
temp[c].complete=1;
}

}
float avg_wt=0,avg_ta_t=0;
for(i=0;i<amount;i++)
{
p[i].burstTime=p[i].burstTime;
p[i].arr_time=p[i].arr_time;
temp[i].waitingTime=temp[i].waitingTime;

avg_wt+=temp[i].waitingTime;
avg_ta_t+=temp[i].waitingTime+p[i].burstTime;

p[i].waitingTime=temp[i].waitingTime;
p[i].turnAroundTime=p[i].burstTime+temp[i].waitingTime;
}
avg_wt=avg_wt/amount;
avg_ta_t=avg_ta_t/amount;
clrscr();
cout<<"\n\nProcess\tBurstTime\t";
cout<<"Arr time\tPriority\tWait time\tT.A.Tme\n\n";
for(i=0;i<amount;i++){
cout<<"P"<<p[i].p_number<<"\t"<<p[i].burstTime;
cout<<"\t\t"<<p[i].arr_time<<"\t\t"<<p[i].priority;
cout<<"\t\t"<<temp[i].waitingTime;
cout<<"\t\t"<<p[i].turnAroundTime<<"\n";
//delay(200);
}
cout<<"\n\n\n\t\t\tAverage waiting time = "<<avg_wt;
cout<<"\n\t\t\tAverage turn around time = "<<avg_ta_t;
return;
}

  //Round Robin algo
  void cpu::RoundRobin()
{


 clrscr();
float *bt,*at,*wt,t_slice=0;
int *proces;

Getdata();
bt=new float[n];
at=new float[n];
wt=new float[n];
proces=new int[n];


for(int i=1;i<=n;i++){

bt[i]=Bu[i];
at[i]=bt[i];
proces[i]=i;
wt[i]=0;
}
cout<<"\n\n\tEnter Time Slice : ";
cin>>t_slice;
i=0;
float j=0;
while(1){
if(bt[i] >= t_slice  ){
bt[i]-=t_slice;
j+=t_slice;
for(int k=1;k<=n;k++){
if(bt[k] >0 && k != i)
wt[k]+=t_slice;
}
i++;
}
else{
for(int k=1;k<=n;k++){
if(bt[k] > 0 && k != i)
wt[i]+= bt[i];
}
bt[i]=0;
j+=bt[i];
i++;
}
if(i%n <= 0)
i=1;
int flage = 0;
for(int k=1;k<=n;k++){
if(bt[k] <= 0){
flage++;
}
}
if(flage >= n){
break;
}
}
cout<<"\n\t   PROCESS\t   BRUST TIME\t   WATING TIME\t  TURN AROUND TIME";
float avg=0,avg1=0;
for(i=1;i<=n;i++){
cout<<endl<<"\t\t"<<i+1<<"\t\t"<<at[i]<<"\t\t"<<wt[i]<<"\t\t"<<wt[i]+at[i];
avg+=wt[i];
avg1+=(wt[i]+at[i]);
}
cout<<endl<<"Average Wating Time : "<<avg/n;
cout<<endl<<"Average Turn Around Time : "<<avg1/n;

}

void main()
{

cpu c;
int opt;
cout<<"\n\t\t\tGroup Members"<<endl;
cout<<"\t\t\t------------------------"<<endl;
cout<<"\t\t\tAfshan Saeed"<<endl;
cout<<"\t\t\tSana Ghalib"<<endl;
cout<<"\t\t\tHina Farid"<<endl;
cout<<"\t\t\t------------------------"<<endl;
//c.priority_premptive();
getch();

c.Menu();
getch();
}


void display(int n,int p[],float b[],float w[],float tat[], float avg1,float avg2)
{
cout<<"\n NAME"<<"\t\t"<<"Burst"<<"\t\t"<<"Wait"<<"\t\t"<<"Turn_Arround_Time"<<endl;

for(int k=1;k<=n;k++)
 {
  cout<<"\n P"<<p[k]<<"\t\t"<<b[k]<<"\t\t"<<w[k]<<"\t\t"<<tat[k]<<endl;
 }
 cout<<"average waiting tym:"<<avg1<<endl;
 cout<<"average turn around tym:"<<avg2<<endl;

}


void disp(int n,int p[],float b[],float w[],float at[],float tat[], float avg1,float avg2)
{
cout<<"\n NAME"<<"\t\t"<<"Burst"<<"\t\t"<<"Wait"<<"\t\t"<<"arrival Time"<<"\t\t"<<"Turn_Arround_Time"<<endl;

for(int k=1;k<=n;k++)
{
cout<<"\n P"<<p[k]<<"\t\t"<<b[k]<<"\t\t"<<w[k]<<"\t\t"<<tat[k]<<endl;
}

 cout<<"average waiting tym:"<<avg1<<endl;
 cout<<"average turn around tym:"<<avg2<<endl;

}

// display for priority algo

void displayPri(int n,int p[],float Bt[],float wt[],float tat[],int Pri[],float Awt, float avgtat)

{
cout<<"\n NAME"<<"\t"<<"Burst"<<"\t"<<"Wait"<<"\t"<<"Turn_Arround_Time"<<"\t"<<"Priority"<<endl;

for(int k=1;k<=n;k++)
{
cout<<"\n P"<<p[k]<<"\t"<<Bt[k]<<"\t"<<wt[k]<<"\t\t"<<tat[k]<<"\t\t"<<Pri[k]<<endl;
}

 cout<<"average waiting tym:"<<Awt<<endl;
 cout<<"average turn around tym:"<<avgtat<<endl;

}



No comments:

Post a Comment