C++ program to implement Round Robin CPU scheduling algorithm without arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class rr_alg
{
int exe[10];
int n,q;
public:
void getdata();
void display();
void cal_wt_tt();
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of process "<<i+1<<" : ";
cin>>exe[i];
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<"0"<<endl;
}
void rr_alg::cal_wt_tt()
{
int texe=0,exe2[10],flag=1;
int wt=0,tnt=0;
float avg=0,avtnt=0;
for(int i=0;i<n;i++)
{
texe+=exe[i];
exe2[i]=exe[i];
}
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int y=0;y<texe;y++)
{
for(int i=0;i<n;i++)
{
if(exe[i]<=0)
continue;
tnt+=q;
exe[i]-=q;
if(exe[i]<=0)
{
tnt+=exe[i];
wt=tnt-exe2[i];
cout<<setw(5)<<i+1<<setw(15)<<wt<<setw(15)<<tnt<<endl;
avg+=wt;
avtnt+=tnt;
}
}
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class rr_alg
{
int exe[10];
int n,q;
public:
void getdata();
void display();
void cal_wt_tt();
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of process "<<i+1<<" : ";
cin>>exe[i];
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<"0"<<endl;
}
void rr_alg::cal_wt_tt()
{
int texe=0,exe2[10],flag=1;
int wt=0,tnt=0;
float avg=0,avtnt=0;
for(int i=0;i<n;i++)
{
texe+=exe[i];
exe2[i]=exe[i];
}
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int y=0;y<texe;y++)
{
for(int i=0;i<n;i++)
{
if(exe[i]<=0)
continue;
tnt+=q;
exe[i]-=q;
if(exe[i]<=0)
{
tnt+=exe[i];
wt=tnt-exe2[i];
cout<<setw(5)<<i+1<<setw(15)<<wt<<setw(15)<<tnt<<endl;
avg+=wt;
avtnt+=tnt;
}
}
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement Round Robin CPU scheduling algorithm with arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class rr_alg
{
int ar[10],id[10],exe[10];
int n,q;
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and arrival time of process "<<i+1<<" : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<endl;
}
void rr_alg::sort(int *f,int *mid,int *last)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
}
void rr_alg::cal_wt_tt()
{
int exe2[10],flag=1,flag1=0;
int at=0,wt,tnt;
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
exe2[i]=exe[i];
at=ar[0];
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at<ar[i]||id[i]<0)
continue;
flag1=1;
at+=q;
exe[i]-=q;
if(exe[i]<=0)
{
at+=exe[i];
wt=at-exe2[i]-ar[i];
tnt=at-ar[i];
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[i]=-1;
avg+=wt;
avtnt+=tnt;
}
}
if(flag1==0)
at++;
flag1=flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class rr_alg
{
int ar[10],id[10],exe[10];
int n,q;
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and arrival time of process "<<i+1<<" : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<endl;
}
void rr_alg::sort(int *f,int *mid,int *last)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
}
void rr_alg::cal_wt_tt()
{
int exe2[10],flag=1,flag1=0;
int at=0,wt,tnt;
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
exe2[i]=exe[i];
at=ar[0];
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at<ar[i]||id[i]<0)
continue;
flag1=1;
at+=q;
exe[i]-=q;
if(exe[i]<=0)
{
at+=exe[i];
wt=at-exe2[i]-ar[i];
tnt=at-ar[i];
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[i]=-1;
avg+=wt;
avtnt+=tnt;
}
}
if(flag1==0)
at++;
flag1=flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement Round Robin CPU scheduling algorithm with Gantt chart
Following program uses BGI graphics and compiled it using TurboC/C++ 3.0 compiler.
#include<graphics.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class rr_alg
{
int ar[10],id[10],exe[10];
int n,q,x,y;
char d[10];
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
void int_to_ch(int);
void chart(int,int);
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and arrival time of process "<<i+1<<" : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::int_to_ch(int temp)
{
int i;
if(temp==0)
{
d[0]=48;
d[1]=0;
return;
}
for(i=0;temp!=0;i++)
{
d[i]=temp%10+48;
temp/=10;
}
d[i]=0;
strrev(d);
}
void rr_alg::chart(int id,int t)
{
rectangle(x,y,x+30,y+30);
if(id!=-1)
{
int_to_ch(id);
outtextxy(x+12,y+12,d);
}
int_to_ch(t);
if(t>=10)
outtextxy(x+24,y+33,d);
else
outtextxy(x+30,y+33,d);
x+=30;
if(x+60>getmaxx())
{
y+=50;
x=5;
outtextxy(x,y+33,d);
}
}
void rr_alg::display()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
outtextxy(5,56,"Process ID Execution time Arrival Time ");
x=5,y=66;
for(int i=0;i<n;i++,y+=16)
{
int_to_ch(i+1);
outtextxy(35,y,d);
int_to_ch(exe[i]);
outtextxy(150,y,d);
int_to_ch(ar[i]);
outtextxy(270,y,d);
}
}
void rr_alg::sort(int *f,int *mid,int *last)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
}
void rr_alg::cal_wt_tt()
{
int exe2[10],flag=1,flag1=0,fl=1;
int at=0,wt,tnt,z=y+10,prev,ap;
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
exe2[i]=exe[i];
at=ar[0];
y+=(n+2)*16;
outtextxy(x,y+10,"GANTT CHART");
y=y+26;
int_to_ch(at);
outtextxy(x,y+33,d);
outtextxy(x,z,"Process id Waiting time Turn around time ");
z=z+16;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at<ar[i]||id[i]<0)
continue;
if(fl==0)
chart(-1,at);
fl=flag1=1;
at+=q;
exe[i]-=q;
if(exe[i]<0)
at+=exe[i];
chart(id[i],at);
if(exe[i]<=0)
{
wt=at-exe2[i]-ar[i];
tnt=at-ar[i];
int_to_ch(id[i]);
outtextxy(35,z,d);
int_to_ch(wt);
outtextxy(150,z,d);
int_to_ch(tnt);
outtextxy(270,z,d);
z+=16;
id[i]=-1;
avg+=wt;
avtnt+=tnt;
}
}
if(flag1==0)
{
at++;
fl=0;
}
flag1=flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
getch();
return 0;
}
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class rr_alg
{
int ar[10],id[10],exe[10];
int n,q,x,y;
char d[10];
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
void int_to_ch(int);
void chart(int,int);
};
void rr_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and arrival time of process "<<i+1<<" : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
cout<<"Enter Quantum slice : ";
cin>>q;
}
void rr_alg::int_to_ch(int temp)
{
int i;
if(temp==0)
{
d[0]=48;
d[1]=0;
return;
}
for(i=0;temp!=0;i++)
{
d[i]=temp%10+48;
temp/=10;
}
d[i]=0;
strrev(d);
}
void rr_alg::chart(int id,int t)
{
rectangle(x,y,x+30,y+30);
if(id!=-1)
{
int_to_ch(id);
outtextxy(x+12,y+12,d);
}
int_to_ch(t);
if(t>=10)
outtextxy(x+24,y+33,d);
else
outtextxy(x+30,y+33,d);
x+=30;
if(x+60>getmaxx())
{
y+=50;
x=5;
outtextxy(x,y+33,d);
}
}
void rr_alg::display()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
outtextxy(5,56,"Process ID Execution time Arrival Time ");
x=5,y=66;
for(int i=0;i<n;i++,y+=16)
{
int_to_ch(i+1);
outtextxy(35,y,d);
int_to_ch(exe[i]);
outtextxy(150,y,d);
int_to_ch(ar[i]);
outtextxy(270,y,d);
}
}
void rr_alg::sort(int *f,int *mid,int *last)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
}
void rr_alg::cal_wt_tt()
{
int exe2[10],flag=1,flag1=0,fl=1;
int at=0,wt,tnt,z=y+10,prev,ap;
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
exe2[i]=exe[i];
at=ar[0];
y+=(n+2)*16;
outtextxy(x,y+10,"GANTT CHART");
y=y+26;
int_to_ch(at);
outtextxy(x,y+33,d);
outtextxy(x,z,"Process id Waiting time Turn around time ");
z=z+16;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at<ar[i]||id[i]<0)
continue;
if(fl==0)
chart(-1,at);
fl=flag1=1;
at+=q;
exe[i]-=q;
if(exe[i]<0)
at+=exe[i];
chart(id[i],at);
if(exe[i]<=0)
{
wt=at-exe2[i]-ar[i];
tnt=at-ar[i];
int_to_ch(id[i]);
outtextxy(35,z,d);
int_to_ch(wt);
outtextxy(150,z,d);
int_to_ch(tnt);
outtextxy(270,z,d);
z+=16;
id[i]=-1;
avg+=wt;
avtnt+=tnt;
}
}
if(flag1==0)
{
at++;
fl=0;
}
flag1=flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
rr_alg rr;
rr.getdata();
rr.display();
rr.cal_wt_tt();
getch();
return 0;
}
No comments:
Post a Comment