实验二、进程管理
2010-11-17 12:34
302 查看
//#include<stdafx.h> #include<stdio.h> #include<iostream> using namespace std; #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];//上下文切换区,存放三个进程在切换时的上下文 char addr;//地址变量 int i,s1,s2,seed,exe=NIL;//s1,s2是共享变量 void 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? ",j+1); scanf("%d",&i); pcb[j].priority=i; } sem[0].value=1; //信号量初始化 sem[0].firstwr=NIL; sem[1].value=1; sem[1].firstwr=NIL; for(i=0;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)%65535; return (float)(m/32767.0); }//产生随机数程序 int timeint(char ad) /* The time slice interrupt*/ { //子调度程序 float x; x = random(); if(x<0.33 && exe==0) return(FALSE); if(x<0.66 && exe==1) return(FALSE); if(x<1 && exe==2) return(FALSE); savearea[exe][0]=i;//用变量I模仿寄存器,作为保存上下文内容之一。 savearea[exe][1]=ad;//被调度程序的地址 pcb[exe].status='t';//进程状态为高就绪态 printf("Times silce interrupt' process%d enter into ready./n",exe+1); exe=NIL;//exe指向正在执行(运行)的进程 return TRUE; } int find()// 先查找高就绪态进程队列中找一进程,若没有低再到就绪态进程队列中找一进程。 { int j,pd=NIL,w=MAXPRI; for(j=0;j<3;j++) if(pcb[j].status=='r') if(pcb[j].priority<w) { 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) { w=pcb[j].priority; pd=j; } return(pd); } int 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",exe+1); } else if(pcb[pd].priority<pcb[exe].priority) { pcb[exe].status='r'; printf("process %d enter into ready/n",exe+1); pcb[pd].status='e'; exe=pd; printf("process %d is executing/n",exe+1); } } i=savearea[exe][0]; addr=savearea[exe][1]; return(exe); } void block(int se) {int w; printf("process %d is blocked/n",exe+1); 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; } } int wait(int se,char ad) //ad为调用进程程序地址 { if(--sem[se].value>=0) return(FALSE); block(se); savearea[exe][0]=i; savearea[exe][1]=ad; exe=NIL; return(TRUE); } void wakeup(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",w+1); } } int signal(int se,char ad) { if(++sem[se].value>0) return(FALSE); wakeup(se); savearea[exe][1]=ad; savearea[exe][0]=i; return(TRUE); } int 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 wait on the semaphore 1/n"); if(wait(0,'a')) break; a1: printf("process1 is call wait on the semphore 1/n"); if(timeint('b')) break; b1: printf("s1=%d/n",++s1); printf("process1 call signal on semphore1 and quit cretical section 1./n"); if(signal(0,'c')) break; c1: printf("process1 call wait on semphore 2./n"); if(wait(1,'d')) break; d1: printf("process1 is execting cretical section 2./n"); if(timeint('e')) break; e1: printf("s2=%d/n",++s2); printf("process1 call signal on semaphore2 and quit cretical section2/n"); if(signal(1,'f')) break; f1: printf("process1 cycle count =%d/n",i); } if(i<6) return 0; exit(0); } int 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 wait on the semaphore 2./n"); if(wait(1,'a')) break; a2: printf("process2 is call wait on the semphore 2./n"); if(timeint('b')) break; b2: printf("s2=%d/n",++s2); printf("process2 call signal on semphore1 and quit cretical section 2./n"); if(signal(1,'c')) break; c2: printf("process2 call wait on semphore 1./n"); if(wait(0,'d')) break; d2: printf("process2 is execting cretical section 1./n"); if(timeint('e')) break; e2: printf("s1=%d/n",++s1); printf("process2 call signal on semaphore2 and quit cretical section1/n"); if(signal(0,'f')) break; f2: printf("process2 cycle count =%d/n",i); } if(i<6) return 0; exit(1); } void 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 wait on the semaphore 2./n"); if(wait(1,'a')) break; a3: printf("process3 is execting cretical section 2./n"); if(timeint('b')) break; b3: printf("s2=%d/n",++s2); printf("process3 call signal on semphore2 and quit cretical section 2./n"); if(signal(1,'c')) break; c3: printf("process3 cycle count =%d/n",i); } if(i<6) return ; exit(2); } int main() {int k; printf("*****process management*******/n/n"); init(); printf("s1=%d, s2=%d/n",s1,s2); 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",s1,s2); printf("/n****end*****/n"); return 0; }
相关文章推荐
- 进程管理之(二)进程的控制实验
- 操作系统-进程管理实验(1)
- 操作系统实验之进程管理
- 蓝点中文Linux2.0 实验十三 进程与作业管理
- 操作系统-进程管理实验(2)
- Linux父子进程对键盘IO的管理 实验一则
- Linux进程管理 进程状态和时钟滴答[哈工大操作系统实验]
- 进程及线程管理实验1
- 实验:管理进程及计划任务
- 操作系统——实验一.进程管理
- Linux之进程控制与管理实验五
- Windows 8 动手实验系列教程 实验5:进程生命周期管理
- 哈工大操作系统实验3—进程管理
- Linux进程管理 (9)实时调度类分析,以及FIFO和RR对比实验
- 操作系统实验一(进程管理)
- 进程及线程管理实验2
- 实验二 Linux基本命令 进程管理
- 操作系统进程调度管理实验【C语言】【源码】【windows版】
- 进程管理之(一) 进程的创建实验
- 操作系统进程管理实验(FC5下)