资源描述:
《操作系统课程设计进程调度模拟源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//experiment1.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdio.h"#include"stdlib.h"#include"time.h"typedefstructPCB{intname;intruntime;intrunedtime;//intstate;intkilltime;intwaitpoint;intwaittime;structPCB*next;}PCB;#defineNUM10intmain(intar
2、gc,char*argv[]){intn=0;inttimeslice=3;PCB*runqueue=NULL;//运行队列PCB*top=NULL,*tail=NULL,*temp;//就绪队列PCB*top_wait=NULL,*tail_wait=NULL,*p;inti;srand(10);//给定随机数种子srand((int)time(NULL));for(i=0;iname=i;temp->runtime=rand()%20+1;
3、temp->runedtime=0;temp->next=NULL;temp->killtime=0;temp->waitpoint=rand()%temp->runtime+1;temp->waittime=rand()%20+1;if(i==0){top=temp;tail=temp;}else{tail->next=temp;tail=temp;}printf("createname%d,runtime=%d,runedtime=%d,killtime=%d,waitpoint=%d,waittime=%
4、d",tail->name,tail->runtime,tail->runedtime,tail->killtime,tail->waitpoint,tail->waittime);}while(top!=NULL
5、
6、top_wait!=NULL){if(top==NULL)gotoflag;runqueue=top;top=top->next;runqueue->next=NULL;//从就绪队列选一个节点,插入运行队列runqueue->killtime=timeslice;//计算消耗的时间runqu
7、eue->runedtime=runqueue->runedtime+runqueue->killtime;//计算总共运行时间runqueue->runtime=runqueue->runtime-timeslice;//计算剩余运行时间if((runqueue->runedtime-runqueue->waitpoint>=0)&&(runqueue->runedtime-runqueue->waitpoint8、unqueue->runtime=runqueue->runtime+runqueue->runedtime-runqueue->waitpoint;runqueue->killtime=timeslice-(runqueue->runedtime-runqueue->waitpoint);runqueue->runedtime=runqueue->waitpoint;if(top_wait==NULL){//判断阻塞队列是否为空top_wait=runqueue;tail_wait=top_wait;//插入
9、阻塞队列}else{tail_wait->next=runqueue;tail_wait=tail_wait->next;//插入阻塞队列}printf("runtowaitname%d,runtime=%d,runedtime=%d,killtime=%d",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);}elseif(runqueue->runtime<=0){//判断是否结束runqueue->kill
10、time=0;runqueue->runtime=0;printf("killname%d,runtime=%d,runedtime=%d,killtime=%d",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);n++;delete(runqueue);}//如果是,删除节点else