




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验2 进程状态转换及其pcb的变化1 目的自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程 之间的状态转换及其所带来的pcb内容、组织的变化,理解进程与其pcb间 的一一对应关系。2. 内容及要求1)设计并实现一个模拟进程状态转换及其相应pcb内容、组织结构变化的 程序。2)独立编写、调试程序。进程的数目、进程的状态模型(三状态、五状态、 七状态或其它)以及pcb的组织形式可自行选择。3)合理设计与进程pcb相对应的数据结构。pcb的内容要涵盖进程的基 本信息、控制信息、资源需求及现场信息。4)设计出可视性较好的界面,应能反映出进程状态的变化引起的对应pcb 内容、组织结构
2、的变化。5)代码书写要规范,要适当地加入注释。6)鼓励在实验中加入新的观点或想法,并加以实现。7)认真进行预习,完成预习报告。8)实验完成后,耍认真总结,完成实验报告。3. 程序流程图进程的三种基本状态及其转换如下图所示。y结束j4 数据结构及说明在本实验中,主要的数据结构是pcb的数据结构,具体如下: struct processchar name; 进程名称int needtime; ow所需要的运行吋间int priority;/进程的优先级5 源程序#in clude<stdio.h>#in clude<stdlib.h>#in clude<stri ng
3、.h>struct process!char name;int needtime;int priority;;struct process readyqueue5;struct process run;struct process blockedqueue5;const struct process nu归null,0,0;int readyqueuehead=o;int blockedqueuehead=0;int cpustate=0;int cputime=0;void order(struct process parameter,int head);/将队列中的进程按优先级排列
4、int creat();void dispath();int timeout();int eventwait();int eventoccur();void order(struct process parameter,int head)int k,i;struct process temp; for(k=0;k<head-1 ;k+)for(i=0;i<head-k-1 ;i+) if(parameteri.priority>=parameteri+1.priority) temp=parameteri;parameteri=parameteri+1;parameteri+
5、1=怕 mp;int creat()if(readyqueuehead>=5)printfc'the ready queue has been fulln");return 0;labell:printf(" input new process name(must be a letter): rt);scanf(h%c",&(readyqueuereadyqueueh);getchar();int k;for( k=o;k<readyqueuehead;k+)if(readyqueuereadyqueuehead.nam
6、e=readyq|readyqueuerea dyqueuehead. name=readyq+32|readyqueuereadyqueuehead. na me=readyqueuek. name32)printf(hthe process is already exist!rt);goto labell;for( k=0;k<blockedqueuehead;k+)if(readyqueuereadyqueueh=blockedqueuek. name|readyqueuer eadyqueuehead. name=block
7、edq+32|readyqueuereadyqueuehea d.n ame=blockedqueuek. name32)printf("the process is already exist!rt);goto labell;if(readyqueuereadyqueueh=r un.n ame|readyqueuereadyqueueh ead. name=r un.n ame+32|readyqueuereadyqueueh=r un.n ame-32)printf("the process is already e
8、xist!rt);goto labell;printf(hinput needtime (input a int number):n");iabel2:scanf("%dh,&(readyqueuereadyqueuehead.needtime);getchar();if(readyqueuereadyqueuehead. needtimevl |readyqueuereadyqueuehead.n eedtime>100)printf("please input the true needtime(1-100)nh); goto iabel2;pr
9、intf(h input the priority(1-10): nh);iabel3:scanf(n%d",&(readyqueuereadyqueuehead.priority);getchar(); if(readyqueuereadyqueuehead.priority<1 |readyqueuereadyqueuehead.prio rity>10)printffplease 1-10!nh);goto iabel3;readyqueuehead+;order(readyqueue,readyqueuehead); return 0;void dispa
10、th()if (cpustate=0)readyqueuehead-;if(readyqueuereadyqueuehead.needtime>0) order(readyqueue,readyqueuehead); run=readyqueuereadyqueuehead; readyqueuereadyqueuehead=n ull;cpustate=1;else printf("no process in the ready queuert);else timeout();dispath();int timeout()cputime+;if (=null)
11、 return 0;readyqueuereadyqueuehead=run;run=n ull;cpustate=o; readyqueuereadyqueuehead.needtime-; if(readyqueuereadyqueuehead.needtime=o)hasprin tf("theprocess'%cfinished",readyqueuereadyqueueh);readyqueuereadyqueuehead=n ull; return 0;readyqueuehead+;order(readyqueue,readyqueue
12、head); return 0;int eventwait()if(blockedqueuehead>=5)printf("error:the blocked queue has been full'n”); return 0; if(cpustate=o) printf("error:no process in cpu"); return 0;run .needtime-;blockedqueueblockedqueuehead=run; blockedqueuehead+;run=n ull;cpustate=o;cputime+;printf(
13、"the process is blocked!rt); return 0;int eventoccur()if(readyqueuehead>=5)printf("the ready queue has been fullnh);return 0;prin tf("please in put the process name whose eve nt occured!rt); char name=getchar();getchar();inti;struct process temp; for(i=0;i<blockedqueuehead;i+)if
14、(name=blockedq) blockedqueuehead-; readyqueuereadyqueuehead=blockedqueuei; readyqueuehead+;blockedqueuei=blockedqueueblockedqueuehead; blockedqueueblockedqueuehead=null; order(readyqueue,readyqueuehead);printf("the process %c is ready!n",name);return 0;if(i=blockedqueuehead)print
15、f(uerror:this process has not been blocked!rt);return 0;int show()printf(”ncpu time:%dnh,cputime);printfc*name needtime priority'"');printf("ready queue:”);inti;if(readyqueueo.name!=null)for(i=readyqueuehead;i>0;i-)printf(” c%d%dn",readyqueuei-1. name,readyqueueieedtime,rea
16、dyqueuei-1.priority);else printf(”null”);printf("nrurrning process:”);if(=null) printf(nnulln);else printf("%c%d%dn”,run .name,run.needtime,ru n.priority);printfc'vnblock queue:”);if(blockedq=null) printf("null");else for(i=blockedqueuehead;i>0;i-)printf(
17、"%c%d%dn",blockedqueuei .n ame,blockedqueuei .n eedtime,blockedqueuei .priority); int main()select:prin tf("nn1:inputnewprocessn2:dispathn3:timeoutn4:eventwaitn5:eventoccursno:exitrt);int select=getchar();getchar();switch(select)case '1 ':creat();show();break;case 2:dispath();
18、show();break;case '3':timeout();show();break;case *4':eventwait():show();break;case '5':eventoccur();show();break;case 'o':exit(o); default:printf("please select from 0 to 5n");goto select;return 0;6 运行结果及其说明(1) 创建进程:按'1'创建进程,进程被放入就绪队列,并按优先级从高到 低 排列。就绪队列
19、最多容纳5个进程,当创建第6个进程吋,程序会提示。1:input new process2:dispath3:tineout4:euentwait5:euentoccurs0:exit1input new process name<must be a letter>:ainput needtime <input a int nunber>:3input the priority<l10>:4cpu tine:0name needtine priorityready queue:a34running process:nullblock queue:null1:
20、input new processname needtime prioritybeady queue:punning process:nullblock queue:null11:input new process2:dispath3:timeout4:euentwait5:euentoccurs0:exit1input new process nane<nust be a letter>: binput needtime <input a int number):1input the priority<l10>:10cpu time:0name needtine
21、 priorityready queue:b110input the priority<l10>: 10cpu t ine:0ready queue :nameneedtinepriorityb110a34running process :nullblock queue:null1:input new process2:dispath3:timeout4:euentwait5:euentoccurs0:exit1input nev/ process nane<nust be a letter>:cinput needtine <input a int number
22、>:5input the p>*iopity<l10> :6cpu t ine:0nameneedtimepriorityready queue:b110c56a34cpu time:0naneneedtineprioritybeady queue:b110c56a34d22epunning process:nullblock queue:null1:input new process2:dispath3:t imeout 4:euentwait5:euentoccurs 0:exit121the ready queue 半:has beenfull(2)dispath
23、:按'2'将就绪队列中的进程转移到运行队列中,如果运行队列中己有进程 则该进程先执行一个时间片,然后回到就绪队列屮,最后将新就绪队列屮优先级最 大的进程放到运行队列中nameneedtimepriority厶ready queue:c56a34d22e21running process:b110block queue:null11:input new process2:dispath3:t ineout4:eventwait5:eventoccurs0:exit2the process 9has finishedcpu t ine:1naneneedtimepriorityre
24、ady queue:a34d22e21running process :c56三block queue:null1:input new process2:dispath3:tineout4:eventljai t5:eventoccurs0:exit2cpu tine:2naneneedtimepriorityready queue:a34d22e21running process:1 ac46_nameready queue:running process:4block queue:nullcpu tine:3nameready queue:aecpu tine:4nameready que
25、ue:priority6421priority421priority6421needtine3322needtine3322needtime322running process:nullrunning process:nullblock queue:null(3) timeout:消耗一个时间片,若之前运行队列中有进程,则该进程所需执行时间减一并回到 就绪队列,否则什么也不发生1:input new process2:dispath3:t ineout4:eventwait5:euentoccurs 0:exit31:input new process 2:dispath3:tineout 4:e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程承揽合作协议书(2025版)
- 简单脚手架承包合同(2025版)
- 四年级数学(三位数乘两位数)计算题专项练习及答案
- 数控机床智能化升级对2025年机械制造行业的转型升级影响报告
- 2025版办公桌环保材料采购与认证合同
- 二零二五年度生态宜居区房地产代理合同
- 2025年度插画师设计聘用合同
- 二零二五年度海洋资源开发合伙协议书
- 2025年度国际股权投资管理合作协议范本
- 二零二五年度新品发布会场地租赁服务协议
- 2025届广西梧州柳州高三第三次模拟考试英语试卷含解析
- 危大工程清单(模板)
- 中国血管性认知障碍诊治指南(2024版)解读
- 手术室毒麻药品管理
- 汽车采购合同
- 2024胃食管反流病指南
- 中华人民共和国文物保护法
- 污水处理合作意向
- Python快速编程入门(第3版) 课件 第3章 流程控制
- 消防电技术规格书
- 2024年全国中学生奥林匹克数学竞赛浙江赛区初赛试题
评论
0/150
提交评论