实验一 进程管理
【实验目的】
1) 加深对进程概念及进程管理各部分内容的理解。
2) 熟悉进程管理中主要数据结构的设计和进程调度算法、进程控制机构、同步机构、通讯机构的实施。
【实验要求】
调试并运行一个允许n 个进程并发运行的进程管理模拟系统。了解该系统的进程控制、同步及通讯机构,每个进程如何用一个 PCB 表示、其内容的设置;各进程间的同步关系;系统在运行过程中显示各进程的状态和有关参数变化情况的意义。
【实验环境】
具备Windows或MS-DOS操作系统、带有Turbo C 集成环境的PC机。
【实验重点及难点】
重点:理解进程的概念,进程管理中主要数据结构的设计和进程调度算法、进程控制
机构、同步机构、通讯机构的实施。
难点:实验程序的问题描述、实现算法、数据结构。
【实验内容】
一.阅读实验程序
程序代码见【实验例程】。
二.编译实验例程
用Turbo C 编译实验例程。
三.运行程序并对照实验源程序阅读理解实验输出结果的意义。
【实验例程】
#include #define TRUE 1 #define FALSE 0 #define MAXPRI 100 #define NIL -1 struct { int id; char status; int nextwr; int priority; } pcb [3]; struct { int value; int firstwr; } sem[2]; char savearea[3][4],addr; int i,s1,s2,seed, exe=NIL; init() { int j; for (j=0;j<3;j++) { pcb[j].id=j; pcb[j].status='r'; pcb[j].nextwr=NIL; printf(\"\\n process%d priority?\ scanf(\"%d\ pcb[j].priority=i; } sem[0].value=1; sem[0].firstwr=NIL; sem[1].value=1; sem[1].firstwr=NIL; for(i=1;i<3;i++) for(j=0;j<4;j++) savearea[i] [j]='0'; } float random() { int m; if (seed<0) m=-seed; else m=seed; seed=(25173*seed+13849)%65536; return(m/32767.0); } timeint(ad) char ad; { float x; x=random(); if((x<0.33)&&(exe==0))return(FALSE); if((x<0.66)&&(exe==1))return(FALSE); if((x<1.0)&&(exe==2))return(FALSE); savearea[exe][0]=i; savearea[exe][1]=ad; pcb[exe].status='t'; printf(\"times silce interrupt'\\n process%d enter into ready.\\n\ exe=NIL; return(TRUE); } scheduler() { int pd; if ((pd=find())==NIL && exe==NIL) return(NIL); if (pd!=NIL) { if (exe==NIL) { pcb[pd].status='e'; exe=pd; printf(\"process%d is executing.\\n\ } else if(pcb[pd].priority pcb[exe].status='r'; printf(\"process%d enter into ready\\n\ pcb[pd].status='e'; exe=pd; printf(\"process%d is executing\\n\ } } i=savearea[exe][0]; addr=savearea[exe][1]; return(exe); } find() { int j,pd=NIL,w=MAXPRI; for (j=0;j<3;j++) if(pcb[j].status=='r') if(pcb[j].priority w=pcb[j].priority;pd=j; } if (pd==NIL) for(j=0;j<3;j++) if (pcb[j].status=='t') if (pcb[j].priority w=pcb[j].priority;pd=j; } return(pd); } p(se,ad) int se; char ad; { if(--sem[se].value>=0) return(FALSE); block(se); savearea[exe][0]=i; savearea[exe][1]=ad; exe=NIL; return(TRUE); } block(se) int se; { int w; printf(\"process%d is blocked\\n\ pcb[exe].status='w'; pcb[exe].nextwr=NIL; if((w=sem[se].firstwr)==NIL) sem[se].firstwr=exe; else { while(pcb[w].nextwr!=NIL) w=pcb[w].nextwr; pcb[w].nextwr=exe; } } v(se,ad) int se; char ad; { if(++sem[se].value>0) return(FALSE); wakeup(se); savearea[exe][1]=ad; savearea[exe][0]=i; return(TRUE); } wakeup(se) int se; { int w; w=sem[se].firstwr; if(w!=NIL) { sem[se].firstwr=pcb[w].nextwr; pcb[w].status='r'; printf(\"process%d is waken up\\n\ } } process1() { if(addr=='a') goto a1; if(addr=='b') goto b1; if(addr=='c') goto c1; if(addr=='d') goto d1; if(addr=='e') goto e1; if(addr=='f') goto f1; for(i=1;i<6;i++) { printf(\"process1 calls P on the semaphore 1\\n\"); if(p(0,'a')) break; a1: printf(\"process1 is executing in the cretical section 1\\n\"); if(timeint('b')) break; b1: printf(\"s1=%d\\n\ printf(\"process1 calls V on semaphore1 and quit cretical section 1.\\n\"); if(v(0,'c')) break; c1:printf(\"process1 calls P on semaphore1 2.\\n\"); if (p(1,'d')) break; d1:printf(\"process1 is execting creting cretical section 2.\\n\"); if (timeint('e')) break; e1:printf(\"s2=%d\\n\ printf(\"process1 calls V on semaphore2 and quit cretical section2.\\n\"); if(v(1,'f')) break; f1:printf(\"process1 cycle count=%d\\n\ } if(i<6) return; eexit(0); } process2() { if(addr=='a') goto a2; if(addr=='b') goto b2; if(addr=='c') goto c2; if(addr=='d') goto d2; if(addr=='e') goto e2; if(addr=='f') goto f2; for(i=1;i<6;i++) { printf(\"process2 calls P on semaphore2\\n\"); if(p(1,'a')) break; a2: printf(\"process2 is executing in the cretical section2.\\n\"); if(timeint('b')) break; b2: printf(\"s2=%d\\n\ printf(\"process2 calls V on semaphore2 and quit cretical section2.\\n\"); if(v(1,'c')) break; c2:printf(\"process2 calls P on semaphore1.\\n\"); if (p(0,'d')) break; d2:printf(\"process2 is execting creting cretical section1.\\n\"); if (timeint('e')) break; e2:printf(\"s1=%d\\n\ printf(\"process2 calls V on semaphore1 and quit cretical section1.\\n\"); if(v(0,'f')) break; f2:printf(\"process2 cycle count=%d\\n\ } if(i<6) return; eexit(1); } process3() { if(addr=='a') goto a3; if(addr=='b') goto b3; if(addr=='c') goto c3; for(i=1;i<6;i++) { printf(\"process3 calls P on semaphore2.\\n\"); if(p(1,'a')) break; a3: printf(\"process3 is executing on its cretical section.\\n\"); if(timeint('b')) break; b3: printf(\"s2=%d\\n\ printf(\"process3 calls V on semaphore2 and quit cretical section.\\n\"); if(v(1,'c')) break; c3:printf(\"process3 cycle count=%d\\n\ } if(i<6) return; eexit(2); } eexit(n) int n; { pcb[n].status='c'; printf(\"process%d is completed !\\n\ exe=NIL; } main() { int k; printf(\"********** process management ***********\\n\\n\"); init(); printf(\"s1=%d,s2=%d\\n\ printf(\"process1,process2,process3 are all in ready!\\n\"); for( ; ; ) if((k=scheduler())!=NIL) switch(k) { case 0: process1(); break; case 1: process2(); break; case 2: process3(); break; default: printf(\"process identifer error\\n\"); break; } else break; printf(\"s1=%d,s2=%d\\n\ printf(\"\\n *************** END ****************\\n\"); } 【问题与讨论】 1、 系统为进程设置了几种状态?说明这些状态的含义。 2、 采用何种方式来模拟时间片?简要说明实现方法。 3、 在终端窗口下由于输出结果较多,一屏显示不完,如何较好地阅读程序输出? 4、 描述进程管理主控程序的算法(流程图或N-S图)。 5、 描述进程调度程序的算法(流程图或N-S图)。 6、 用一种你所熟悉的程序设计语言改写实验例程,并上机调试运行;其功能应不亚于原实验例程。 在实验报告中完成问题 1、2、3、4、5的讨论说明。第6个问题可以作为课外实验项目(大作业1)。 因篇幅问题不能全部显示,请点此查看更多更全内容