📄 roundr.cpp
字号:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n,i,j,k,l,temp1,temp2,tq,flag,c_p_id;
int p[10][2],wt[11];
int cq[20],cq_1[20];
int first=0,last=0,count,x,Twt,z;
float Awt;
cout<<"\n\t\tRound Robin Scheduling Algorithm";
cout<<"\n\tEnter time quantum :: ";
cin>>tq;
cout<<"\n\tEnter the no. of processes :: ";
cin>>n;
for(i=0;i<11;i++)
{
wt[i]=0;
}
for(i=0;i<n;i++)
{
p[i][1]=i+1;
}
for(i=0;i<n;i++)
{
cout<<"\n\tEnter burst time for process "<<i+1<<" :: ";
cin>>p[i][0];
}
for(j=0;j<(n-1);j++)
{
for(k=0;k<n-1;k++)
{
if(p[k][0]>p[k+1][0])
{
temp1=p[k][0];
temp2=p[k][1];
p[k][0]=p[k+1][0];
p[k][1]=p[k+1][1];
p[k+1][0]=temp1;
p[k+1][1]=temp2;
}
}
}
l=0;
for(;;)
{
flag=0;
for(i=0;i<n;i++)
if(p[i][0]>0)
{
flag=1;
break;
}
if(flag==1)
{
if(p[l][0]>0)
{
cq[last]=p[l][1];
if(p[l][0]>=tq)
cq_1[last]=tq;
else
cq_1[last]=p[l][0];
count=0;
for(x=last-1;x>=0;x--)
{
if(cq[x]==cq[last])
break;
else
wt[cq[last]]+=cq_1[x];
}
last=(last+1)%20;
p[l][0]-=tq;
l=(l+1)%n;
}
else
{
while(p[l][0]<=0)
l=(l+1)%n;
cq[last]=p[l][1];
if(p[l][0]>=tq)
cq_1[last]=tq;
else
cq_1[last]=p[l][0];
count=0;
for(x=last-1;x>=0;x--)
{
if(cq[x]==cq[last])
break;
else
wt[cq[last]]+=cq_1[x];
}
last=(last+1)%20;
p[l][0]-=tq;
l=(l+1)%n;
}
}
else
{
cout<<"\nEnd";
break;
}
c_p_id=cq[first];
first=(first+1)%20;
cout<<"\nExecuting :: "<<c_p_id;
}
cout<<"\n\n\t\tWait Array";
Twt=0;
for(i=1;i<=n;i++)
{
cout<<"\nTotal wait period for process "<<i<<" :: "<<wt[i];
Twt+=wt[i];
}
Awt=Twt/float(n);
cout<<"\n\n\t\tAverage waiting time :: "<<Awt;
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -