您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页实验2 进程状态转换及其PCB的变化(优.选)

实验2 进程状态转换及其PCB的变化(优.选)

来源:保捱科技网


实验2 进程状态转换及其PCB的变化

1.目的

自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容 、组织的变化,理解进程与其PCB间的一一对应关系。

2. 内容及要求

1)设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。

2)编写、调试程序。进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择。

3)合理设计与进程PCB相对应的数据结构。PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。

4)设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。

5)代码书写要规范,要适当地加入注释。

6)鼓励在实验中加入新的观点或想法,并加以实现。

7)认真进行预习,完成预习报告。

1 / 26word.

8)实验完成后,要认真总结,完成实验报告。

3.程序流程图

进程的三种基本状态及其转换如下图所示。

2 / 26word.

开始输入要执行的指令创建进程1?Y就绪队列已满?NYN2?N3?N4?Cputime++提示就绪队列已满该进程执行一个时间片后放回就绪队列Y有进程处于运行状态?YN将就绪队列中优先级最高的进程放入运行队列有进程处于运行状态?Y该进程所需执行时间减1,并回到就绪队列Y有进程处于运行状态?Y将该进程放入阻塞队列NN5?N0?Y结束输入事件发生的进程名称提示无运行的进程将该进程放入就绪队列阻塞队列中有该进程?YN提示该进程并未阻塞N提示输入错误

4.数据结构及说明

3 / 26word.

在本实验中,主要的数据结构是PCB的数据结构,具体如下:

struct process{

char name; //进程名称

int needtime; //进程所需要的运行时间

int priority; //进程的优先级

};

5.源程序

#include

#include

#include

struct process{

char name;

int needtime;

4 / 26word.

int priority;

};

struct process readyQueue[5];

struct process run;

struct process blockedQueue[5];

const struct process null={NULL,0,0};

int readyQueueHead=0;

int blockedQueueHead=0;

int cpuState=0;

int cpuTime=0;

void Order(struct process parameter[],int head);//将队列中的进程按优先级排列

int Creat();

void Dispath();

5 / 26word.

int Timeout();

int EventWait();

int EventOccur();

void Order(struct process parameter[],int head){

int k,i;

struct process temp;

for(k=0;kfor(i=0;iif(parameter[i].priority>=parameter[i+1].priority){

temp=parameter[i];

parameter[i]=parameter[i+1];

parameter[i+1]=temp;

}

6 / 26word.

}

}

}

int Creat(){

if(readyQueueHead>=5){

printf(\"The Ready Queue has been full\\n\");

return 0;

}

label1:

printf(\" input new process name(must be a letter): \\n\");

scanf(\"%c\

int k;

for( k=0;k7 / 26word.

if(readyQueue[readyQueueHead].name==readyQueue[k].name||readyQueue[readyQueueHead].name==readyQueue[k].name+32||readyQueue[readyQueueHead].name==readyQueue[k].name-32)

{

printf(\"the process is already exist!\\n\");

goto label1;

}

for( k=0;kif(readyQueue[readyQueueHead].name==blockedQueue[k].name||readyQueue[readyQueueHead].name==blockedQueue[k].name+32||readyQueue[readyQueueHead].name==blockedQueue[k].name-32)

{

printf(\"the process is already exist!\\n\");

goto label1;

}

8 / 26word.

if(readyQueue[readyQueueHead].name==run.name||readyQueue[readyQueueHead].name==run.name+32||readyQueue[readyQueueHead].name==run.name-32)

{

printf(\"the process is already exist!\\n\");

goto label1;

}

printf(\"input needtime (input a int number):\\n\");

label2:

scanf(\"%d\

if(readyQueue[readyQueueHead].needtime<1||readyQueue[readyQueueHead].needtime>100)

{

printf(\"please input the true needtime(1--100)\\n\");

goto label2;

9 / 26word.

}

printf(\" input the priority(1--10): \\n\");

label3:

scanf(\"%d\

if(readyQueue[readyQueueHead].priority<1||readyQueue[readyQueueHead].priority>10)

{

printf(\"please 1--10!\\n\");

goto label3;

}

readyQueueHead++;

Order(readyQueue,readyQueueHead);

return 0;

}

10 / 26word.

void Dispath(){

if (cpuState==0){

readyQueueHead--;

if(readyQueue[readyQueueHead].needtime>0){

Order(readyQueue,readyQueueHead);

run=readyQueue[readyQueueHead];

readyQueue[readyQueueHead]=null;

cpuState=1;

}

else printf(\"no process in the Ready Queue\\n\");

}

else {

Timeout();

11 / 26word.

Dispath();

}

}

int Timeout(){

cpuTime++;

if (run.name==NULL) return 0;

readyQueue[readyQueueHead]=run;

run=null;

cpuState=0;

readyQueue[readyQueueHead].needtime--;

if(readyQueue[readyQueueHead].needtime==0){

printf(\"The process finished\

readyQueue[readyQueueHead]=null;

12 / 26word.

'%c' has

return 0;

}

readyQueueHead++;

Order(readyQueue,readyQueueHead);

return 0;

}

int EventWait(){

if(blockedQueueHead>=5){

printf(\"error:The Blocked Queue has been full\\n\");

return 0;

}

if(cpuState==0){

printf(\"error:no process in CPU\");

13 / 26word.

return 0;

}

run.needtime--;

blockedQueue[blockedQueueHead]=run;

blockedQueueHead++;

run=null;

cpuState=0;

cpuTime++;

printf(\"The process is blocked!\\n\");

return 0;

}

int EventOccur(){

if(readyQueueHead>=5){

14 / 26word.

printf(\"The Ready Queue has been full\\n\");

return 0;

}

printf(\"Please input the process name whose event occured!\\n\");

char name=getchar();

getchar();

int i;

struct process temp;

for(i=0;iif(name==blockedQueue[i].name){

blockedQueueHead--;

readyQueue[readyQueueHead]=blockedQueue[i];

readyQueueHead++;

15 / 26word.

blockedQueue[i]=blockedQueue[blockedQueueHead];

blockedQueue[blockedQueueHead]=null;

Order(readyQueue,readyQueueHead);

printf(\"The process %c is ready!\\n\

return 0;

}

}

if(i==blockedQueueHead){

printf(\"error:This process has not been blocked!\\n\");

}

return 0;

}

int Show(){

16 / 26word.

printf(\"\\nCPU time:%d\\n\

printf(\" name needtime priority\\n\");

printf(\"Ready Queue: \");

int i;

if(readyQueue[0].name!=NULL)

for(i=readyQueueHead;i>0;i--)

printf(\"%c %d %d\\n

\

else printf(\"null\");

printf(\"\\nRunning Process: \");

if(run.name==NULL) printf(\"null\");

else printf(\"%c %d %d\\n

\

printf(\"\\nBlock Queue: \");

17 / 26word.

if(blockedQueue[0].name==NULL) printf(\"null\");

else for(i=blockedQueueHead;i>0;i--)

printf(\"%c %d %d\\n

\y);

}

int main(){

SELECT:

printf(\"\\n\\n1:input

process\\n2:Dispath\\n3:Timeout\\n4:EventWait\\n5:EventOccurs\\n0:exit\\n\");

new

int select=getchar();getchar();

switch(select)

{

case '1':Creat();Show();break;

case '2':Dispath();Show();break;

18 / 26word.

case '3':Timeout();Show();break;

case '4':EventWait();Show();break;

case '5':EventOccur();Show();break;

case '0':exit(0);

default:printf(\"Please select from 0 to 5\\n\");

}

goto SELECT;

return 0;

6.运行结果及其说明

(1)创建进程:按‘1’创建进程,进程被放入就绪队列,并按优先级从高到低 排列。就绪队列最多容纳5个进程,当创建第6个进程时,程序会提示。

19 / 26word.

20 / 26word.

(2)Dispath:按‘2’将就绪队列中的进程转移到运行队列中,如果运行队列中已有进程 则该进程先执行一个时间片,然后回到就绪队列中,最后将新就绪队列中优先级最

21 / 26word.

大的进程放到运行队列中

(3)Timeout:消耗一个时间片,若之前运行队列中有进程,则该进程所需执行时间减一并回到

就绪队列,否则什么也不发生

22 / 26word.

(4)EventWait:当运行队列中有程序时,使用EventWait将该程序转移到阻塞队列

23 / 26word.

(5)EventOccurs:在阻塞队列中选择一个事件发生的进程,将其放入就绪队列中。

24 / 26word.

7.程序使用说明

(1)输入‘1’创建进程

(2)输入‘2’将就绪队列中优先级最大的进程放入运行队列

(3)输入‘3’消耗一个时间片

(4)输入‘4’将正在运行状态的进程放入阻塞队列中

(5)输入‘5’将阻塞的进程放入就绪队列中

最新文件---------------- 仅供参考--------------------已改成word文本

25 / 26word.

--------------------- 方便更改

26 / 26word.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务