操作系统实验报告进程状态转换.pdf
实验 进程状态转换及其PCB的变化、程序流程图:、使用的数据结构及说明:在本实验中,主要用到的数据结构是PCB 的结构,其中 PCB 的数据结构如下: struct PCBint P_ld;char P_Name10;char P_State10;int P_Ru ntime;int P_Requiry; struct PCB *n ext ;l -其中,P_Id,和 P_Name用来标示一个进程,而 P_State 用来标示进程的五种状态:/PCB 的 ID 号/PCB 的名称/PCB 状态/PCB 的所需要的运行时间/PCB 所需要的资源要求/PCB 块的下一个指针Create_state,Ready_state,Block_state,Run_state,Exit_state 。 P_Runtime 标示要完成一个进程所需 要的时间。 P_Requiry 标示一个进程的执行所需要的其他条件, 当其他的条件满足, 则 P_Requiry 置 1,否则置 0。Struct PCB * next用来指向同一队列中的下一个PCB 块。三、程序源代码 :#includestdlib.h #includestdio.h#includestring.h/* globle structure and viable */ struct PCB/PCB 的 ID 号/PCB 的名称int P_Id;char P_Name10;char P_State10;int P_Runtime;while(q!=NULL)int P_Requiry;struct PCB * next ; ;/PCB 状态/PCB 的所需要的运行时间/PCB 所需要的资源要求/PCB 块的下一个指针p=*head; q=p-next;/ 找到最后的元素位置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;/ 创建状态/ 运行状态/ 就绪状态/ 阻塞状态/ 退出状态/ 标示进程/ 标示进程的完成状态的完成状态/* insert node function */void InsertQueue(struct PCB *head,struct PCB *node)struct PCB * p,*q;node-next=NULL;if(*head=NULL)/ 如果队列为空*head=node;Else/ 队列不空p=q;q=q-next;p-next=node;/ 将节点插入队列void DeleteQueue(struct PCB *head,struct PCB *node)struct PCB *p,*q; q=*head;if(*head=NULL|node=NULL) return ;if(*head=node)*head=(*head)-next; return;Elsewhile(q-next!=p&q-next!=NULL)q=q-next;q=p-next; p-next=NULL;void Display_Process(struct PCB * node)/ 撤销进程,从队列中删除元素/ 如果队列为空,返回/ 如果要删除的元素是队首元素/ 如果不是队列的首元素printf(nnthis process Id is printf(thisprocess name is printf(this process stateis printf(this process Runtime is/ 打印进程状态的元素函数: %d n,node-P_Id);: %s n,node-P_Name);: on %s n ,node-P_State);: %d n,node-P_Runtime);if(node-P_Requiry)printf(this process resource is readyelseprintf(this process resource is not ready ! n);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);InsertQueue(&Block_state,node); / 插入到阻塞队列 Display_Process(node);void DispatchToReady(struct PCB *node) / dispatch to ready state / 调度到就绪状态的函数 if(node-P_Requiry) / 如果所需的资源满足,则调度strcpy(node-P_State,Ready);InsertQueue(&Ready_state,node);Display_Process(node);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)/ 如果阻塞状态队列不空/如果所需要的资源满足DeleteQueue(&Block_state,p);strcpy(p-P_State,ready);InsertQueue(&Ready_state,p);printf(process4 will be in the state of ready!n);Display_Process(p);if(signal5&p-P_Id=5)DeleteQueue(&Block_state,p); strcpy(p-P_State,ready);InsertQueue(&Ready_state,p);printf(process5 will be in the state of ready!n);Display_Process(p);void Create_Process()/ 创建进程函数int i;struct PCB *p;char name10;strcpy(name,process);for(i=1;iP_Id=i;name7=i+0;name8=0; strcpy(p-P_Name,name);strcpy(p-P_State,create);p-P_Runtime=1; / 所需要的时间片为 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 nn,i);DispatchToBlock(p); / 同时调度到阻塞队列for(i=3;iP_Id=i;name7=i+0;name8=0;strcpy(p-P_Name,name); strcpy(p-P_State,create);p-P_Requiry=1;if(i=6)p-P_Runtime=2;elsep-P_Runtime=1;Display_Process(p);sleep(4);printf( n process%d will be in the state of Ready, waiting to run nn,i);DispatchToReady(p);/ 在这里个进程 6void display(struct PCB *head)struct PCB *p,*q; p=*head;while(p!=NULL)sleep(2);/printf(nn/n);printf(nnthis process Id isprintf(this process name isprintf(this process state isprintf(this process Runtime isif(p-P_Requiry)printf(this process resource is readyelseprintf(this process resource is not ready ! n); p=p-next;: %d n,p-P_Id);: %s n,p-P_Name);: on %s n ,p-P_State);: %d n,p-P_Runtime);n);void Process_Run()/ 进程运行函数struct PCB *p,*q;p=Ready_state;q=p;while(p!=NULL)/ 就绪队列不空则继续执行/ 如果时间片执行完了,则跳出循环if(p-P_RuntimeP_State,running);Display_Process(p);p-P_Runtime=p-P_Runtime-1;sleep(4);if(p-P_Runtime0)/ 没有完成,则进入就绪队列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(nnProcess%d is finished and will be in the state of exit!nn,p-P_Id);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(printf( .Press s to start the process. n);scanf(%c,&c);while(1)Ding Hai bon);if(c=s)break; scanf(%c,&c);Create_Process(); / 调用创建进程函数printf(nn); printf(n Displaythe Ready queue n); sleep(5);display(&Ready_state); / 显示就绪队列里的进程printf(nn);printf(n Display the Block queuen);sleep(5); / 显示阻塞队列函数 display(&Block_state); /printf(nnnn);printf(n Now the process start to runsleep(5);Process_Run(); / 调用进程运行函数n);四、运行结果及说明:运行结果的截图: 下面的运行结果是程序执行的每一步进程调度的显示,从进程的创建到进程的执行,结束, 每一步进程调度都有显示。sthis process Id is1this process name isprocesslthis process state ison createthis process Runtime is:1this process resource is not ready !processl will be in the state of Block, waiting the resource readythis processId is:1this processname is:processlthis processstate is:on blockthis process Runtime Is : 1this process resource is not ready !this process Id is this process:2name is this process state is:process2this process Runtime this:on createprocess resourceis : 1 is not ready !this processId is: 2this processname Is: process2this processstate is: on blockthis process Runtime is : 1this process resource is not ready !this process Id is this processname Is this process state is this:process3 :onprocess Runtime iscreatethis process resource 1$:1readyprocess3 will be In thestate of Ready, waiting to runthis process Id is this processname is this process state is this:process3 :onprocess Runtime Is this processReady :1 readyresource isthis process Id is this processname is this process state is thisprocess Runtime Is this processresource is:4:process4:on create:1readyfile Edit View Tfeirninal labs Helpthis process Id isthis process name 15this process state isthis process Runtime isthis process resource is:4:process4:on Redy:1readythis process Id isthis process name isthis process state Isthis process Runtime isthis process resoiurce isprocess5 will be In the:5:processB:on Create:1readystate of Rsadywaiting to runthis process Id isthis procsssi name isthis process state Isthis process Runtime isthis process resoiurce is:5:processi on Ready:1readythis process Id isthis process name isthis process state 1&:6:processB:on createthis process Runtime is : 2 this process resource is ready五程序使用说明:1) 输入字符启动程序2)程序将自动执行,自动创建进程,运行进程和结束进程3)程序将自动显示每一步进程的执行状态。