C++ program to implement Non Preemptive Priority CPU scheduling algorithm without arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[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 priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<pr[i]<<endl;
}
void priority_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 priority_alg::cal_wt_tt()
{
float avg=0,avtnt=0,wt=0,tnt=0;
sort(pr,id,exe);
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)<<tnt<<setw(15)<<wt<<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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[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 priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<pr[i]<<endl;
}
void priority_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 priority_alg::cal_wt_tt()
{
float avg=0,avtnt=0,wt=0,tnt=0;
sort(pr,id,exe);
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)<<tnt<<setw(15)<<wt<<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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement Non Preemptive Priority CPU scheduling algorithm with arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n;
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time \tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<setw(15)<<pr[i]<<endl;
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int flag=1;
int at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=1;i<n;i++)
if(max<pr[i])
max=pr[i];
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>pr[i]&&id[i]>0)
{
ind=i;
min=pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n;
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time \tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<setw(15)<<pr[i]<<endl;
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int flag=1;
int at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=1;i<n;i++)
if(max<pr[i])
max=pr[i];
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>pr[i]&&id[i]>0)
{
ind=i;
min=pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement Preemptive Priority CPU scheduling algorithm with arrival time
#include<iostream>
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n;
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time \tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<setw(15)<<pr[i]<<endl;
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int flag=1;
int exe2[10],at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=0;i<n;i++)
{
if(max<pr[i])
max=pr[i];
exe2[i]=exe[i];
}
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>pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
#include<iomanip>
using namespace std;
class priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n;
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time \tPriority "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<setw(15)<<pr[i]<<endl;
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int flag=1;
int exe2[10],at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=0;i<n;i++)
{
if(max<pr[i])
max=pr[i];
exe2[i]=exe[i];
}
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>pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
return 0;
}
OUTPUT
C++ program to implement Preemptive Priority 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 priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n,x,y;
char d[20];
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
void int_to_ch(int);
void chart(int,int);
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_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 priority_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 priority_alg::display()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
outtextxy(5,56,"Process ID Execution time Arrival Time Priority ");
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);
int_to_ch(pr[i]);
outtextxy(385,y,d);
}
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int exe2[10],flag=1,z=y+10,prev,pa,pfl=0;
int at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<pr[i])
max=pr[i];
}
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>pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
getch();
return 0;
}
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class priority_alg
{
int pr[10],id[10],exe[10],ar[10];
int n,x,y;
char d[20];
void sort(int*,int*,int*,int *);
public:
void getdata();
void display();
void cal_wt_tt();
void int_to_ch(int);
void chart(int,int);
};
void priority_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution,arrival time and priority of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]>>pr[i];
id[i]=i+1;
}
}
void priority_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 priority_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 priority_alg::display()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
outtextxy(5,56,"Process ID Execution time Arrival Time Priority ");
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);
int_to_ch(pr[i]);
outtextxy(385,y,d);
}
}
void priority_alg::sort(int *f,int *mid,int *last,int *ll)
{
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;
temp=ll[z];
ll[z]=ll[z+1];
ll[z+1]=temp;
}
}
}
void priority_alg::cal_wt_tt()
{
int exe2[10],flag=1,z=y+10,prev,pa,pfl=0;
int at=0,ind,wt,tnt,min,max=pr[0];
float avg=0,avtnt=0;
sort(ar,id,exe,pr);
at=ar[0];
sort(pr,id,exe,ar);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<pr[i])
max=pr[i];
}
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>pr[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()
{
priority_alg priority;
priority.getdata();
priority.display();
priority.cal_wt_tt();
getch();
return 0;
}
Ty, it helps alot
ReplyDeletethanks a lot it really helps my study
ReplyDeleteplease make code C++ but don't use Arrival time
ReplyDelete