实验3高(动态)优先权优先的进程调度算法模拟

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

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

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

资源描述

实验3高(动态)优先权优先的进程调度算法模拟1.实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。2.实验环境装有操作系统WindowsXP和开发工具VC++6.0,内存在256M以上的微机;或者:装有Linux(Fedora7)操作系统和gcc编译器,内存在256M以上的微机。3.实验内容(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:进程标识数ID;进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;进程已占用的CPU时间CPUTIME;进程还需占用的CPU时间NEEDTIME。当进程运行完毕时,NEEDTIME变为0;进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;进程状态STATE;(READY,RUNNING,BLOCK,FINISH)队列指针NEXT,用来将PCB排成队列。(3)优先数改变的原则:进程在就绪队列中呆一个时间片,优先数增加1;进程每运行一个时间片,优先数减3。(4)假设在调度前,系统中有5个进程,它们的初始状态如下:ID01234PRIORITY93830290CPUTIME00000NEEDTIME33634STARTBLOCK2-1-1-1-1BLOCKTIME30000STATEREADYREADYREADYREADYREADY(5)为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:RUNNINGPROCESS:$id0READYQUEUE:$id1-$id2BLOCKQUEUE:$id3-$id4FINISHQUEUE:$id0-$id1-$id2-$id3-$id4==================================================================IDPRIORITYCPUTIMENEEDTIMESTATESTARTBLOCKBLOCKTIME0XXXXXXXXXXXX1XXXXXXXXXXXX2XXXXXXXXXXXX3XXXXXXXXXXXX4XXXXXXXXXXXX====================================================================4.实验要求(1)将源程序(priority.c)和程序运行结果写入实验报告。(2)将该算法执行过程与高响应比优先调度算法的执行过程进行比较。#includestdio.h#includeiostreamusingnamespacestd;inti;//循环值intj;//还在阻塞或就绪队列中的进程数ints;intm;//最大priority的idstructpcb{intid;intp;//priorityintcputime;intalltime;intstartblock;intblocktime;intstate;//0表示ready1表示end-1表示block};structpcbpro[5]={{0,9,0,3,2,3,0},{1,38,0,3,-1,0,0},{2,30,0,6,-1,0,0},{3,29,0,3,-1,0,0},{4,0,0,4,-1,0,0}};intchangestate0(){if(pro[0].startblock==0){pro[0].state=-1;pro[0].startblock--;return1;}if(pro[0].blocktime==0){pro[0].state=0;return1;}if(pro[0].state==0&&pro[0].startblock!=-1){pro[0].startblock--;return1;}if(pro[0].state==-1&&pro[0].blocktime!=0){pro[0].blocktime--;return1;}}intstate0(){changestate0();s=pro[0].p;if(pro[0].state==-1)s=-100;returns;}intmaxp()//求出最大priority{state0();intmax=s;m=pro[0].id;for(i=0;ij;i++){if(pro[i+1].ppro[i].p){max=pro[i+1].p;m=pro[i+1].id;}}returnm;}voidchange(){maxp();intx;//得到m现在的数组编号for(i=0;ij;i++){pro[i].p++;}for(i=0;ij;i++){if(pro[i].id==m)x=i;}pro[x].cputime++;pro[x].p=pro[x].p-4;pro[x].alltime--;if(pro[x].alltime==0){pro[x].state=1;}}voiddisplay(){change();coutRUNNINGPROG:mendl;cout===============================================================\n;coutID;for(i=0;ij;i++){cout.width(10);coutpro[i].id;}coutendlPRIORITY;for(i=0;ij;i++){cout.width(10);coutpro[i].p;}coutendlCPUTIME;for(i=0;ij;i++){cout.width(10);coutpro[i].cputime;}coutendlALLTIME;for(i=0;ij;i++){cout.width(10);coutpro[i].alltime;}coutendlSTARTBLOCK;for(i=0;ij;i++){cout.width(10);coutpro[i].startblock;}coutendlBLOCKTIME;for(i=0;ij;i++){cout.width(10);coutpro[i].blocktime;}coutendlSTATE;for(i=0;ij;i++){cout.width(10);coutpro[i].state;}coutendl;}intmain(){j=5;//刚开始有5个进程while(j!=0){for(i=0;ij;i++){if(pro[i].state==1){for(;ij;i++){pro[i]=pro[i+1];}j=j-1;}}display();getchar();}}运行结果:RUNNINGPROG:1===============================================================ID01234PRIORITY103531301CPUTIME01000ALLTIME32634STARTBLOCK1-1-1-1-1BLOCKTIME30000STATE00000RUNNINGPROG:1===============================================================ID01234PRIORITY113232312CPUTIME02000ALLTIME31634STARTBLOCK0-1-1-1-1BLOCKTIME30000STATE00000RUNNINGPROG:1===============================================================ID01234PRIORITY122933323CPUTIME03000ALLTIME30634STARTBLOCK-1-1-1-1-1BLOCKTIME30000STATE-11000RUNNINGPROG:2===============================================================ID0234PRIORITY1330334CPUTIME0100ALLTIME3534STARTBLOCK-1-1-1-1BLOCKTIME2000STATE-1000RUNNINGPROG:3===============================================================ID0234PRIORITY1431305CPUTIME0110ALLTIME3524STARTBLOCK-1-1-1-1BLOCKTIME1000STATE-1000RUNNINGPROG:2===============================================================ID0234PRIORITY1528316CPUTIME0210ALLTIME3424STARTBLOCK-1-1-1-1BLOCKTIME0000STATE-1000RUNNINGPROG:3===============================================================ID0234PRIORITY1629287CPUTIME0220ALLTIME3414STARTBLOCK-1-1-1-1BLOCKTIME0000STATE0000RUNNINGPROG:2===============================================================ID0234PRIORITY1726298CPUTIME0320ALLTIME3314STARTBLOCK-1-1-1-1BLOCKTIME0000STATE0000RUNNINGPROG:3===============================================================ID0234PRIORITY1827269CPUTIME0330ALLTIME3304STARTBLOCK-1-1-1-1BLOCKTIME0000STATE0010RUNNINGPROG:2===============================================================ID024PRIORITY192410CPUTIME040ALLTIME324STARTBLOCK-1-1-1BLOCKTIME000STATE000RUNNINGPROG:2===============================================================ID024PRIORITY202111CPUTIME050ALLTIME314STARTBLOCK-1-1-1BLOCKTIME000STATE000RUNNINGPROG:2===============================================================ID024PRIORITY211812CPUTIME060ALLTIME304STARTBLOCK-1-1-1BLOCKTIME000STATE010RUNNINGPROG:0===============================================================ID04PRIORITY1813CPUTIME10ALLTIME24STARTBLOCK-1-1BLOCKTIME00ST

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

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

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

×
保存成功