All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Tuesday, 26 August 2014

Round Robin Algorithm

#include<iostream>
using namespace std;
int main()
{
int n,i,ts,rp,t;
int twt=0,tat=0,f=0;
int bt[i],rt[i];
cout<<"Enter No of Process :";
cin>>n;
rp=n;
for(i=0;i<n;i++)
{
cout<<"Enter Burst time for Process"<<i<<": ";
cin>>bt[i];
rt[i]=bt[i];
}
cout<<"Enter Time Slice: ";
cin>>ts;
cout<<"\nP_NO\tB_Time\tW_time\tT_Time\n";
for(t=0,i=0;rp!=0;)
{
if(rt[i]>ts)
{
rt[i]-=ts;
t+=ts;
}
else if(rt[i]<=ts&&rt[i]>0)
{
t+=rt[i];
rt[i]=0;
f=1;
}

if(rt[i]==0&&f==1)
{
rp--;
cout<<"P"<<i<<"\t"<<bt[i]<<"\t"<<t-bt[i]<<"\t"<<t<<endl;
twt+=t-bt[i];

tat+=t;
f=0;
}
if(i==n-1)
i=0;
else
i++;
}
cout<<"\nTotal Waiting Time="<<twt<<"\t\t";
cout<<"Average Waiting time="<<twt/(float)n;
cout<<"\nTotal Turnaround Time="<<tat<<"\t";
cout<<"Average Turnaround Time="<<tat/(float)n;
return 0;
}

No comments:

Post a Comment