C++ program to implement SRTF CPU scheduling algorithm with arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class srtf_alg
{
int ar[10],id[10],exe[10];
int n;
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
};
void srtf_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 "<<i+1<<" process : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
}
void srtf_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<endl;
}
void srtf_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 srtf_alg::cal_wt_tt()
{
int exe2[10],flag=1;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<exe[i])
max=exe[i];
}
at=ar[0];
min=max+1;
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at>=ar[i]&&min>exe[i]&&id[i]>0)
{
ind=i;
min=exe[i];
}
}
at++;
exe[ind]--;
min=max+1;
if(exe[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
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()
{
srtf_alg srtf;
srtf.getdata();
srtf.display();
srtf.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class srtf_alg
{
int ar[10],id[10],exe[10];
int n;
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
};
void srtf_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 "<<i+1<<" process : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
}
void srtf_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<endl;
}
void srtf_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 srtf_alg::cal_wt_tt()
{
int exe2[10],flag=1;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<exe[i])
max=exe[i];
}
at=ar[0];
min=max+1;
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at>=ar[i]&&min>exe[i]&&id[i]>0)
{
ind=i;
min=exe[i];
}
}
at++;
exe[ind]--;
min=max+1;
if(exe[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
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()
{
srtf_alg srtf;
srtf.getdata();
srtf.display();
srtf.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement SRTF 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 srtf_alg
{
int ar[10],id[10],exe[10];
int n,x,y;
char d[10];
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
void chart(int,int);
void int_to_ch(int);
};
void srtf_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 "<<i+1<<" process : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
}
void srtf_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 srtf_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 srtf_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 srtf_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 srtf_alg::cal_wt_tt()
{
int exe2[10],flag=1,z=y+10,prev,pa,pfl=0;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<exe[i])
max=exe[i];
}
at=ar[0];
min=max+1;
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]&&min>exe[i]&&id[i]>0)
{
ind=i;
min=exe[i];
}
}
at++;
exe[ind]--;
min=max+1;
if(prev!=id[ind]&&pfl)
chart(prev,pa);
prev=id[ind];
pa=at;
pfl=1;
if(exe[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
int_to_ch(id[ind]);
outtextxy(35,z,d);
int_to_ch(wt);
outtextxy(150,z,d);
int_to_ch(tnt);
outtextxy(270,z,d);
z+=16;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
if(flag==0)
chart(prev,pa);
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
srtf_alg srtf;
srtf.getdata();
srtf.display();
srtf.cal_wt_tt();
getch();
return 0;
}
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class srtf_alg
{
int ar[10],id[10],exe[10];
int n,x,y;
char d[10];
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
void chart(int,int);
void int_to_ch(int);
};
void srtf_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 "<<i+1<<" process : ";
cin>>exe[i]>>ar[i];
id[i]=i+1;
}
}
void srtf_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 srtf_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 srtf_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 srtf_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 srtf_alg::cal_wt_tt()
{
int exe2[10],flag=1,z=y+10,prev,pa,pfl=0;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<exe[i])
max=exe[i];
}
at=ar[0];
min=max+1;
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]&&min>exe[i]&&id[i]>0)
{
ind=i;
min=exe[i];
}
}
at++;
exe[ind]--;
min=max+1;
if(prev!=id[ind]&&pfl)
chart(prev,pa);
prev=id[ind];
pa=at;
pfl=1;
if(exe[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
int_to_ch(id[ind]);
outtextxy(35,z,d);
int_to_ch(wt);
outtextxy(150,z,d);
int_to_ch(tnt);
outtextxy(270,z,d);
z+=16;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
if(flag==0)
chart(prev,pa);
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
srtf_alg srtf;
srtf.getdata();
srtf.display();
srtf.cal_wt_tt();
getch();
return 0;
}
all scheduling only gives the same answer no use at all
ReplyDeleteall scheduling same ..... no use .... omg
ReplyDelete