《2022年操作系统进程管理程序 2.pdf》由会员分享,可在线阅读,更多相关《2022年操作系统进程管理程序 2.pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include #include #include #include #include #define P_NUM 5 #define P_TIME 50 enum state ready, execute, block, finish ; struct pcb char name4; int priority; int cputime; int needtime; int count; int round; state process; pcb * next; ; pcb * get_process(); pcb * get_process() pcb *q; pcb *t; pcb *p
2、; int i=0; coutinput name and timeendl; while (iq-name; cinq-needtime; q-cputime=0; q-priority=P_TIME-q-needtime; q-process=ready; q-next=NULL; if (i=0) p=q; t=q; else t-next=q; t=q; i+; /while return p; void display(pcb *p) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
3、 - 第 1 页,共 5 页 - - - - - - - - - coutname cputime needtime priority stateendl; while(p) coutname; cout ; coutcputime; cout ; coutneedtime; cout ; coutpriority; coutprocess) case ready:coutreadyendl;break; case execute:coutexecuteendl;break; case block:coutblockendl;break; case finish:coutfinishnext;
4、 int process_finish(pcb *q) int bl=1; while(bl&q) bl=bl&q-needtime=0; q=q-next; return bl; void cpuexe(pcb *q) pcb *t=q; int tp=0; while(q) if (q-process!=finish) q-process=ready; if(q-needtime=0) q-process=finish; if(tppriority&q-process!=finish) tp=q-priority; t=q; q=q-next; if(t-needtime!=0) t-pr
5、iority-=3; t-needtime-; t-process=execute; t-cputime+; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - void priority_cal() pcb * p; p=get_process(); int cpu=0; while(!process_finish(p) cpu+; coutcputime:cpuendl; cpuexe(p); display(p); print
6、f(All processes have finished,press any key to exit); getch(); void display_menu() coutCHOOSE THE ALGORITHM:endl; cout1 PRIORITYendl; cout2 ROUNDROBINendl; cout3 EXITendl; pcb * get_process_round() pcb *q; pcb *t; pcb *p; int i=0; coutinput name and timeendl; while (iq-name; cinq-needtime; q-cputime
7、=0; q-round=0; q-count=0; q-process=ready; q-next=NULL; if (i=0) p=q; t=q; else t-next=q; t=q; i+; /while return p; void cpu_round(pcb *q) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - q-cputime+=2; q-needtime-=2; if(q-needtimeneedtime=0;
8、 q-count+; q-round+; q-process=execute; pcb * get_next(pcb * k,pcb * head) pcb * t; t=k; do t=t-next; while (t & t-process=finish); if(t=NULL) t=head; while (t-next!=k & t-process=finish) t=t-next; return t; void set_state(pcb *p) while(p) if (p-needtime=0) p-process=finish; if (p-process=execute) p
9、-process=ready; p=p-next; void display_round(pcb *p) coutNAME CPUTIME NEEDTIME COUNT ROUND STATEendl; while(p) coutname; cout ; coutcputime; cout ; coutneedtime; cout ; coutcount; cout ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - coutr
10、ound; coutprocess) case ready:coutreadyendl;break; case execute:coutexecuteendl;break; case finish:coutfinishnext; void round_cal() pcb * p; pcb * r; p=get_process_round(); int cpu=0; r=p; while(!process_finish(p) cpu+=2; cpu_round(r); r=get_next(r,p); coutcpu cpuendl; display_round(p); set_state(p); void main() display_menu(); int k; scanf(%d,&k); switch(k) case 1:priority_cal();break; case 2:round_cal();break; case 3:break; display_menu(); scanf(%d,&k); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -