轮询算法 这是一个印度人写的,学习下。 来自 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]
相关文章推荐
- C语言主要做哪些方面的开发---一个来自“IT技术学习”微信群的问题及答复
- 来自一个小白学习PHP的学习路径
- C语言主要做哪些方面的开发---一个来自“IT技术学习”微信群的问题及答复
- Delphi中,indy控件实现收发邮件的几点学习记录( 可以考虑加入多线程,用多个邮箱做一个邮箱群发器) 转
- 学习一个通用数据库访问类
- d3.js学习4----做一个简单的图表
- python3 Queue(一个同步队列类)学习
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- 一个游戏程序员的学习资料
- 推荐一个学习深度学习的好网站
- 字串处理 expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
- 通过一个实际的例子学习Oracle存储过程
- 算法学习--4 设置一个有getMin功能的栈之栈的升级版(待修改)
- 学习扎记:一个最简单的Servlet――Hello World
- Core Java 8 学习笔记(四) 打印一个简单的日历
- 【C/C++学习笔记】判断一个点是否在多边形内部的例子
- ASP.NET MVC5的一个轻量级的框架学习的第一天
- 一个职业中专的学习计算机应用专业的准备对口招生考试的学生对2017的总结
- 学习Silverlight时练手的一个网站程序代码
- 一步一个脚印学习WCF系列之WCF概要—WCF与SOA(二)