您好,欢迎来到小侦探旅游网。
搜索
您的当前位置:首页操作系统实验报告进程状态转换

操作系统实验报告进程状态转换

来源:小侦探旅游网
实验 进程状态转换及其PCB的变化

一、程序流程图:

开始 初始化使用界面 创建进程 空 就绪队列 不空 就绪队列链首进程开始运结束 该进程运行一个时间片 是 在时间片内是否完成 否 撤销该进程 运行指针指向下一个进程

二、使用的数据结构及说明:

{

int P_Id;

//PCB的ID号 //PCB状态

//PCB的所需要的运行时间 //PCB所需要的资源要求 //PCB块的下一个指针

char P_Name[10]; char P_State[10]; int P_Runtime; int P_Requiry;

struct PCB * next ;

//PCB的名称

在本实验中,主要用到的数据结构是PCB的结构,其中PCB的数据结构如下: struct PCB

} ;

其中,P_Id,和P_Name用来标示一个进程,而P_State用来标示进程的五种状态:

Create_state,Ready_state,Block_state,Run_state,Exit_state。P_Runtime标示要完成一个进程所需要的时间。P_Requiry标示一个进程的执行所需要的其他条件,当其他的条件满足,则P_Requiry置1,否则置0。Struct PCB * next 用来指向同一队列中的下一个PCB块。

三、程序源代码:

#include\"stdlib.h\" #include\"stdio.h\" #include\"string.h\"

/********** globle structure and viable ******/ struct PCB {

int P_Id;

//PCB的ID号 //PCB的名称

//PCB状态

//PCB的所需要的运行时间 //PCB所需要的资源要求 //PCB块的下一个指针 //创建状态 //运行状态

char P_Name[10]; char P_State[10]; int P_Runtime; int P_Requiry; } ;

struct PCB * Create_state; struct PCB * Run_state; struct PCB * Ready_state; struct PCB * Block_state; struct PCB * Exit_state; int signal4=0; int signal5=0; {

struct PCB * p,*q; node->next=NULL; if(*head==NULL) {

*head=node; } Else {

p=*head; q=p->next; while(q!=NULL) {

p=q;

//将节点插入队列

q=q->next; }

p->next=node; }

//找到最后的元素位置

//队列不空

//如果队列为空

struct PCB * next ;

//就绪状态 //阻塞状态

//退出状态

//标示进程4的完成状态 //标示进程5的完成状态

void InsertQueue(struct PCB **head,struct PCB *node) /* insert node function */

}

void DeleteQueue(struct PCB **head,struct PCB *node) //撤销进程,从队列中删除元素 { }

void Display_Process(struct PCB * node) {

printf(\"\\n\\nthis process Id is : %d \\n\ printf(\"this process name is : %s \\n\ printf(\"this process state is : on %s \\n \ printf(\"this process Runtime is : %d \\n\

if(node->P_Requiry)

printf(\"this process resource is ready \\n\"); else

printf(\"this process resource is not ready ! \\n\"); }

void DispatchToBlock(struct PCB *node) // /* dispatch to block function*/ {

//调度到阻塞状态的函数

//struct PCB *p=(struct PCB *)malloc(sizeof(struct PCB)); if(!node->P_Requiry) {

strcpy(node->P_State,\"block\");

//插入到阻塞队列

Display_Process(node);

InsertQueue(&Block_state,node); }

//如果所需要的资源没有满足则,调度到阻塞状态

//打印进程状态的元素函数

struct PCB *p,*q; q=*head;

if(*head==NULL||node==NULL) return ; if(*head==node) { } Else {

while(q->next!=p&&q->next!=NULL)

q=q->next; q=p->next; p->next=NULL; }

//如果不是队列的首元素

return;

//如果要删除的元素是队首元素

//如果队列为空,返回

*head=(*head)->next;

}

void DispatchToReady(struct PCB *node) // dispatch to ready state {

//调度到就绪状态的函数 //如果所需的资源满足,则调度

if(node->P_Requiry) { }

void DispatchBlockToReady() //dispatch the process to readyqueue {

//从阻塞状态调度到就绪状态函数

struct PCB*p,*q; q=Block_state; while(q!=NULL) { p=q; q=q->next;

if(signal4&&p->P_Id==4) } }

void Create_Process() { int i; struct PCB *p; char name[10];

//创建进程函数

{

DeleteQueue(&Block_state,p); strcpy(p->P_State,\"ready\");

printf(\"process4 will be in the state of ready!\\n\"); Display_Process(p); } {

DeleteQueue(&Block_state,p); strcpy(p->P_State,\"ready\");

printf(\"process5 will be in the state of ready!\\n\"); Display_Process(p); }

//如果所需要的资源满足

//如果阻塞状态队列不空

strcpy(node->P_State,\"Ready\"); Display_Process(node);

InsertQueue(&Ready_state,node); }

InsertQueue(&Ready_state,p);

if(signal5&&p->P_Id==5)

InsertQueue(&Ready_state,p);

strcpy(name,\"process\"); for(i=1;i<3;i++) {

p=(struct PCB *)malloc(sizeof(struct PCB)); p->P_Id=i; name[7]=i+'0'; name[8]='\\0';

strcpy(p->P_Name,name); strcpy(p->P_State,\"create\"); p->P_Runtime=1; p->P_Requiry=0;

Display_Process(p); sleep(4);

printf(\" \\n process%d will be in the state of Block, waiting the resource ready \\n\\n\ DispatchToBlock(p); }

for(i=3;i<7;i++) {

p=(struct PCB *)malloc(sizeof(struct PCB)); p->P_Id=i; name[7]=i+'0'; name[8]='\\0';

strcpy(p->P_Name,name); strcpy(p->P_State,\"create\");

p->P_Requiry=1; if(i==6) else

p->P_Runtime=1;

Display_Process(p); sleep(4);

printf(\" \\n process%d will be in the state of Ready, waiting to run \\n\\n\ DispatchToReady(p); } }

void display(struct PCB **head) {

struct PCB *p,*q; p=*head;

//打印各个状态队列里进程数目

//在这里个进程6所需要的时间片为2

p->P_Runtime=2;

//创建4个就绪状态的队列

//同时调度到阻塞队列

//所需要的时间片为1

//动态创建2个处于阻塞状态的进程

while(p!=NULL) { sleep(2);

//printf(\"\\n\\n///////////////////////////////////\\n\"); printf(\"\\n\\nthis process Id is : %d \\n\ printf(\"this process name is : %s \\n\ printf(\"this process state is : on %s \\n \ printf(\"this process Runtime is : %d \\n\

if(p->P_Requiry)

printf(\"this process resource is ready \\n\"); else

printf(\"this process resource is not ready ! \\n\"); p=p->next; } }

void Process_Run() {

struct PCB *p,*q; p=Ready_state; q=p;

while(p!=NULL) {

if(p->P_Runtime<=0) break; strcpy(p->P_State,\"running\");

Display_Process(p);

p->P_Runtime=p->P_Runtime-1; sleep(4); if(p->P_Runtime>0) {

printf(\"this process is not finished,will be dispatch to the ready queue!!\\n\"); DeleteQueue(&Ready_state,p);

strcpy(p->P_State,\"ready\"); InsertQueue(&Ready_state,p); Display_Process(p); } Else {

printf(\"\\n\\nProcess%d is finished and will be in the state of exit!\\n\\n\ if(p->P_Id==4) signal4=1; if(p->P_Id==5) signal5=1;

}

if(signal4||signal5)

//执行完成,则跳出,并发送相应的信息

//没有完成,则进入就绪队列

//如果时间片执行完了,则跳出循环

//就绪队列不空则继续执行

//进程运行函数

DispatchBlockToReady();

q=q->next; p=q;

//如果资源满足,则将进程调度到就绪队列

} if(q==NULL)

printf(\"\\nthere is no process ready!\\n STOP Machine!!!\\n\"); }

int main(int argc,char * argv[]) { int i; char c='c';

//界面

printf(\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\n\"); printf(\"...................................Ding Hai bo\\n\"); printf(\"......Press s to start the process.......\\n\"); scanf(\"%c\ while(1) {

if(c=='s')break; scanf(\"%c\ }

Create_Process();

//调用创建进程函数

printf(\"\\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\\n\"); printf(\"\\n>>>>>>> Display the Ready queue >>>>>>>>>>>>>>>\\n\"); sleep(5);

display(&Ready_state); ////////////////显示就绪队列里的进程 printf(\"\\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\\n\");

printf(\"\\n>>>>>>>> Display the Block queue >>>>>>>>>>>>\\n\"); sleep(5);

//显示阻塞队列函数

display(&Block_state); /////////////////////

printf(\"\\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\\n\\n\\n\");

printf(\"\\n>>>>>>>> Now the process start to run >>>>>>>>>>>\\n\"); sleep(5); Process_Run(); }

//调用进程运行函数

//主函数

四、运行结果及说明:

运行结果的截图:

下面的运行结果是程序执行的每一步进程调度的显示,从进程的创建到进程的执行,结束,每一步进程调度都有显示。

五.程序使用说明:

1)输入字符’s’启动程序

2)程序将自动执行,自动创建进程,运行进程和结束进程。 3)程序将自动显示每一步进程的执行状态。

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

Copyright © 2019- xiaozhentang.com 版权所有

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

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