Round Robin Process Scheduling Algorithm in C++
Here is my own written code in C++ for Round Robin process scheduling algorithm.Hope its useful for you.Any improvement to this code is welcomed.So this is your code:
Source code:
# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
void main(){
clrscr();
int slice,i,j,m,wt[10],st[10],et[10],n,tat[10],pt[10],a[10],twt=0,ttat=0;
float avgwt,avgtat;
char p[10][10];
cout<<"enter no of process";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the name of process:";
cin>> p[i];
cout<<"\n Enter the burst time:";
cin>>pt[i];
}
cout<<"Enter the time-slice:";
cin>>slice;
st[0]=0;
i=0;
do{
if(pt[i]<=slice)
{et[i]=st[i]+pt[i];
pt[i]=0;
tat[i]=et[i];}
else
{et[i]=st[i]+slice;
pt[i]=pt[i]-slice;}
wt[i]=st[i];
st[i+1]=et[i];
i=i+1;
}while(i<n);
m=n-1;
j=0;
a[m]=1234;
do{ for(i=0;i<n;i++){
if(pt[i]==0)
{tat[i]=tat[i];}
else
{st[m+1]=et[m];
a[m+1]=i;
if(pt[i]<=slice)
{et[m+1]=st[m+1]+pt[i];
pt[i]=0;
tat[i]=et[m+1];}
else
{et[m+1]=st[m+1]+slice;
pt[i]=pt[i]-slice;}
if(a[m]==a[m+1])
{wt[i]=wt[i]+0;}
else
{wt[i]+=st[m+1]-et[i];}
m=m+1;} }
if(pt[j]!=0)continue;
j=j+1;
}while(j<n);
cout<<"\n name"<<"\t waiting time"<<"\t turn aroun time";
for(i=0;i<n;i++)
{cout<<"\n"<<p[i]<<"\t\t"<<wt[i]<<"\t\t\t"<<tat[i]<<"\n";
twt+=wt[i];
ttat+=tat[i];
}
avgwt=(float) (twt)/n;
avgtat= (float)(ttat)/n;
cout<<"\n Average waiting time:"<<avgwt;
cout<<"\n Average turn around time:"<<avgtat;
getch();
}
# include<conio.h>
# include<stdlib.h>
void main(){
clrscr();
int slice,i,j,m,wt[10],st[10],et[10],n,tat[10],pt[10],a[10],twt=0,ttat=0;
float avgwt,avgtat;
char p[10][10];
cout<<"enter no of process";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the name of process:";
cin>> p[i];
cout<<"\n Enter the burst time:";
cin>>pt[i];
}
cout<<"Enter the time-slice:";
cin>>slice;
st[0]=0;
i=0;
do{
if(pt[i]<=slice)
{et[i]=st[i]+pt[i];
pt[i]=0;
tat[i]=et[i];}
else
{et[i]=st[i]+slice;
pt[i]=pt[i]-slice;}
wt[i]=st[i];
st[i+1]=et[i];
i=i+1;
}while(i<n);
m=n-1;
j=0;
a[m]=1234;
do{ for(i=0;i<n;i++){
if(pt[i]==0)
{tat[i]=tat[i];}
else
{st[m+1]=et[m];
a[m+1]=i;
if(pt[i]<=slice)
{et[m+1]=st[m+1]+pt[i];
pt[i]=0;
tat[i]=et[m+1];}
else
{et[m+1]=st[m+1]+slice;
pt[i]=pt[i]-slice;}
if(a[m]==a[m+1])
{wt[i]=wt[i]+0;}
else
{wt[i]+=st[m+1]-et[i];}
m=m+1;} }
if(pt[j]!=0)continue;
j=j+1;
}while(j<n);
cout<<"\n name"<<"\t waiting time"<<"\t turn aroun time";
for(i=0;i<n;i++)
{cout<<"\n"<<p[i]<<"\t\t"<<wt[i]<<"\t\t\t"<<tat[i]<<"\n";
twt+=wt[i];
ttat+=tat[i];
}
avgwt=(float) (twt)/n;
avgtat= (float)(ttat)/n;
cout<<"\n Average waiting time:"<<avgwt;
cout<<"\n Average turn around time:"<<avgtat;
getch();
}
OUTPUT:
What a co-incidence ,i too have written the same code indeed by myself...!!!
ReplyDeleteanyway good work...
a rare coincidence..i still cant believe!!logic might be similar but nt d entire code!
ReplyDeletethank you so much :)
ReplyDeleteIts my pleasure :)
Deleteits good thanks
ReplyDeleteAng hirap intindihin....
ReplyDeletehow do you calculate average response time..?
ReplyDeletePS:- I have the added the gantt chart for this example.
DeleteAverage response time=(3+6+8+11+12)/5=8 which is ignoring context switch
Definition:Average time elapsed from when process is submitted and until first output is obtained.
here all the process arrives or is submitted at 0
This comment has been removed by the author.
ReplyDeletethank you very much
ReplyDelete