您的位置:首页 > 其它

轮询算法 这是一个印度人写的,学习下。 来自 codeproject

2011-04-10 14:24 78 查看
#pragma once
#include<list>
using namespace std;

class roundrobin//class representing round robin scheduling
{
int *rq;//request times
int     n;//number of processes
int     q;//time quantum
int     *w;//wait times
int     *t;//turn-around times
int *a;//arrival times
list<int> order;
public:
roundrobin(void);
~roundrobin(void);
int read();//read input from the user
void calc();//to calculate turn-around and wait times of all processes and the ordering
void display();
};

[code]#include "roundrobin.h"
#include<iomanip>
#include<iostream>
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
;
w=new int
;
t=new int
;
a=new int
;
}
catch(bad_alloc &ba)
{
cerr<<ba.what()<<endl;
exit(1);
}
cout<<"Enter arrival times:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter request times:\n";
for(i=0;i<n;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
;
}
catch(bad_alloc &ba)
{
cerr<<ba.what()<<endl;
exit(1);
}
for(i=0;i<n;i++)        r[i]=rq[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<n;i=(i+1)%n)//while there are uncompleted processes
{
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]+=time,r[i]-=time,order.push_back(i+1);
if(r[i]==0)     j++;//if the process has got completed, increment j
for(k=0;k<n;k++)
if(r[k]!=0&&k!=i&&a[k]<sp+time)//for all other arrived processes incompleted after scheduling this process
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]+=time,t[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<n;it++)
if(sp<a[it])//if process has'nt yet arrived
{
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<n;i++)
tav+=t[i],wav+=w[i];
tav/=n,wav/=n;
cout<<"Scheduling order:\n";
list<int>::iterator oi;
for(oi=order.begin();oi!=order.end();oi++)
cout<<*oi<<"\t";
cout<<"\nAverage turn-around time = "<<tav<<endl<<"Average wait time = "<<wav<<endl;
}


#include"roundrobin.h"
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
roundrobin r;
r.read();
r.calc();
r.display();
cout<<"Press any key to exit...";
_getch();
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: