C++ program to implement SJF CPU scheduling algorithm without arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class sjf_alg
{
int exe[10],id[10];
int n;
void sort(int *,int*);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of "<<i+1<<" process : ";
cin>>exe[i];
id[i]=i+1;
}
}
void sjf_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 sjf_alg::sort(int *f,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int wt=0,tnt=0;
float avg=0,avtnt=0;
sort(exe,id);
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int i=0;i<n;i++)
{
tnt+=exe[i];
avtnt+=tnt;
avg+=wt;
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
wt+=exe[i];
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class sjf_alg
{
int exe[10],id[10];
int n;
void sort(int *,int*);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of "<<i+1<<" process : ";
cin>>exe[i];
id[i]=i+1;
}
}
void sjf_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 sjf_alg::sort(int *f,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int wt=0,tnt=0;
float avg=0,avtnt=0;
sort(exe,id);
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int i=0;i<n;i++)
{
tnt+=exe[i];
avtnt+=tnt;
avg+=wt;
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
wt+=exe[i];
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement SJF CPU scheduling algorithm with arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class sjf_alg
{
int ar[10],exe[10],id[10];
int n;
void sort(int *,int *,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes 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 sjf_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 sjf_alg::sort(int *f,int *m,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
temp=m[z];
m[z]=m[z+1];
m[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int flag=1;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,exe,id);
for(int i=1;i<n;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]!=-1)
{
ind=i;
min=exe[i];
}
}
if(id[ind]==-1)
{
at++;
continue;
}
wt=at-ar[ind];
at+=exe[ind];
avg+=wt;
tnt=at-ar[ind];
avtnt+=tnt;
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
min=max+1;
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()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class sjf_alg
{
int ar[10],exe[10],id[10];
int n;
void sort(int *,int *,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes 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 sjf_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 sjf_alg::sort(int *f,int *m,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
temp=m[z];
m[z]=m[z+1];
m[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int flag=1;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,exe,id);
for(int i=1;i<n;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]!=-1)
{
ind=i;
min=exe[i];
}
}
if(id[ind]==-1)
{
at++;
continue;
}
wt=at-ar[ind];
at+=exe[ind];
avg+=wt;
tnt=at-ar[ind];
avtnt+=tnt;
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
min=max+1;
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()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement SJF 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 sjf_alg
{
int ar[10],exe[10],id[10];
int n,x,y;
char d[10];
void sort(int *,int *,int *);
public:
void getdata();
void display();
void chart(int,int);
void int_to_ch(int);
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes 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 sjf_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 sjf_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 sjf_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 sjf_alg::sort(int *f,int *m,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
temp=m[z];
m[z]=m[z+1];
m[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int flag=1,z=y+10,prev=1,ap;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,exe,id);
for(int i=1;i<n;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]!=-1)
{
ind=i;
min=exe[i];
}
}
if(id[ind]==-1)
{
at++;
prev=-1;
ap=at;
continue;
}
if(prev==-1)
{
chart(prev,ap);
prev=1;
}
wt=at-ar[ind];
at+=exe[ind];
avg+=wt;
tnt=at-ar[ind];
avtnt+=tnt;
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);
chart(id[ind],at);
z+=16;
id[ind]=-1;
min=max+1;
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()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
getch();
return 0;
}
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class sjf_alg
{
int ar[10],exe[10],id[10];
int n,x,y;
char d[10];
void sort(int *,int *,int *);
public:
void getdata();
void display();
void chart(int,int);
void int_to_ch(int);
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes 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 sjf_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 sjf_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 sjf_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 sjf_alg::sort(int *f,int *m,int *l)
{
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=l[z];
l[z]=l[z+1];
l[z+1]=temp;
temp=m[z];
m[z]=m[z+1];
m[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int flag=1,z=y+10,prev=1,ap;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,exe,id);
for(int i=1;i<n;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]!=-1)
{
ind=i;
min=exe[i];
}
}
if(id[ind]==-1)
{
at++;
prev=-1;
ap=at;
continue;
}
if(prev==-1)
{
chart(prev,ap);
prev=1;
}
wt=at-ar[ind];
at+=exe[ind];
avg+=wt;
tnt=at-ar[ind];
avtnt+=tnt;
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);
chart(id[ind],at);
z+=16;
id[ind]=-1;
min=max+1;
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()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
getch();
return 0;
}
u should've put some comments on it.
ReplyDelete