欢迎来到天天文库
浏览记录
ID:41747498
大小:157.67 KB
页数:11页
时间:2019-08-31
《24李后浪实验四进程调度》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验报告实验项目名称:进程调度实验时间:2014.5.5实验室:K4・406指导老师:李芳一、实验目的进程调度是处理机管理的核心内容。木实验要求用C语言编写和调试一个简单的进程调度程序。通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间丿f轮转调度算法的具体实施办法。二、实验内容①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。②为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或伦转时间数以及进程需运行的时间片数的初始值均由用户给定。③在优先数算法中,优先数可以先取值为98,进程每执行一次,优
2、先数减3,CPU吋间片数加1,进程还需要的时间片数减1。在轮转算法屮,采用固定时间片(即:每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。④对于遇到优先数一致的情况,采用FIFO策略解决。三、实验过程程序修改而,优先数算法中,优先级为50,时间片为1,在轮转算法中,固定吋间片为2:源程序为:#include#include#include#include#include#defineP_NUM5#de
3、fineP_T1ME50enumstate{ready,execute,block,finish};structpcb{charname[4];intpriority;intcputimc;intneedtime;intcount;intround;stateprocess;pcb*next;};pcb*get_process();pcb*get_process(){pcb*q;pcb*t;pcb*p;inti=0;cout«MinputnameandtimeM«endl;while(i4、q->name;cin»q->needtime;q->cputime=();q->priority=P_TIME-q->nccdtimc;q->process=ready;q->next=NULL;if(i==0){p=q;t=q;}else{t->next=q;t=q;}i++;}//whilereturnp;}”vv“priority“vv”voiddisplay(pcb*p){cout«,,namc,,«M”vv“cputimc”vv””vv”nccdtimc”vv”"«"stateH«endl;while(p){cout«p->name;cout«n”;cout«p->5、cputime;cout«"cout«p->needtime;cout«nn;cout«p->priority;cout«"“;switch(p->process){caseready:cout«nreadyn«endl;break;caseexecute:cout«HexecuteH«endl;break;caseblock:cout«,'blockn«endl;break;casefinish:cout«Hfinishn«endl;break;}p=p->next;intprocess_finish(pcb*q){intbl=l;whilc(bl&&q){bl=bl&&q-6、>needtime==O;q=q->next;}returnbl;}voidcpuexe(pcb*q){peb*t=q;inttp=();while(q){if(q->proccss!=finish){q->process=ready;if(q->needtime==0){q->process=finish;}}if(tppriority&&q->process!=finish){tp=q->priority;t=q;}q=q->ncxt;}if(t->needtime!=O){t->priority-=3;t->needtime~;t->process=execute7、;t->cputime++;voidpriority_cal(){pcb*p;p=ge(_process();intcpu=0;while(!process_finish(p)){cpu++;cout«ncputime:H«cpu«endl;cpuexe(p);display(p);}printf(MAllprocesseshavefinished,pressanykeytoexit");getch();}voiddisplay_menu(){cout«nCHOOSETHEALGORITHM
4、q->name;cin»q->needtime;q->cputime=();q->priority=P_TIME-q->nccdtimc;q->process=ready;q->next=NULL;if(i==0){p=q;t=q;}else{t->next=q;t=q;}i++;}//whilereturnp;}”vv“priority“vv”voiddisplay(pcb*p){cout«,,namc,,«M”vv“cputimc”vv””vv”nccdtimc”vv”"«"stateH«endl;while(p){cout«p->name;cout«n”;cout«p->
5、cputime;cout«"cout«p->needtime;cout«nn;cout«p->priority;cout«"“;switch(p->process){caseready:cout«nreadyn«endl;break;caseexecute:cout«HexecuteH«endl;break;caseblock:cout«,'blockn«endl;break;casefinish:cout«Hfinishn«endl;break;}p=p->next;intprocess_finish(pcb*q){intbl=l;whilc(bl&&q){bl=bl&&q-
6、>needtime==O;q=q->next;}returnbl;}voidcpuexe(pcb*q){peb*t=q;inttp=();while(q){if(q->proccss!=finish){q->process=ready;if(q->needtime==0){q->process=finish;}}if(tppriority&&q->process!=finish){tp=q->priority;t=q;}q=q->ncxt;}if(t->needtime!=O){t->priority-=3;t->needtime~;t->process=execute
7、;t->cputime++;voidpriority_cal(){pcb*p;p=ge(_process();intcpu=0;while(!process_finish(p)){cpu++;cout«ncputime:H«cpu«endl;cpuexe(p);display(p);}printf(MAllprocesseshavefinished,pressanykeytoexit");getch();}voiddisplay_menu(){cout«nCHOOSETHEALGORITHM
此文档下载收益归作者所有