c++实现进程调度

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeiostream#includestring#includeconio.h#includewindows.h#includeiomanip#includestdlib.husingnamespacestd;classPCB_Control;//进程控制类classPCB//进程控制块结点{public:friendclassPCB_Control;PCB*Next;intNo;intPriority;intNeed_Time;intUsed_Time;charStation;intTime_Priority;PCB(PCB*ptrnext=NULL)//构造头结点{Next=ptrnext;}PCB(intno1,intpri1,intneed_time1,intused_time1,charstation1,inttimepriority,PCB*ptrnext=NULL)//构造普通结点{No=no1;Priority=pri1;Need_Time=need_time1;Used_Time=used_time1;Station=station1;Time_Priority=timepriority;Next=ptrnext;}~PCB(void){}};classPCB_Control{private:PCB*head;intsize;inttime_priority;//时间优先级charini_station;//初始状态intused_time;public:PCB_Control(void)//构造函数{head=newPCB;size=0;time_priority=1000;}voidIni_Insert(intno,intpriority,intneed_time)//对输入的进程进行初始化{used_time=0;ini_station='W';PCB*newnode=newPCB(no,priority,need_time,used_time,ini_station,time_priority,NULL);Insert_And_Sort(newnode);time_priority--;}voidInsert_And_Sort(PCB*ins)//插入函数,插入时并按照优先级和时间进行排序{if(size==0){ins-Next=NULL;head-Next=ins;size++;}else{PCB*front=head;for(intm=0;msize;m++)//按最高优先级调度算法插入,同等优先级下按先来先服务算法插入{if(ins-Priority!=Index(m)-Priority)//最高优先级调度{if(ins-PriorityIndex(m)-Priority){ins-Next=Index(m);front-Next=ins;break;}if(ins-PriorityIndex(m)-Priority){if(Index(m)-Next==NULL){Index(m)-Next=ins;ins-Next=NULL;break;}else{front=front-Next;continue;}}}else//同优先级下按先来先服务算法{if(ins-Time_PriorityIndex(m)-Time_Priority){if(Index(m)-Next==NULL){ins-Next=NULL;Index(m)-Next=ins;break;}else{front=front-Next;continue;}}else{ins-Next=front-Next;front-Next=ins;break;}}}size++;}}PCB*Index(inti)//定位函数{if(i-1||isize-1){cout;exit(0);}if(i==-1)returnhead;PCB*p=head-Next;intk=0;while(p!=NULL&&ki){p=p-Next;k++;}returnp;}voidDisplay()//进程调度函数{intflag=1;while(flag==1){Sleep(1000);PCB*p=newPCB;p=Index(0);p-Station='R';Showall();//显示当前状态p-Need_Time--;p-Used_Time++;p-Priority--;p-Station='W';if(p-Need_Time0){Delete();Insert_And_Sort(p);continue;}if(p-Need_Time==0){Delete();cout进程p-No完成,退出!endl;if(size==0){flag=0;coutendl进程执行完!endl;break;}}}}voidDelete()//从就绪队列中删除一个进程{head-Next=head-Next-Next;size--;}voidShowone(PCB*p)//显示单个进程的信息{intp_no=p-No;intp_need_time=p-Need_Time;intp_used_time=p-Used_Time;intp_priority=p-Priority;charp_station=p-Station;cout进程p_no优先级:p_priority需要时间p_need_time已用时间:p_used_time状态:p_stationendl;}voidShowall()//显示就绪队列全部进程的信息{coutendl;coutendl;for(inta=0;asize;a++){PCB*p=Index(a);Showone(p);}coutendl;}};voidmain(){PCB_ControlNewPCB;coutendl******************************************************************************endl|进程调度|endl;coutstd::leftsetw(10)||endl;cout******************************************************************************endl;coutPoweredby:王力Time:2012//09//12endl;cout******************************************************************************endl;cout请输入进程的个数:;intk;cink;for(inti=0;ik;i++){cout请填写下面的信息:endl;cout进程i+1:endl;intpriority;cout优先级:;cinpriority;intneed_time;cout需要时间:;cinneed_time;NewPCB.Ini_Insert(i+1,priority,need_time);coutendl;}cout进程调度情况如下:endl;NewPCB.Display();coutendl;cout输入任意字符结束:endl;getche();}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功