时间片轮转调度算法.doc_第1页
时间片轮转调度算法.doc_第2页
时间片轮转调度算法.doc_第3页
时间片轮转调度算法.doc_第4页
时间片轮转调度算法.doc_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论