您的位置:首页 > 其它

实验二、进程管理

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;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息