1、4) 基于时间片的高优先级调度在调度算法中,只有处于就绪状态的进程才能被调度,调度算法结合了优先级调度和时间片轮转调度算法,约定:从最高优先级队列取第1个就绪状态的进程进行调度,时间片到后降低其优先级(减半),然后插入到低优先级队列的尾部,每次调度后,显示进程的状态。四、程序清单#include stdio.htime.h/#include string.h/定义进程数#define LEN 10/定义最高优先级#define MAXPIOR 3/ 定义时间片#define QUANTUM 2#define PCB sizeof(struct pcb)struct pcb /PCBint id
2、ent;/标识符 int state;/状态 0-就绪,1运行,2堵塞 int pior;/优先级,MAXPIOR为最高优先级*/ int life;/生命期*/ struct pcb *next;/*指针*/ *arrayMAXPIOR;/ 定义优先级队列static int idlistLEN;/*标识符表,标识进程是否被创建,0为未创建*/int life=0;/*总生命期初始化为0*/char str20;char command710;void init();int create();void kill(int x);void process();void round();void
3、ps();/初始化命令void init() int i=0; for (i=0;iident=i;state=0;pior=pior;life=1+rand()%20;/进程有生命期假设为120next=NULL; life=life+(s-life); p=arraypior;/建立同优先级队列(链表) if (p=NULL) arraypior=s; else while(p!=NULL) q=p; p=p-next; q-next=s; printf(success create process id=%d, current process state disp below:n,s-i
4、dent); ps();end displayn return 1;/显示每个优先级队列中的所有进程信息void ps()int i=0; struct pcb *p; p=arrayi; while (p! printf(id:%d,state:%d,pior:%d,life:%dn,p-ident,p-state,p-pior,p- /阻塞进程void sleep(int x)/寻找X所在指针,提示:参考kill(int x)函数/如果找不到,输出错误信息,如果找到,修改p-state的状态int i=0,test=0; struct pcb *p=NULL,*q=NULL; while(
5、test=0&i!=MAXPIOR)/ if (i!=MAXPIOR & p=NULL) i+;continue; if (p-ident=x) test=1;break; q=p;p=p- if (test=0) i+; /*寻找X所在指针*/ if(i=MAXPIOR)Invaild process number if(p-state=2)the process %d has blocked,cannot sleep again p-state=2;void awake(int x)/代码同sleep(int x)函数state=0)the process %d has blocked,c
6、annot awake againstate=0;void kill(int x)int i=0,test=0; if (i=MAXPIOR) /找不到X所在指针Invaild process number. else /找到X所在指针,则将其从优先级队列中删除 if (p=arrayi) arrayi=arrayi- idlistx=0; free(p); q-next=p- idlistx=0; life=life-(p- free(p);/对输入命令的处理void process()int i=0,ii=0;7; if (stricmp(str,commandi)=0) break; s
7、witch(i) case 0:printf(thank you for using the program!exit(0); case 1:ps(); case 2:create(); case 3: printf(Which process you want to kill? scanf(%d,&ii); kill(ii); break; case 4: round(); case 5:Which process you want to sleep? sleep(ii); case 6:Which process you want to awake? awake(ii); default:
8、Error command.Please input create, ps, kill,sleep,awake,quitn/模拟基于优先级的时间片轮转调度算法,/执行一次调度运行,将最高优先级队列的进程运行1个时间片,并降低其优先级void round() int i=MAXPIOR-1,pior=0,t; struct pcb *pp,*qq,*pr,*r; do while(iarrayi=NULL) i=i-; if(istate!=0) pr=r; r=r- i-; while(r=NULL);the one in the highest piror process will exec
9、ute 1 quantum. r-state=1;process id=%d is running,r- /从高优先队列开始,队列中寻找一个就绪进程以调度它,让其执行一个时间片 /执行过程: /首先让进程处于运行状态,并给出“第x个进程正在运行”的提示信息 /其次,用下面用循环模拟延时,自己也可以采用其他方法 for (int k=1;k600000;k+) for(int k1=1;k1pior)/2; if(r-life-QUANTUMlife=r-life-QUANTUM; life=life-QUANTUM; life=life-r-life;life=0;life=0)the pro
10、cess %d has success run and release it kill(r- if(pr=r) arrayi+1=r- pr-next=r- t=r-pior; pp=arrayt; qq=NULL; while(pp! qq=pp; pp=pp- if(qq=NULL) arrayt=r; qq-next=r;next!=NULL;after.n 1 quantum seccessful run!/运行结束,并给出“第x个进程运行结束”的提示信息/将进程优先级减半/让进程处于就绪状态/将进程的生命期减少,此时需要判断,如果该进程当前生命期QUANTUM,/则直接减去一个时间周
11、期QUANTUM,否则,直接将进程当前生命期减为0/进程的生命期为0,说明进程运行完成,KILL它,/否则,将该进程结点从原队列中删除,并将其加入到相应低优先级队列中的最后/调用ps()输出运行一个周期后,所有优先级队列中的所有进程信息/输出信息,提示一个周期成功运行/*int main()init();Welcome to the Process Scheduling system. This program simulate the Round-Robin with piror Scheduling alogrithm. nc:%s,str); process(); while (strc
12、mp(str,)!nc:五、实习报告要求:1. 代码补充: 请利用提供的代码框架补充代码,完成sleep,awake和round函数。补充代码如上!2.思考题:读懂程序并画出您所用的数据结构简图;至少包含:连续创建进程05的过程中,得到的按优先级存放的结构;3. 对不同的执行结果进行截图,并对截图进行说明,截图至少要包含创建后的初始状态,阻塞部分进程后的状态,再唤醒部分进程后的状态,删除部分进程后的状态,运行1个时间周期、3个时间周期和个5时间周期的各优先级队列的进程状态截图;4. 给出round和sleep,awake函数的代码,本次实习不用单独交代码。六实验总结本次代码结合老师给的示例以及和同学交流之后完成,通过这次实验我学到了很多。对于时间片轮转有了更深的认识!
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2