1、在本实验中按数值大小决定优先级,数值大的优先级高。2) 基于时间片调度 将所有的就绪进程按照先来先服务的原则,排成一个队列,每次调度时,将 cpu 分配给队首进程,并令其执行一个时间片。当时间片用完时,由一个计时器发出时钟中断请求,调度程序把此进程终止,把该进程放到队尾。在该实验中,时间片以 100ms 为单位(实际的要小得多)。在调度过程中,需要通过时间函数检测进程的执行时间,当该进程执行时间?时间片大小时,进行调度。3) 高优先级调度 优先级高的进程优先得到 cpu,等该进程执行完毕后,另外的进程才能执行。4) 基于时间片的高优先级调度 在调度算法中,只有处于就绪状态的进程才能被调度,调度
2、算法结合了优先级调度和时间片轮转调度算法,约定:从最高优先级队列取第1个就绪状态的进程进行调度,时间片到后降低其优先级(降低一半),然后插入到低优先级队列的尾部,每次调度后,显示进程的状态。四、程序清单 #include stdio.htime.h/#include string.h/定义进程数 #define LEN 10 /定义最高优先级 #define MAXPIOR 3 / 定义时间片 #define QUANTUM 2 #define PCB sizeof(struct pcb) struct pcb /PCB int ident;/标识符 int state;/状态 0-就绪,1,
3、运行,2,堵塞 int pior;/优先级,MAXPIOR为最高优先级*/ int life;/生命期*/ struct pcb *next;/*指针*/ *arrayMAXPIOR;static int idlistLEN;/*标识符表*/ int life=0;/*总生命期初始化为0*/ char str20;char command710;int killtest=0;void init();int create();void kill(int x);void process();void routine();void ps();void init() int i=0;for (i=0;
4、iident=i;state=0;pior=pior;life=1+rand()%20;/进程有生命期假设为1,20 next=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-ident);ps();end displaynreturn 1;void ps() in
5、t 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) int i=0,test=0;struct pcb *p=NULL,*q=NULL;while(test=0&i!=MAXPIOR) if (i!=MAXPIOR & p=NULL) continue;if (p-ident=x) test=1;killtest=1;break;if (test=0) i+;/*找到X所在指针*/ if (i=MAXPIOR
6、) Invaild process number.state=2) the process %d has blocked,cannot sleep again!p-state=2;void awake(int x) state=0) the process %d is ready state,cannot awake again!void kill(int x) if (p=arrayi) arrayi=arrayi-idlistx=0;free(p);next=p-life=life-(p-void process()/对输入命令的处理 int i=0,ii=0;7;if (stricmp(
7、str,commandi)=0) switch(i) case 0:thank you for using the program!exit(0);case 1:case 2:create();case 3:Which process you want to kill?scanf(%d,&ii);kill(ii);case 4:routine();case 5:Which process you want to sleep?sleep(ii);case 6:Which process you want to awake?awake(ii);default:Error command.Pleas
8、e input create, ps, kill,sleep,awake,quitnvoid routine()/执行一次调度运行,将最高优先级队列的进程运行1个时间片,并降低其优先级 int i=MAXPIOR-1,pior=0,t;struct pcb *pp,*qq,*pr,*r;do while (i=0 & arrayi=NULL) i=i-;if (istate!=0) pr=r;r=r-i-;while(r=NULL);/从高优先队列中寻找就绪进程以调度它 The one in the hightest piror process will execute 1 quantum.n
9、r-state=1; /进程处于运行状态 process id=%d is running.,r-for (int k=1;k600000;k+) for(int k1=1;k1pior)/2; /进程处于就绪状态 if(r-life-QUANTUMlife=r-life-QUANTUM; /时间减少QUANTUM life=life-QUANTUM;life=life-r-life;life=0;if (r-life=0)/进程运行完成,KILL它 the process %d is successful run,and release it!kill(r-if (pr=r) /将r结点从原
10、队列中删除 arrayi+1=r-pr-next=r-t=r-pior; /将r进程加入到相应低优先级队列中的最后 pp=arrayt;qq=NULL;while (pp!qq=pp;pp=pp-if(qq=NULL) /插入到队尾 arrayt=r;qq-next=r;after.nn 1 quantum successful run!/* void main() init();Welcome to the Process Scheduling system. This program simulate the Round-Robin with piror Scheduling alogrithm. nc:%s,str);process();while (strcmp(str,)!=0) nc:五、实验结果分析 对算法时间、空间复杂度、结果进行分析。六、思考题 (1)读懂程序画出算法所用的数据结构简图。(2)修改routine()函数,使得算法能够模拟运行进程被外界中断或因请求设备而不能运行自动转入阻塞状态并进行调度。