CPU SCHEDULING
#include<iostream.h>
#include<conio.h>
class
schedule
{
public:
int bt,wt,at,tbt,twt,tat,p,t;
float awt,abt,aat;
};
schedule
ob,obp[100];
int
i,j,k,n,t,p[50],pa[100],bt[100];
void
get()
{
ob.twt=0,ob.tat=0,ob.tbt=0;
cout<<"\nEnter the number of
process: ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\nEnter the Burst Time for
P"<<i<<" : ";
cin>>obp[i].bt;
bt[i]=obp[i].bt;
ob.tbt=ob.tbt+obp[i].bt;
obp[i].t=0;
p[i]=i;
}
}
void
disp()
{
cout<<"\n\n";
for(i=1;i<=n;i++)
{
cout<<"\nBurst time for
P"<<i<<" :
"<<obp[i].bt<<endl;
cout<<"\nWaiting time for
P"<<i<<" :
"<<obp[i].wt<<endl;
cout<<"\nTurnAround time for
P"<<i<<" :
"<<obp[i].at<<endl;
}
cout<<"\nTotal Burst time : "<<ob.tbt<<endl;
cout<<"\nTotal Waiting time:
"<<ob.twt<<endl;
cout<<"\nTotal Around time :
"<<ob.tat<<endl;
cout<<"\nAverage Burst time : "<<ob.abt<<endl;
cout<<"\nAverage Waiting time : "<<ob.awt<<endl;
cout<<"\nAverage TurnAround time:
"<<ob.aat<<endl;
}
void
fcfs()
{
clrscr();
get();
cout<<"\n\nFCFS Process Array:
\n\n";
for(i=1;i<=n;i++)
cout<<"P"<<i<<"\t";
i=1;
obp[i].wt=0;
obp[i].at=obp[1].bt;
for(i=2;i<=n;i++)
{
obp[i].wt=obp[i-1].wt+obp[i-1].bt;
obp[i].at=obp[i].wt+obp[i].bt;
}
for(i=1;i<=n;i++)
{
ob.twt=ob.twt+obp[i].wt;
ob.tat=ob.tat+obp[i].at;
}
ob.awt=(float)ob.twt/n;
ob.abt=(float)ob.tbt/n;
ob.aat=(float)ob.tat/n;
disp();
}
void
sjf()
{
clrscr();
get();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(bt[i]==obp[j].bt)
{
p[i]=j;
}
cout<<"\nShortest Job First Process
Array: \n\n";
for(i=1;i<=n;i++)
cout<<"P"<<p[i]<<"\t";
cout<<"\n\n\n";
i=1;
obp[p[i]].wt=0;
obp[p[i]].at=obp[p[i]].bt;
for(i=2;i<=n;i++)
{
obp[p[i]].wt=obp[p[i-1]].wt+obp[p[i-1]].bt;
obp[p[i]].at=obp[p[i]].wt+obp[p[i]].bt;
}
for(i=1;i<=n;i++)
{
ob.twt=ob.twt+obp[i].wt;
ob.tat=ob.tat+obp[i].at;
}
ob.awt=(float)ob.twt/n;
ob.abt=(float)ob.tbt/n;
ob.aat=(float)ob.tat/n;
disp();
}
void
roundrobin()
{
clrscr();
get();
for(i=1,j=1,k=n;i<=ob.tbt;i++)
{
pa[i]=p[j];
obp[p[j]].t=obp[p[j]].t+1;
if(obp[p[j]].t==obp[p[j]].bt)
{
if(j==k)
{
p[j]='\0';
j=j-1;
}
else
{
for(t=j;t<=k-1;t++)
{
p[t]=p[t+1];
}
p[t]='\0';
}
k=k-1;
if(j!=p[k])
goto l;
}
if(j>=k)
j=1;
else
j++;
l:
}
pa[i]='\0';
cout<<"\nRound Robin Schedule
Process Array: \n\n";
i=1;
while(pa[i]!='\0')
{
cout<<"P"<<pa[i]<<"
";
i++;
}
for(i=1;i<=n;i++)
{
for(j=ob.tbt;j>0;j--)
{
if(pa[j]==i)
{
obp[i].wt=j-obp[i].bt;
obp[i].at=obp[i].bt+obp[i].wt;
break;
}
}
ob.twt=ob.twt+obp[i].wt;
ob.tat=ob.tat+obp[i].at;
}
ob.awt=(float)ob.twt/n;
ob.abt=(float)ob.tbt/n;
ob.aat=(float)ob.tat/n;
disp();
}
void
main()
{
int ch;
clrscr();
cout<<"\nCPU SCHEDULING
"<<endl;
cout<<"\n1.FCFS\n2.SJF\n3.Round
Robin\n4.Exit\n\n";
do
{
cout<<"\n\nEnter your
choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1: fcfs();
break;
case 2: sjf();
break;
case 3: roundrobin();
break;
}
}while(ch!=4);
getch();
}
OUPUT:
CPU
SCHEDULING
1.FCFS
2.SJF
3.Round
Robin
4.Exit
Enter
your choice:
1
Enter
the number of process: 3
Enter
the Burst Time for P1 : 3
Enter
the Burst Time for P2 : 2
Enter
the Burst Time for P3 : 1
FCFS
Process Array:
P1 P2
P3
Burst
time for P1 : 3
Waiting
time for P1 : 0
TurnAround
time for P1 : 3
Burst
time for P2 : 2
Waiting
time for P2 : 3
TurnAround
time for P2 : 5
Burst
time for P3 : 1
Waiting
time for P3 : 5
TurnAround
time for P3 : 6
Total
Burst time : 6
Total
Waiting time: 8
Total
Around time : 14
Average
Burst time : 2
Average
Waiting time : 2.666667
Average
TurnAround time: 4.666667
Enter
your choice:
2
Enter
the number of process: 3
Enter
the Burst Time for P1 : 4
Enter
the Burst Time for P2 : 3
Enter
the Burst Time for P3 : 7
Shortest
Job First Process Array:
P2 P1
P3
Burst
time for P1 : 4
Waiting
time for P1 : 3
TurnAround
time for P1 : 7
Burst
time for P2 : 3
Waiting
time for P2 : 0
TurnAround
time for P2 : 3
Burst
time for P3 : 7
Waiting
time for P3 : 7
TurnAround
time for P3 : 14
Total
Burst time : 14
Total
Waiting time: 10
Total
Around time : 24
Average
Burst time : 4.666667
Average
Waiting time : 3.333333
Average
TurnAround time: 8
Enter
your choice:
3
Enter
the number of process: 3
Enter
the Burst Time for P1 : 9
Enter
the Burst Time for P2 : 3
Enter
the Burst Time for P3 : 1
Round
Robin Schedule Process Array:
P1
P2 P3 P1 P2 P1 P2 P1 P1 P1 P1 P1 P1
Burst
time for P1 : 9
Waiting
time for P1 : 4
TurnAround
time for P1 : 13
Burst
time for P2 : 3
Waiting
time for P2 : 4
TurnAround
time for P2 : 7
Burst
time for P3 : 1
Waiting
time for P3 : 2
TurnAround
time for P3 : 3
Total
Burst time : 13
Total
Waiting time: 10
Total
Around time : 23
Average
Burst time : 4.333333
Average
Waiting time : 3.333333
Average
TurnAround time: 7.666667
Enter
your choice:
4
No comments:
Post a Comment