1、实验3高动态优先权优先的进程调度算法模拟实验3 高(动态)优先权优先的进程调度算法模拟实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。实验环境装有操作系统Windows XP和开发工具VC+6.0,内存在256M以上的微机;或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。实验内容用C语言来实现对N个进程采用动态优先权优先算法的进程调度。每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:进程标识数ID;进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;进程已占用的CPU时间CPUTIME;进程还需占用的CP
2、U时间NEEDTIME。当进程运行完毕时,NEEDTIME变为0;进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;进程状态STATE;(READY, RUNNING, BLOCK, FINISH)队列指针NEXT,用来将PCB排成队列。优先数改变的原则:进程在就绪队列中呆一个时间片,优先数增加1;进程每运行一个时间片,优先数减3。假设在调度前,系统中有5个进程,它们的初始状态如下: ID 0 1 2 3 4 PRIORITY 9
3、38 30 29 0 CPUTIME 0 0 0 0 0 NEEDTIME 3 3 6 3 4 STARTBLOCK 2 -1 -1 -1 -1 BLOCKTIME 3 0 0 0 0 STATE READY READY READY READY READY为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下: RUNNING PROCESS: $id0 READY QUEUE: $id1-$id2 BLOCK QUEUE: $id3-$id4 FINISH QUEUE: $id0-$id1-$id2-$id3-$id4 = ID PRIORITY CPUT
4、IME NEEDTIME STATE STARTBLOCK BLOCKTIME0 XX XX XX XX XX XX1 XX XX XX XX XX XX2 XX XX XX XX XX XX3 XX XX XX XX XX XX4 XX XX XX XX XX XX =实验要求将源程序(priority.c)和程序运行结果写入实验报告。将该算法执行过程与高响应比优先调度算法的执行过程进行比较。#include #include using namespace std; int i; /循环值 int j; /还在阻塞或就绪队列中的进程数 int s; int m; /最大priority的i
5、d struct pcb int id; int p; /priority int cputime; int alltime; int startblock; int blocktime; int state; /0 表示ready 1表示end -1表示block ; struct pcb pro5= 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 ; int changestate0() if(pro0.startblock=0) pro0.state=-1; pro0.sta
6、rtblock-; return 1; if(pro0.blocktime=0) pro0.state=0; return 1; if(pro0.state=0&pro0.startblock!=-1) pro0.startblock-;return 1; if(pro0.state=-1&pro0.blocktime!=0) pro0.blocktime-;return 1; int state0() changestate0(); s=pro0.p; if(pro0.state=-1) s=-100; return s; int maxp()/求出最大priority state0();
7、int max=s; m=pro0.id; for(i=0;iproi.p) max=proi+1.p; m=proi+1.id; return m; void change() maxp(); int x;/得到m现在的数组编号 for(i=0;ij;i+) proi.p+; for(i=0;ij;i+) if(proi.id=m) x=i; prox.cputime+; prox.p=prox.p-4; prox.alltime-; if(prox.alltime=0) prox.state=1; void display() change(); coutRUNNING PROG:mend
8、l; cout=n; coutID ; for(i=0;ij;i+) cout.width(10); coutproi.id; coutendlPRIORITY ; for(i=0;ij;i+) cout.width(10); coutproi.p; coutendlCPUTIME ; for(i=0;ij;i+) cout.width(10); coutproi.cputime; coutendlALLTIME ; for(i=0;ij;i+) cout.width(10); coutproi.alltime; coutendlSTARTBLOCK; for(i=0;ij;i+) cout.
9、width(10); coutproi.startblock; coutendlBLOCKTIME ; for(i=0;ij;i+) cout.width(10); coutproi.blocktime; coutendlSTATE ; for(i=0;ij;i+) cout.width(10); coutproi.state; coutendl; int main() j=5;/刚开始有5个进程 while(j!=0) for(i=0;ij;i+) if(proi.state=1) for(;ij;i+) proi=proi+1; j=j-1; display(); getchar(); 运
10、行结果:RUNNING PROG:1=ID 0 1 2 3 4PRIORITY 10 35 31 30 1CPUTIME 0 1 0 0 0ALLTIME 3 2 6 3 4STARTBLOCK 1 -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE 0 0 0 0 0RUNNING PROG:1=ID 0 1 2 3 4PRIORITY 11 32 32 31 2CPUTIME 0 2 0 0 0ALLTIME 3 1 6 3 4STARTBLOCK 0 -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE 0 0 0 0 0RUNNING PROG:1
11、=ID 0 1 2 3 4PRIORITY 12 29 33 32 3CPUTIME 0 3 0 0 0ALLTIME 3 0 6 3 4STARTBLOCK -1 -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE -1 1 0 0 0RUNNING PROG:2=ID 0 2 3 4PRIORITY 13 30 33 4CPUTIME 0 1 0 0ALLTIME 3 5 3 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 2 0 0 0STATE -1 0 0 0RUNNING PROG:3=ID 0 2 3 4PRIORITY 14 31 30 5
12、CPUTIME 0 1 1 0ALLTIME 3 5 2 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 1 0 0 0STATE -1 0 0 0RUNNING PROG:2=ID 0 2 3 4PRIORITY 15 28 31 6CPUTIME 0 2 1 0ALLTIME 3 4 2 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 0 0 0 0STATE -1 0 0 0RUNNING PROG:3=ID 0 2 3 4PRIORITY 16 29 28 7CPUTIME 0 2 2 0ALLTIME 3 4 1 4STARTBLOCK -1 -1
13、 -1 -1BLOCKTIME 0 0 0 0STATE 0 0 0 0RUNNING PROG:2=ID 0 2 3 4PRIORITY 17 26 29 8CPUTIME 0 3 2 0ALLTIME 3 3 1 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 0 0 0 0STATE 0 0 0 0RUNNING PROG:3=ID 0 2 3 4PRIORITY 18 27 26 9CPUTIME 0 3 3 0ALLTIME 3 3 0 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 0 0 0 0STATE 0 0 1 0RUNNING PROG
14、:2=ID 0 2 4PRIORITY 19 24 10CPUTIME 0 4 0ALLTIME 3 2 4STARTBLOCK -1 -1 -1BLOCKTIME 0 0 0STATE 0 0 0RUNNING PROG:2=ID 0 2 4PRIORITY 20 21 11CPUTIME 0 5 0ALLTIME 3 1 4STARTBLOCK -1 -1 -1BLOCKTIME 0 0 0STATE 0 0 0RUNNING PROG:2=ID 0 2 4PRIORITY 21 18 12CPUTIME 0 6 0ALLTIME 3 0 4STARTBLOCK -1 -1 -1BLOCK
15、TIME 0 0 0STATE 0 1 0RUNNING PROG:0=ID 0 4PRIORITY 18 13CPUTIME 1 0ALLTIME 2 4STARTBLOCK -1 -1BLOCKTIME 0 0STATE 0 0RUNNING PROG:0=ID 0 4PRIORITY 15 14CPUTIME 2 0ALLTIME 1 4STARTBLOCK -1 -1BLOCKTIME 0 0STATE 0 0RUNNING PROG:0=ID 0 4PRIORITY 12 15CPUTIME 3 0ALLTIME 0 4STARTBLOCK -1 -1BLOCKTIME 0 0STATE 1 0RUNNING PROG:4=ID 4PRIORITY 12CPUTIME 1ALLTIME 3STARTBLOCK -1BLOCKTIME
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1