




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/ 时间片轮转调度算法 #include #include #include #include using namespace std; enum STATUS RUN,READY,WAIT,FINISH; struct PCBNode int processID; /进程ID STATUS status; /进程状态 int priorityNum; /优先数 int reqTime; /总的需要运行时间 int remainTime; /剩下需要运行时间 int arriveTime; /进入就绪队列时间 int startTime; /开始运行时间 int finishTime; /结束运行时间 int totalTime; /周转时间 float weightTotalTime; /带权周转时间 ; struct QueueNode int ID; /进程ID struct QueueNode * next; /队列中下一个进程指针 ; struct LinkQueue QueueNode *head;/队首 ; void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable); bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Round,int& currentTime,PCBNode * ProcessTable); /分配时间片给q所指进程,p为刚退出的进程 void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable); /时间片轮转调度,调用RR_Run(),时间片大小设为Round void InitialQueue(LinkQueue& Q,PCBNode * ProcessTable,const int processnum); /初始化就绪队列 void Input(PCBNode * ProcessTable, const int processnum); /从input.txt文件输入数据 int main() LinkQueue Q;/就绪队列 Q.head = NULL; const int processnum = 16;/进程数 const int Round = 1; /时间片大小 int totalTimeSum = 0; /周转时间 int WeightTotalTimeSum = 0;/带权周转时间 PCBNode * ProcessTable=new PCBNodeprocessnum; /进程表 Input(ProcessTable, processnum); InitialQueue(Q, ProcessTable, processnum); RoundRobin(Q, Round, totalTimeSum,WeightTotalTimeSum,ProcessTable); cout 时间片轮调度的平均周转时间为: totalTimeSum/processnum endl; cout 时间片轮调度的平均带权周转时间为: WeightTotalTimeSum/processnum endl; Input(ProcessTable, processnum); InitialQueue(Q, ProcessTable, processnum); Fcfs(Q, totalTimeSum,WeightTotalTimeSum,ProcessTable); cout 先来先服务的平均周转时间为: totalTimeSum/processnum endl; cout 先来先服务的平均带权周转时间为: WeightTotalTimeSum/processnum next; while (q != NULL)/从队首开始依次分配时间片 do cout * endl; cout 在时间片 (currentTime+1)/Round 内,活动进程为: ID endl; cout 进程 ID 现在需要的时间片为: ID.remainTime endl; finish = RR_Run(Q, q, p, Round, currentTime, ProcessTable);/分配时间片给q进程 cout next = NULL) r = Q.head- next; else r = q- next; else /否则计算周转时间和带权周转时间 totalTimeSum += ProcessTableq- ID.totalTime; weightTotalTimeSum += ProcessTableq- ID.weightTotalTime; delete q; /从队列中删除q进程 q = p; while (!finish & (ProcessTabler- ID.arriveTime currentTime + Round); /下一个进程很晚才来,则继续给当前进程分配时间片 p = q; q = q- next; if (q = NULL & Q.head- next!=NULL) p = Q.head; q = p- next; delete Q.head; Q.head = NULL; bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Round,int& currentTime,PCBNode * ProcessTable) if (ProcessTableq- ID.remainTime ID.finishTime = currentTime + ProcessTableq- ID.remainTime; ProcessTableq- ID.totalTime += ProcessTableq- ID.remainTime; ProcessTableq- ID.weightTotalTime = ProcessTableq- ID.totalTime/ProcessTableq- ID.reqTime; currentTime = ProcessTableq- ID.finishTime; p- next = q- next; cout endl; cout 进程 ID 完成! ID.remainTime = ProcessTableq- ID.remainTime - Round; ProcessTableq- ID.totalTime += Round; currentTime += Round; return false; void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable) totalTimeSum = 0; weightTotalTimeSum = 0;/平均周转时间 QueueNode* p; QueueNode* q; p = Q.head- next; if (p !=NULL ) ProcessTablep- ID.startTime = ProcessTablep- ID.arriveTime; ProcessTablep- ID.finishTime = ProcessTablep- ID.arriveTime + ProcessTablep- ID.reqTime; for(q=p- next; q!=NULL; q=q- next) if (ProcessTableq- ID.arriveTime ID.finishTime) ProcessTableq- ID.startTime = ProcessTablep- ID.finishTime; ProcessTableq- ID.finishTime = ProcessTablep- ID.finishTime + ProcessTableq- ID.reqTime; else/下个进程到达时间较晚 ProcessTableq- ID.startTime = ProcessTableq- ID.arriveTime; ProcessTableq- ID.finishTime = ProcessTableq- ID.arriveTime + ProcessTableq- ID.reqTime; p = q; for(q=Q.head- next; q!=NULL; q=q- next) ProcessTableq- ID.totalTime = ProcessTableq- ID.finishTime - ProcessTableq- ID.arriveTime; ProcessTableq- ID.weightTotalTime = ProcessTableq- ID.totalTime/ProcessTableq- ID.reqTime; totalTimeSum += ProcessTableq- ID.totalTime; weightTotalTimeSum += ProcessTableq- ID.weightTotalTime; int t = 0; for(q=Q.head- next; q!=NULL; q=q- next) cout * endl; while ( t ID.finishTime ) cout 时刻 t : 进程 ID 活动 next != NULL) cout 时刻 t : 进程 ID 结束活动,开始下一个进程. endl; cout 进程 ID 的周转时间为: ID.totalTime endl; cout 进程 ID 的带权周转时间为: ID.weightTotalTime endl endl; else cout 时刻 t : 进程 ID 结束活动. endl endl; cout 进程 ID 的周转时间为: ID.totalTime endl; cout 进程 ID 的带权周转时间为: ID.weightTotalTime endl endl; cout 所有进程结束活动. endl next; q!=NULL; q=q- next) delete p; p = q; void InitialQueue(LinkQueue& Q, PCBNode * ProcessTable,const int processnum) /初始化 for (int i=0;i next = NULL; QueueNode * p; QueueNode * q; for (i=0;i ID = i; p- next = NULL; if (i = 0) Q.head- next = p; else q- next = p; q = p; void Input(PCBNode * ProcessTable, const int processnum) FILE *fp; /读入线程的相关内容 if(fp=fopen( input.txt , r )=NULL) cout can not open file! endl; exit(0); for(int i=0;i processnum
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025山东济南市市属国有企业集中招聘高校毕业生771人笔试题库历年考点版附带答案详解
- 2025福建泉州市第一医院招聘编制内博士研究生学历学位工作人员42人考前自测高频考点模拟试题及完整答案详解一套
- 2025安徽马鞍山市博望区人民医院招聘派遣制工作人员8人模拟试卷及答案详解(夺冠系列)
- 2025太平人寿保险有限公司湖北分公司校园招聘5人笔试题库历年考点版附带答案详解
- 2025广东佛冈县水头镇选拔储备村(社区)“两委”后备人员考前自测高频考点模拟试题及1套完整答案详解
- 2025北京建工土木工程有限公司校园招聘43人笔试题库历年考点版附带答案详解
- 2025中国葛洲坝集团电力有限责任公司招聘成熟人才若干人笔试题库历年考点版附带答案详解
- 尘世是唯一的天堂课件
- 2025物资采购合同协议书模板
- 民政局与养老院关于五保户的协议书6篇
- 人教版数学四年级上册-第五单元《平行四边形和梯形》练习题(含答案)
- 零工市场(驿站)运营管理 投标方案(技术方案)
- 新《全面质量管理(习题集)》考试题库(含答案)
- 农村建房的邻居协议书模板
- 二年级语文《坐井观天》说课课件
- DL∕T 741-2019 架空输电线路运行规程
- 水杨酸软膏剂的制备
- 《铁皮石斛的介绍》课件
- JCT478.2-2013 建筑石灰试验方法 第2部分 化学分析方法
- 大数据、智慧城市与智慧交通(上)
- 砌砖抹灰工程劳务承包施工合同范文
评论
0/150
提交评论