I have created this program for comparison between various cpu scheduling algorithms. I have included First Come First Serve(FCFS), Shortest Job First(SJF), Priority Scheduling and Round Robin Scheduling in it.
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
//fcfs
int temp=0,i,PR3[4],tq,B1[4],TAT1[4],B2[4],B3[4],B4[4],W1[4],SWT1=0,STT1=0;
float AWT1=0.0,ATT1=0.0;
char P1[4];
for(i=0;i<3;i++)
{
cout<<"Enter process names with their Burst Time and Priority(Ashish)\n";
cin>>P1[i]>>B1[i]>>PR3[i];
B2[i]=B1[i];
B3[i]=B1[i];
B4[i]=B1[i];
}
cout<<"\nEnter time slice for round robin\n";
cin>>tq;
W1[0]=0;
for(i=1;i<3;i++)
W1[i]=W1[i-1]+B1[i-1];
for(i=0;i<3;i++)
TAT1[i]=W1[i]+B1[i];
for(i=0;i<3;i++)
{
SWT1=SWT1+W1[i];
STT1=STT1+TAT1[i];
}
AWT1=(float)SWT1/3;
ATT1=(float)STT1/3;
//sjf
int W2[4],TAT2[4],SWT2=0,STT2=0;
float AWT2=0.0,ATT2=0.0;
for(int j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(B2[i]>B2[i+1])
{temp=B2[i];
B2[i]=B2[i+1];
B2[i+1]=temp;}
}
}
W2[0]=0;
for(i=1;i<3;i++)
W2[i]=W2[i-1]+B2[i-1];
for(i=0;i<3;i++)
TAT2[i]=W2[i]+B2[i];
for(i=0;i<3;i++)
{
SWT2=SWT2+W2[i];
STT2=STT2+TAT2[i];
}
AWT2=(float)SWT2/3;
ATT2=(float)STT2/3;
//priority
int W3[4],TAT3[4],SWT3=0,STT3=0;
float AWT3=0.0,ATT3=0.0;
for(j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(PR3[i]>PR3[i+1])
{temp=PR3[i];
PR3[i]=PR3[i+1];
PR3[i+1]=temp;
temp=B3[i];
B3[i]=B3[i+1];
B3[i+1]=temp;
}
}
}
W3[0]=0;
for(i=1;i<3;i++)
W3[i]=W3[i-1]+B3[i-1];
for(i=0;i<3;i++)
TAT3[i]=W3[i]+B3[i];
for(i=0;i<3;i++)
{
SWT3=SWT3+W3[i];
STT3=STT3+TAT3[i];
}
AWT3=(float)SWT3/3;
ATT3=(float)STT3/3;
//round
int st[10],wt[10], tat[10];
int count=0, swt=0, stat=0, sq=0,n=3;
float AWT4=0.0, ATT4=0.0;
for(i=0;i<3;i++)
st[i]=B4[i];
while(1)
{
for(i=0, count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
{
st[i]=st[i]-tq;
}
else
if (st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-B4[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
AWT4=(float)swt/n;
ATT4=(float)stat/n;
//calculation
cout<<"FCFS- Average waiting time is "<<AWT1<<" and Average turn around time is "<<ATT1<<"\n";
cout<<"SJF- Average waiting time is "<<AWT2<<" and Average turn around time is "<<ATT2<<"\n";
cout<<"PRIORITY- Average waiting time is "<<AWT3<<" and Average turn around time is "<<ATT3<<"\n";
cout<<"ROUND ROBIN- Average waiting time is "<<AWT4<<" and Average turn around time is "<<ATT4<<"\n";
if(AWT1<AWT2 && AWT1<AWT3 && AWT1<AWT4)
{
cout<<"FCFS Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT1<<" and Average Turn Around time= "<<ATT1;
}
else if(AWT2<AWT1 && AWT2<AWT3 && AWT2<AWT4)
{
cout<<"SJF Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT2<<" and Average Turn Around time= "<<ATT2;
}
else if(AWT3<AWT1 && AWT3<AWT2 && AWT3<AWT4)
{
cout<<"Priority Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT3<<" and Average Turn Around time= "<<ATT3;
}
else
{
cout<<"Round Robin Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT4<<" and Average Turn Around time= "<<ATT4;
}
getch();
}
Any doubt?? Drop a comment!
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
//fcfs
int temp=0,i,PR3[4],tq,B1[4],TAT1[4],B2[4],B3[4],B4[4],W1[4],SWT1=0,STT1=0;
float AWT1=0.0,ATT1=0.0;
char P1[4];
for(i=0;i<3;i++)
{
cout<<"Enter process names with their Burst Time and Priority(Ashish)\n";
cin>>P1[i]>>B1[i]>>PR3[i];
B2[i]=B1[i];
B3[i]=B1[i];
B4[i]=B1[i];
}
cout<<"\nEnter time slice for round robin\n";
cin>>tq;
W1[0]=0;
for(i=1;i<3;i++)
W1[i]=W1[i-1]+B1[i-1];
for(i=0;i<3;i++)
TAT1[i]=W1[i]+B1[i];
for(i=0;i<3;i++)
{
SWT1=SWT1+W1[i];
STT1=STT1+TAT1[i];
}
AWT1=(float)SWT1/3;
ATT1=(float)STT1/3;
//sjf
int W2[4],TAT2[4],SWT2=0,STT2=0;
float AWT2=0.0,ATT2=0.0;
for(int j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(B2[i]>B2[i+1])
{temp=B2[i];
B2[i]=B2[i+1];
B2[i+1]=temp;}
}
}
W2[0]=0;
for(i=1;i<3;i++)
W2[i]=W2[i-1]+B2[i-1];
for(i=0;i<3;i++)
TAT2[i]=W2[i]+B2[i];
for(i=0;i<3;i++)
{
SWT2=SWT2+W2[i];
STT2=STT2+TAT2[i];
}
AWT2=(float)SWT2/3;
ATT2=(float)STT2/3;
//priority
int W3[4],TAT3[4],SWT3=0,STT3=0;
float AWT3=0.0,ATT3=0.0;
for(j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(PR3[i]>PR3[i+1])
{temp=PR3[i];
PR3[i]=PR3[i+1];
PR3[i+1]=temp;
temp=B3[i];
B3[i]=B3[i+1];
B3[i+1]=temp;
}
}
}
W3[0]=0;
for(i=1;i<3;i++)
W3[i]=W3[i-1]+B3[i-1];
for(i=0;i<3;i++)
TAT3[i]=W3[i]+B3[i];
for(i=0;i<3;i++)
{
SWT3=SWT3+W3[i];
STT3=STT3+TAT3[i];
}
AWT3=(float)SWT3/3;
ATT3=(float)STT3/3;
//round
int st[10],wt[10], tat[10];
int count=0, swt=0, stat=0, sq=0,n=3;
float AWT4=0.0, ATT4=0.0;
for(i=0;i<3;i++)
st[i]=B4[i];
while(1)
{
for(i=0, count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
{
st[i]=st[i]-tq;
}
else
if (st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-B4[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
AWT4=(float)swt/n;
ATT4=(float)stat/n;
//calculation
cout<<"FCFS- Average waiting time is "<<AWT1<<" and Average turn around time is "<<ATT1<<"\n";
cout<<"SJF- Average waiting time is "<<AWT2<<" and Average turn around time is "<<ATT2<<"\n";
cout<<"PRIORITY- Average waiting time is "<<AWT3<<" and Average turn around time is "<<ATT3<<"\n";
cout<<"ROUND ROBIN- Average waiting time is "<<AWT4<<" and Average turn around time is "<<ATT4<<"\n";
if(AWT1<AWT2 && AWT1<AWT3 && AWT1<AWT4)
{
cout<<"FCFS Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT1<<" and Average Turn Around time= "<<ATT1;
}
else if(AWT2<AWT1 && AWT2<AWT3 && AWT2<AWT4)
{
cout<<"SJF Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT2<<" and Average Turn Around time= "<<ATT2;
}
else if(AWT3<AWT1 && AWT3<AWT2 && AWT3<AWT4)
{
cout<<"Priority Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT3<<" and Average Turn Around time= "<<ATT3;
}
else
{
cout<<"Round Robin Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT4<<" and Average Turn Around time= "<<ATT4;
}
getch();
}
Any doubt?? Drop a comment!
No comments:
Post a Comment