1、实验A4使用动态优先权地进程调度算法地模拟实验四 使用动态优先权的进程调度算法的模拟hi.baidu./xinghui100/blog/item/c41d5c1b325b40d0ad6e75dc.html1、实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。2、实验容(1)用C语言来实现对N个进程采用动态优先算法的进程调度;(2)每个用来标识进程的进程控制块 PCB用结构来描述,包括以下字段: 进程标识符id 进程优先数priority,并规定优先数越大的进程,其优先权越高; 进程已占用的CPU时间cputime ; 进程还需占用的CPU时间alltime,当进程运行完毕时,
2、alltime变为0; 进程的阻塞时间startblock,表示当进程再运行startblock个时间片后,进程将进入阻塞状态; 进程被阻塞的时间blocktime,表示已阻塞的进程再等待blocktime个时间片后,将转换成就绪态 进程状态state; 队列指针next,用来将PCB排成队列(3)优先数改变的原则: 进程在就绪队列中呆一个时间片,优先数增加1 进程每运行一个时间片,优先数减3。(4)假设在调度前,系统中有5个进程,它们的初始状态如下:ID01234PRIORITY93830290CPUTIME00000ALLTIME33634STARTBLOCK2-1-1-1-1BLOCKT
3、IME30000STATEREADYREADYREADYREADYREADY(5)为了清楚地观察诸进程的调度过程,程序应将每个时间片的进程的情况显示出来,参照的具体格式如下:RUNNING PROG: iREADY_QUEUE:-id1-id2BLOCK_QUEUE:-id3-id4=ID 0 1 2 3 4PRIORITY P0 P1 P2 P3 P4CPUTIME C0 C1 C2 C3 C4ALLTIME A0 A1 A2 A3 A4STARTBLOCK T0 T1 T2 T3 T4BLOCKTIME B0 B1 B2 B3 B4STATE S0 S1 S2 S3 S43、思考题(1)在
4、实际的调度中,除了按调度算法选择下一个执行的进程外,还应处理哪些工作?队列实现:#include/#define NULL 0#define M 10 typedef struct node int id; int pr; int ct; int at; int bt; int sb; struct node *next; jd;jd *max(jd *p) jd *maxnode=NULL,*p1,*p2,*p3=p; int maxnum; p1=p; p2=p; if(p-next=NULL) return NULL; maxnum=p-next-pr; while(p1-next!=N
5、ULL) p2=p1-next; if(maxnum pr) maxnode=p2; p3=p1; maxnum=p2-pr; p1=p1-next; p3-next=maxnode-next; maxnode-next=NULL; return maxnode; void blocktoready(jd *pblock,jd *pready) jd *p1=pblock,*p3; while(p1-next!=NULL) p3=p1-next; if(p3-bt=0) p1-next=p3-next; p3-next=pready-next; pready-next=p3; p1=p1-ne
6、xt; if(p1=NULL) break; void ready(jd *p) jd *p1=p-next; while(p1!=NULL) p1-pr+; p1=p1-next; void run(jd *p) jd *p1; if(p-next!=NULL) p1=p-next; p1-pr=p1-pr-3; p1-at-; p1-ct+; void block(jd *p) jd *p1=p-next; while(p1!=NULL) p1-bt-; p1=p1-next; void runtoreadyorblock(jd *prun,jd *pready,jd *pblock) j
7、d *p; if(prun-next=NULL) return; p=prun-next; if(p-at=0) prun-next=NULL; else if(p-ct=p-sb) p-next=pblock-next; pblock-next=p; prun-next=NULL; else p-next=pready-next; pready-next=p; prun-next=NULL; jd *head(jd pcb,int L) int i; for(i=0;iL;i+) printf(input pcd%d informationn,i); printf(PRIORITY:); s
8、canf(%d,&pcbi.pr); printf(ALLTIME:); scanf(%d,&pcbi.at); printf(STARTBLOCK:); scanf(%d,&pcbi.sb); printf(BLOCKTIME:); scanf(%d,&pcbi.bt); pcbi.id=i; pcbi.ct=0; for(i=0;inext=NULL) printf(ttthe queue are emptyn); while(p-next!=NULL) p1=p-next; printf(tt%dt%dt%dt%dt%dt%dn,p1-id,p1-pr,p1-ct,p1-at,p1-sb
9、,p1-bt); p=p-next; if(p=NULL) break; int main() jd pcbM; jd *pready=(jd *)malloc(sizeof(jd); jd *prun=(jd *)malloc(sizeof(jd); jd *pblock=(jd *)malloc(sizeof(jd); int L,i,n=1; pready-next=NULL; prun-next=NULL; pblock-next=NULL; printf(please input the number of pcb :n); scanf(%d,&L); pready-next=hea
10、d(pcb,L); while(1) prun-next=max(pready); run(prun); ready(pready); block(pblock); printf(running%d every pcb information:n,n); printf(ttidtprtcttattsbtbtn); printf(the ready pcb:n); print(pready); printf(the run pcb:n); print(prun); printf(the block pcb:n); print(pblock); printf(the all pcb:n); for
11、(i=0;inext=NULL & pblock-next=NULL) break; xt=head(pcb,L); while(1) prun-next=max(pready); run(prun); ready(pready); block(pblock); printf(running%d every pcb information:n,n); printf(ttidtprtcttattsbtbtn); printf(the ready pcb:n); print(pready); printf(the run pcb:n); print(prun); printf(the block
12、pcb:n); print(pblock); printf(the all pcb:n); for(i=0;inext=NULL & pblock-next=NULL) break; xt=head(pcb,L); while(1) prun-next=max(pready); run(prun); ready(pready); block(pblock); printf(running%d every pcb information:n,n); printf(ttidtprtcttattsbtbtn); printf(the ready pcb:n); print(pready); prin
13、tf(the run pcb:n); print(prun); printf(the block pcb:n); print(pblock); printf(the all pcb:n); for(i=0;inext=NULL & pblock-next=NULL) break; 运行结果:rootlocalhost root# gcc -o a shiyan4.crootlocalhost root# ./a数组实现: 实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。2、 实验容:(1) 用C语言来实现对N个进程采用动态优先权优先算法的进程调度。(2) 每个用来标识进程的
14、进程控制块PCB用结构来描述,包括以下字段: 进程标识数ID 进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高 进程已占用的CPU时间CUPTIME 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态。 进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。 进程状态STATE. 队列指针NEXT,用来将 PCB排成队列。(3) 优先数改变的原则: 进程在就绪队列中呆一个时间片,优先数增加1。 进
15、程每运行一个时间片,优先数减3。(4) 假设在调度前,系统中有 5个进程,它们的初始状态如下:ID 0 1 2 3 4PRIORITY 9 38 30 29 0CPUTIME 0 0 0 0 0ALLTIME 3 3 6 3 4STARTBLOCK 2 -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE READY READY READY READY READY(5)为了清楚地观察诸进程的调度过程,程序将每个时间片的进程的情况显示出来,参照的具体格式如下: RUNING PROG:i READY_QUEUE: -id1-id2 BLOCK_QUEUE: -id3-id4ID
16、 0 1 2 3 4PRIORITY P0 P1 P2 P3 P4CPUTIME C0 C1 C2 C3 C4ALLTIME A0 A1 A2 A3 A4STARTBLOCK T0 T1 T2 T3 T4BLOCKTIME B0 B1 B2 B3 B4STATE S0 S1 S2 S3 S4#include #include using namespace std; int i;/循环值 int j;/还在阻塞或就绪队列中的进程数 int s; int m;/最大priority的id struct pcb int id; int p; /priority int cputime; int a
17、lltime; 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.startblock-; return 1; if(pro0.blocktime=0) pro0.state=0; r
18、eturn 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(); int max=s; m=pro0.id; for(i=0;iproi.p) max=proi+1.p; m=p
19、roi+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:mendl; cout=n; coutID ; for(i=0;ij;i+) cout.width(10); coutp
20、roi.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.width(10); coutproi.startblock; coutendlBLOCKTIME ; for(
21、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(); 运行结果:rootlocalhost root# g+ -o c c.crootlocalhost root# ./cRUNNING 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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1