




已阅读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年自然科学硕士生入学考试试题及答案
- 上海工艺美术职业学院《基础医学概要》2023-2024学年第二学期期末试卷
- 2025年医学检验师考试试卷及答案对比
- 应用统计入门2025年考试试题及答案
- 山西省朔州市怀仁市第一中学2025年高三年级英语试题二模试题含解析
- 2025年职业技能鉴定考试试卷及答案
- 辽宁省丹东市凤城市白旗中学2025年初三期末调研测试物理试题含解析
- 山西省怀仁一中2025届高三.十三校联考.第一次考试英语试题试卷含解析
- 工业废水处理药剂租赁及环保监管服务合同
- 工业级材料扭转试验机租赁与设备定期检修合同
- 强基计划语文试题及答案
- 2025至2030中国玻尿酸市场前景预判及未来消费规模调研报告
- 2025-2030中国船舶行业发展分析及发展前景与投资研究报告
- 耐药菌耐药性监测策略-全面剖析
- 2025年中国陈皮市场调查研究报告
- 2024年农艺师考试考试形式试题及答案
- 老年综合评估技术应用中国专家共识解读
- 北京市通州区2025年初中学业水平模拟考试(一模)英语试卷(含答案)
- 手术中大出血抢救流程
- 初中语文第23课《“蛟龙”探海》课件-2024-2025学年统编版语文七年级下册
- 2025重庆武工工业技术研究院有限公司招聘15人笔试参考题库附带答案详解
评论
0/150
提交评论