轮询调度算法代码


里面是轮询调度算法的简单代码,没有其它内容,编写语言c++
资源截图
代码片段和文件信息
#include “roundrobin.h“
#include
#include
using namespace std;
roundrobin::roundrobin(void)
{
rq=w=t=NULL;
}

roundrobin::~roundrobin(void)
{
if(rq!=NULL)
{
delete[] rq;
delete[] w;
delete[] t;
delete[] a;
}
}
int roundrobin::read()//read input from the user
{
int i;
cout<<“Enter number of processes:“;
cin>>n;
if(rq!=NULL)
{
delete[] rq;
delete[] w;
delete[] t;
}
try
{
rq=new int[n];
w=new int[n];
t=new int[n];
a=new int[n];
}
catch(bad_alloc &ba)
{
cerr< exit(1);
}
cout<<“Enter arrival times:
“;
for(i=0;i {
cin>>a[i];
}
cout<<“Enter request times:
“;
for(i=0;i {
cin>>rq[i];
w[i]=t[i]=0;
}
cout<<“Enter time quantum:“;
cin>>q;
return 1;
}
void roundrobin::calc()//to calculate turn-around and wait times of all processes and the ordering
{
int j=0;
int time;
int k;
int i;
int *r;//remaining times
try
{
r=new int[n];
}
catch(bad_alloc &ba)
{
cerr< exit(1);
}
for(i=0;i bool f=false;//flag to indicate whether any process was scheduled as i changed from 0 to n-1 in the next for loop
int sp=0;//time spent
for(i=0;j {
if(r[i]>0&&sp>=a[i])//find the next uncompleted process which has already or just arrived
{
f=true;
if(r[i]<=q)//if the process requests for time less than the quantum
time=r[i];//time to be alloted in this turn is the complete requested time
else time=q;//else it is the quantum time
//schedule the process
t[i]+=timer[i]-=timeorder.push_back(i+1);
if(r[i]==0) j++;//if the process has got completed increment j
for(k=0;k if(r[k]!=0&&k!=i&&a[k] if(!(a[k]<=sp))//if they arrived while scheduling this process
w[k]+=sp+time-a[k]t[i]+=sp+time-a[k];//account for the time they spent waiting while the process was being scheduled
else
w[k]+=timet[k]+=time;//add time to their wait times and turn-around times
sp+=time;
continue;
}
if(i==n-1)
{
if(!f)
//now there are no more arrived processes to be scheduled
//so change sp to the arrival time of next arriving process
{
int it;
int diff=0;//diff between present time spent and arrivaltime of next arriving process
for(it=0;it if(sp {
if(diff==0) diff=a[it]-sp;
else if(diff>a[it]-sp) diff=a[it]-sp;
}
sp+=diff;
}
f=false;
}
}
delete[] r;
}
void roundrobin::display()
{
int i;
float tav=0;//average turn-around time
float wav=0;//average wait time
for(i=0;i tav+=t[i]wav+=w[i];
tav/=nwav/=n;
cout<<“Scheduling order:
“;
list::iterator oi;
for(oi=order.begin();oi!=order.end();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         204  2007-02-10 08:46  RR.cpp
     文件        3117  2007-02-10 11:46  roundrobin.cpp
     文件         496  2007-02-10 08:46  roundrobin.h

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)