实验四进程调度1.实验目的通过本次实验加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间片轮转调度算法的具体实施办法。2.实验要求进程是处理机管理的核心内容。本实验要求用c语言编写和天使一个简单上的进程控制调度程序。3.实验内容(1)、设计进程控制块pcb表结构(与实验一得结构相同),分别使用与优先数调度算法和循环轮转调度算法。(2)、建立进程就绪队列。对联中不同算法编制入链子程序。(3)、编制两种进程调度算法:1。优先数调度;2。循环轮转调度4.源代码#includestdio.h#includedos.h#includestdlib.h#includewindows.h#includeconio.h#includeiostream.h#defineP_NUM5#defineP_TIME50enumstate{ready,execute,block,finish,};structpcb{charname[4];intpriority;intcputime;intneedtime;intcount;intround;stateprocess;pcb*next;};pcb*get_process();pcb*get_process(){pcb*q;pcb*t;pcb*p;inti=0;coutinputnameandtimeendl;while(iP_NUM){q=(structpcb*)malloc(sizeof(pcb));cinq-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++;}returnp;}voiddisplay(pcb*p){coutnamecputimeneedtimeprioritystateendl;while(p){coutp-name;cout;coutp-cputime;cout;coutp-needtime;cout;coutp-priority;cout;switch(p-process){caseready:coutreadyendl;break;caseexecute:coutexecuteendl;break;caseblock:coutblockendl;break;casefinish:coutfinishendl;break;}p=p-next;}}intprocess_finish(pcb*q){intb1=1;while(b1&&q){b1=b1&&q-needtime==0;q=q-next;}returnb1;}voidcpuexe(pcb*q){pcb*t=q;inttp=0;while(q){if(q-process!=finish){q-process=ready;if(q-needtime==0){q-process=finish;}}if(tpq-priority&&q-process!=finish){tp=q-priority;t=q;}q=q-next;}if(t-needtime!=0){t-priority-=3;t-needtime--;t-process=execute;t-cputime++;}}voidpriority_cal(){pcb*p;//voidclrscr();p=get_process();intcpu=0;system(cls);while(!process_finish(p)){cpu++;coutcputime:cpuendl;cpuexe(p);display(p);Sleep(20);system(cls);}printf(Allprocesshavefinished,pressmanykeytoexit);getch();}voiddisplay_menu(){coutCHOOSETHEALGORITHM:endl;cout1PRIORITYendl;cout2ROUNDROBINendl;cout3EXITendl;}pcb*get_process_round(){pcb*q;pcb*p;pcb*t;inti=0;coutinputnameandtimeendl;while(iP_NUM){q=(structpcb*)malloc(sizeof(pcb));cinq-name;cinq-needtime;q-cputime=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++;}//whilereturnp;}voidcpu_round(pcb*q){q-cputime+=2;q-needtime-=2;if(q-needtime0){q-needtime=0;}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;}returnt;}voidset_state(pcb*p){while(p){if(p-needtime==0){p-process=finish;}if(p-process==execute){p-process=ready;}p=p-next;}}voiddisplay_round(pcb*p){coutNAMECPUTIMENEEDTIMECOUNTROUNDSTATEendl;while(p){coutp-name;cout;coutp-cputime;cout;coutp-needtime;cout;coutp-count;cout;coutp-round;cout;switch(p-process){caseready:coutreadyendl;break;caseexecute:coutexecuteendl;break;casefinish:coutfinishendl;break;}p=p-next;}}voidround_cal(){pcb*p;pcb*r;system(cls);p=get_process_round();intcpu=0;system(cls);r=p;while(!process_finish(p)){cpu+=2;cpu_round(r);r=get_next(r,p);coutcpucpuendl;display_round(p);set_state(p);Sleep(20);system(cls);}}voidmain(){display_menu();intk;scanf(%d,&k);switch(k){case1:priority_cal();break;case2:round_cal();break;case3:break;display_menu();scanf(%d,&k);}}5.运行结果优先数算法运行结果如下时间片轮转算法运行结果如下