时间片轮转调度算法

上传人:pu****.1 文档编号:470026843 上传时间:2023-12-20 格式:DOC 页数:10 大小:28.52KB
返回 下载 相关 举报
时间片轮转调度算法_第1页
第1页 / 共10页
时间片轮转调度算法_第2页
第2页 / 共10页
时间片轮转调度算法_第3页
第3页 / 共10页
时间片轮转调度算法_第4页
第4页 / 共10页
时间片轮转调度算法_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《时间片轮转调度算法》由会员分享,可在线阅读,更多相关《时间片轮转调度算法(10页珍藏版)》请在金锄头文库上搜索。

1、#include #include #include #include/*进程控制块数据结构*/typedef struct node char name10;/*进程名*/int prio; /*进程优先级*/ int round; /*循环轮转法进程每次轮转的时间片*/ int cputime; /*进程累计消耗的CUP时间*/int needtime; /*进程到完成还需要的CUP时间*/int count; /*循环轮转法一个时间片内进程运行时间*/char state; /*进程的状态:R:运行,W:等待,F:结束*/struct node *next;/*指向下一个进程的链指针*/

2、 PCB;PCB *finish,*ready,*tail,*run;/*指向三个队列的队首的指针, finish为完成队列头指针, ready为就绪队列头指针, tail为就绪队列的队尾指针, run为当前运行进程头指针*/int N;/*定义进程的数目*/void firstin(void); /调度就绪队列的第一个进程投入运行;void print1(char a); /打印表头行信息void print2(char chose,PCB *p); /打印每一行的状态信息void print(char chose); /打印每执行一次算法后所有的进程的状态信息void insert_pri

3、o(PCB *q); /在优先数算法中,将尚未完成的PCB按优先数顺序插入到就绪队列中; void prior_init(char chose); /进程优先级法初始化将进程按优先级插入到就绪队列里 void priority(char chose); /进程优先级算法总函数void insert_rr(PCB *q); /在轮转法中,将执行了一个时间片单位(为2),但尚未完成的进程的PCB,插到就绪队列的队尾; void roundrun_init(char chose); /循环轮转法初始化将就绪队列保存为FIFO队列 void roundrun(char chose); /循环轮转法总算

4、法void main()/主函数 char chose= ; while(chose!=e)&(chose!=E) fflush(stdin); system(cls); /*printf(ttt两种进程调度算法的模拟nn); printf(tP.进程优先级算法模拟nn);*/ printf(tR.循环轮转算法模拟nn); printf(tE.退出程序nn); printf(t请输入你的选择:); scanf(%c,&chose); if(chose!=e)&(chose!=E) system(cls); /*if(chose=P)|(chose=p) prior_init(chose); p

5、riority(chose); system(cls); */ /*else */if(chose=r)|(chose=R) roundrun_init(chose); roundrun(chose); system(cls); printf(ntt谢谢使用!n);void firstin(void)/调度就绪队列的第一个进程投入运行; if(ready!=NULL) run=ready; ready=ready-next; run-state=R; run-next=NULL; else run=NULL; void print1(char a)/打印表头行信息if(toupper(a)=P

6、)printf(name cputime needtime priority state n);elseprintf(name cputime needtime count round state n); void print2(char chose,PCB *p)/打印每一行的状态信息if(toupper(chose)=P) printf(%st%dt%dt%dt %cn,p-name,p-cputime,p-needtime,p-prio,p-state); elseprintf(%st%dt%dt%dt%dt%cn,p-name,p-cputime,p-needtime,p-count,

7、p-round,p-state);void print(char chose)/打印每执行一次算法后所有的进程的状态信息PCB *p;print1(chose);if(run!=NULL)print2(chose,run);p=ready;while(p!=NULL) print2(chose,p); p=p-next;p=finish;while(p!=NULL)print2(chose,p);p=p-next;void insert_prio(PCB *q)/*在优先数算法中,将尚未 完成的PCB按优先数顺序插入到就绪队列中;*/PCB *p,*s,*r; /*p,r用来控制就绪队列滚动,

8、S指向插入的队列*/s=q;p=ready;r=p;if(s-prioready-prio)/ 要插入的进程的优先级大于ready的优先级 s-next=ready;ready=s; else/要插入的进程的优先级不大于ready的优先级 while(p) if(p-prio=s-prio) r=p; p=p-next; else break; /找到要插入的位置s-next=p;r-next=s;/*void prior_init(char chose)/*进程优先级法初始化 将进程按优先级插入到就绪队列里 PCB *p; int i,time; char na10; ready=NULL;

9、 finish=NULL; run=NULL; printf(tt进程优先级算法模拟全过程nn); printf(输入进程 的个数 N:n); scanf(%d,&N);for(i=0;iname,na);p-cputime=0;p-needtime=time;p-state=W;p-prio=50-time;/设置进程优先值初值if(ready=NULL) ready=p; ready-next=NULL;else insert_prio(p);printf(当前就绪队列的进程的信息n);print(chose);printf(%d个进程已按优先级从高到低进到就绪队列中n,N); print

10、f(按回车键开始模拟优先级算法.n); fflush(stdin); getchar();firstin();*/*void priority(char chose)/进程优先级算法总函数int i=1;while(run!=NULL)run-cputime+=1;run-needtime-=1;run-prio-=1;if(run-needtime=0) run-next=finish; finish=run; run-state=F; run-prio=0; run=NULL; firstin(); else if(ready!=NULL)&(run-prioprio) run-state

11、=W; insert_prio(run); run=NULL; firstin(); printf(第%d次执行优先级算法n,i+); print(chose); if(run) printf(按回车键继续下一次优先级算法.n); else printf(优先级算法模拟过程结束!n); fflush(stdin); getchar();*/void insert_rr(PCB *q)/在轮转法中,将执行了一个时间片单位(为2), /但尚未完成的进程的PCB,插到就绪队列的队尾;tail-next=q;tail=q;q-next=NULL;void roundrun_init(char chose)/*循环轮转法初始化 将就绪队列保存为FIFO队列*/PCB *p;int i,time;char na10;ready=NULL;finish=NULL;run=NULL;printf(tt循环轮转算法模拟全过程nn);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号