使用动态优先权的进程调度算法的模拟实验_第1页
使用动态优先权的进程调度算法的模拟实验_第2页
使用动态优先权的进程调度算法的模拟实验_第3页
使用动态优先权的进程调度算法的模拟实验_第4页
使用动态优先权的进程调度算法的模拟实验_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、使用动态优先权的进程调度算法的模拟试验1.试验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的懂得;2.试验内容(1)用 c语言实现对n 个进程采纳动态优先权优先算法的进程调度;(2)每个用来标识进程的进程掌握块pcb用结构来描述,包括以下字段: 进程标识数;进程优先数priority ,并规定优先数越大的进程,其优先权越高;进程已占用的cpu时间 cputime ;进程仍需占用的cpu 时间 alltime ,当进程运行完毕时,alltime 变为 0 ;进程的堵塞时间startblock ,表示当进程再运行startblock 个时间片后, 进程将进入堵塞状态;进程被堵塞的时间b

2、licktime ,表示已堵塞的进程再等待blocktime 个时间片后,将转换为就绪态;进程状态 state ;队列指针 next ,用来将 pcb排成队列;(3)优先数转变的原就:进程在就绪队列中呆一个时间片,优先数增加1.进程每运行一个时间片,优先数减3;(4)假设在调度前,系统中有5 个进程,它们得初始状态如下:id01234priority93830290cputime00000alltime33634startblock2-1-1-1-1blocktime30000statereadyreadyreadyreadyready(5)为了清晰地观看诸进程的调度过程,程序应将每个时间片内

3、的进程的情形显示出来, 参照的详细格式如下:running prog: iready_queu:e ->id1->id2block_queue: ->id3->id4=id01234prioritycputimep0c0p1c1p2c3p3c4p4c5alltimea0a1a2a3a4startblockt0t1t2t3t4blocktimeb0b1b2b3b4states0s1s2s3s4开头创建就绪队列3.过程(流程图)否alltime>0是就绪执行显示状态转变优先数p.alltime-1 p.cuptime+1是p.alltime=0否p.startbloc

4、k>0p.startblock-1否否p.startblock=0是是执行堵塞执行就绪是blk=null否p.blocktime-1否p.blocktime =0是堵塞就绪终止4.代码#include <stdio.h>#include <stdlib.h>#include <string.h> typedef struct nodeint id;/ 进程标识数int priority;/ 进程优先数,优先数越大优先级越高int cputime;/ 进程已占用的cpu 时间int alltime;/ 进程仍需占用的cpu时间int startblock

5、;/ 进程的堵塞时间int blocktime;/ 进程被堵塞的时间char state10;/ 进程状态struct node *next;/ 队列指针pcb;pcb *creatqueueint num/ 创建一个就绪队列int i;/i 为循环计数器pcb *head, *temp1, *temp2, *temp3;/head为就绪队列的头指针,temp1 为创建进程结点的指针,temp2 、temp3 分别为比较结点的前驱结点和比较结点fori=0;i<num;i+/ 依据进程的个数创建结点并按从大到小的次序进行排序temp1=pcb *mallocsizeofpcb; prin

6、tf" 输入第 %d 个进程的 idstaten",i;scanf"%d%d%d%d%d%d%s",&temp1->id,&temp1->priority,&temp1->cputime,&temp1->all time,&temp1->startblock,&temp1->blocktime,temp1->state;ifi=0/ 假如创建的是第一个结点head=temp1;head->next=null; continue;ifhead->prior

7、ity< temp1->priority/ 假如创建结点中所储存的数比头结点所储存的数要大,就直接把该结点插入到头结点之前temp1->next=head; head=temp1; continue;temp2=head;/temp2为比较结点的直接前驱结点temp3=temp2->next;/temp3为比较的结点whiletemp3.=null && temp3->priority>=temp1->priority/ 实现查找的功能temp2=temp3; temp3=temp2->next;temp2->next=te

8、mp1; temp1->next=temp3;return head;pcb *insertqueuepcb *head,pcb *run/ 在就绪队列中插入一个结点pcb *temp1,*temp2;/temp1和 temp2 分别为比较结点的前驱和比较结点ifhead=null/ 假如就绪队列为空head=run;head->next=null;else ifhead->priority < run->priority/ 假如插入结点中所储存的数比头结点所储存的数要大,就直接把该结点插入到头结点之前elserun->next=head; head=run

9、;temp1=head;/temp1为比较结点的直接前驱结点temp2=temp1->next;/temp2为比较的结点whiletemp2.=null && temp2->priority>=run->priority/ 实现查找的功能temp1=temp2; temp2=temp1->next;temp1->next=run; run->next=temp2;return head;mainint num;/num为进程的个数int alltime=0;/ 用来储存全部进程需要占用的cpu时间pcb *head;/head 为就绪队

10、列的头指针pcb *run=null;/run为执行进程结点的指针 pcb *block=null;/block 为堵塞进程的结点 pcb *temp;printf" 请输入进程的个数:" scanf"%d",&num; head=creatqueuenum; getchar;temp=head; whiletemp.=nullalltime+=temp->alltime; temp=temp->next;whilealltime > 0ifhead.=nullrun=head;/ 把就绪队列中的第一个进程取出来执行 head=

11、head->next;/ 就绪队列的头指针指向下一个结点 strcpyrun->state,"run"/ 状态改为执行run->next=null;/* 显示状态 */printf"running prog:%dn",run->id;/ 显示执行进程printf"ready_queue:"/ 显示就绪进程temp=head; whiletemp.=nullprintf"->%d",temp->id; temp=temp->next;printf"n"pr

12、intf"block_queue:"/ 显示堵塞进程ifblock.=nullprintf"%d",block->id;printf"n"printf"=n"printf"id priority cputime alltimestartblockblocktimestaten"printf"%d%d%d%d%d%d%sn",run->id,run->priority,run->cputime,run->alltime,run->startb

13、lock,run->blocktime,run->state;temp=head; whiletemp.=nullprintf"%d%d%d%d%d%d%sn",temp->id,temp->priority,temp->cputime,temp->alltime,temp->startblock,temp->block time,temp->state;temp=temp->next;ifblock.=nullprintf"%d%d%d%d%d%d%s",block->id,block-

14、>priority,block->cputime,block->alltime,block->startblock,block->blockti me,block->state;printf"n" printf"=n"/* 显示状态 */* 转变优先数 */run->priority-=3;/ 执行进程的优先数减3 temp=head;whiletemp.=null/ 就绪进程的优先数加1temp->priority+=1; temp=temp->next;/* 转变优先数 */* 转变执行进程的有关

15、参数*/run->cputime+=1;/ 执行进程的已占用cpu 时间加 1 run->alltime-=1;/ 仍需要的cpu时间减 1ifrun->alltime.=0ifrun->startblock > 0/ 假如该进程会被堵塞run->startblock-=1;/ 执行完一个时间片后, 开头堵塞的时间减1ifrun->startblock=0/ 假如堵塞的时间到了block=run;/ 执行转堵塞strcpyblock->state,"b"/ 状态转堵塞alltime-; printf"n"c

16、ontinue;strcpyrun->state,"r"/ 状态转就绪head=insertqueuehead,run;/ 执行转就绪run=null;else/* 转变执行进程的有关参数*/ alltime-;/* 显示状态 */printf"running prog:n"/ 显示执行进程 printf"ready_queue:n"/ 显示就绪进程 printf"block_queue:"/ 显示堵塞进程ifblock.=nullprintf"%d",block->id;print

17、f"n"printf"=n"printf"id priority cputime alltimestartblockblocktimestaten"ifblock.=nullprintf"%d%d%d%d%d%d%s",block->id,block->priority,block->cputime,block->alltime,block->startblock,block->blockti me,block->state;printf"n"print

18、f"=n"/* 显示状态 */* 转变堵塞进程的有关参数*/ifblock.=null/ 假如有堵塞进程block->blocktime-=1;/ 被堵塞的时间减1 ifblock->blocktime=0 /假如被堵塞的时间到了strcpyblock->state,"r"/ 状态转就绪head=insertqueuehead,block;/ 堵塞转就绪 block=null;/* 转变堵塞进程的有关参数*/getchar;5.运行结果输入 5 个进程,分别是0 4 进程,运行结果可以看到第一次运行进程1,优先数为38; 其次次运行的进

19、程是进程1,优先数为35,cpu 时间占用为1,进程所需时间为2, 同时下一个进程(进程1)的优先数 +1;第三次运行进程2,优先数32, cpu 占用时间将 +1,所需时间将-1;同时下一个进程(进程1)优先数 +1,;第四次运行进程1,优先数33, cpu 占用时间2+1,所需时间将-1;同时下一个进程(进程3)优先数 +1,第四次运行进程1 完毕,所需时间为0;进程 1 运行完毕;第五次运行进程3,优先数33, cpu 占用时间0 将+1,所需时间3 将-1;同时下一个进程(进程2)优先数 +1;第六次运行进程2,优先数31 将-3, cpu 占用时间1 将 +1,所需时间5 将-1;同

20、时下一个进程(进程3)优先数 +1;第七次运行进程3,优先数31 将-3, cpu 占用时间1 将 +1,所需时间2 将-1;同时下一个进程(进程2)优先数 +1;第八次运行进程2,优先数29 将-3, cpu 占用时间同时下一个进程(进程3)优先数 +1;2 将 +1,所需时间4 将-1;第九次运行进程3,优先数29 将-3, cpu 占用时间2 将 +1,所需时间1 将-1;同时下一个进程(进程2)优先数 +1;第九次运行完毕,进程3 的所需时间为0,进程 3 运行完毕;第十次运行进程2,优先数27 将 -3, cpu 占用时间3 将+1,所需时间3 将-1;同时下一个进程(进程0)优先数

21、 +1;第十一次运行进程2,优先数 24 将-3, cpu 占用时间4 将+1,所需时间2 将-1;同时下一个进程(进程0)优先数 +1;第十二次运行进程2,优先数 21 将-3, cpu 占用时间5 将+1,所需时间1 将-1;同时下一个进程(进程0)优先数 +1;第十二次运行完毕,进程2 所需时间为0,进程 2 运行完毕;第十三次运行进程0,优先数 21 将-3, cpu 占用时间0 将+1,所需时间3 将-1;同时下一个进程(进程4)优先数 +1;第十四次运行进程0,优先数 18 将-3, cpu 占用时间1 将+1,所需时间2 将-1;同时下一个进程(进程4)优先数 +1;第十五次运行进程4,优先数 14 将-3, cpu 占用时间0 将+1,所需时间4 将-1;同时下一个进程(进程0)优先数 +1;第十六次运行进程4,优先数 11 将-3, cpu 占用时间1 将+1,所需时间3 将-1;同时下一个进程(进程0)优先数 +1;第十七次运行进程4,优先数 8 将-3, cpu 占用时间2 将+1,所需时间2 将-1;同时下一个进程(进程0)优先数 +1;第十八次运行进程0,优先数 15 将-3, cpu 占用时间2 将+1,所需时间1 将-1;同时下一个进程(进程4)优

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论