我大概了解了P,V操作什么的。
这个代码打进去,运行时要求打进程1的优先数,这个时候我不知道要打什么啊?
我试了很多种,好似运行结果都一样。
谁能稍微给我讲下这个实验,到底要做啥?
--------------------------------------------------------------------------------------------
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define MAXPRI 100
#define NIL -1
struct {
int id; //进程标示数,id=0,1,2;
char status; //进程状态,可为 e,r,t,w,c;
//(e:执行态;r:高就绪态;t:低就绪态-执行进程因时间片到限而转入;w:等待态;c:完成态)
int nextwr; //等待链指针,指示在同一信号量上等待的下一个进程的标识数。
int priority; //进程优先数。
}pcb[1];
struct {
int value; //信号量值,初值为1;
int firstwr; //等待链首指针,指示该信号量上第一个等待进程的标识数。
}sem[2];
char savearea[1][3],addr;
int i,s1,s2,seed,exe=NIL; // exe:执行进程指针,其值为进程标识数。
// i:用来模拟一个通用寄存器。
init( )
{ int j;
for (j=0;j<1;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=1;i<1;i++)
for (j=0;j<3;j++)
savearea[i][j]='0';
}
float random ( )
{int m;
if (seed<0)
m=-seed;
else m=seed;
seed=(24151*seed+11839)%64416;
return(m/12565.0);
}
timeint(char addr)
{
float x;
x=random();
if ((x<0.11)&&(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]=addr;
pcb[exe].status='t';
printf("Times silce interrupt'\nprocess%d enter into ready.\n", exe+1);
exe=NIL;
return (TRUE);
}
find()
{
int j,pd=NIL, w=MAXPRI;
for (j=0;j<1;j++)
if (pcb[j].status=='r')
if (pcb[j].priority<w){
w=pcb[j].priority; pd=j;
}
if (pd==NIL)
for (j=0; j<1;j++)
if (pcb[j].status=='t')
if (pcb[j].priority<w)
{
w=pcb[j].priority; pd=j;
}
return(pd) ;
}
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 enter into ready\n", exe+1);
}
}
i=savearea[exe][0];
addr=savearea[exe][1];
return (exe);
}
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;
}
}
p(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);
}
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);
}
}
v(int se,char ad)
{
if(++sem[se].value>0) return(FALSE);
wakeup(se);
savearea[exe][1]=ad;
savearea[exe][0]=i;
return(TRUE);
}
eexit(int n)
{
pcb[n].status='c';
printf("process%d is completed!\n", n+1);
exe=NIL;
}
processl()
{
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 semaphore1\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",++s1);
printf("process1 calls V on semaphorel and quit cretical section1\n");
if(v(0,'c'))break;
c1:printf("process1 calls P on esemaphorel 2\n");
if(p(1,'d'))break;
d1:printf("process1 is execting cretical section 2\n");
if(timeint('e'))break;
e1:printf("s2=%d\n",++s2);
printf("process1 calls V on semaphore2 and quit cretical section2\n");
if(v(1,'f'))break;
f1:printf("process1 cycle count=%d\n",i);
}
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 the 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",++s2);
printf("process2 calls V on semaphore2 and quit cretical section2.\n");
if(v(1,'c'))break;
c2:printf("process2 calls P on esemaphore1.\n");
if(p(0,'d'))break;
d2:printf("process2 is execting cretical section1.\n");
if(timeint('e'))break;
e2:printf("s1=%d\n",++s1);
printf("process2 calls V on semaphore1 and quit cretical section1.\n");
if(v(0,'f'))break;
f2:printf("process2 cycle count=%d\n",i);
}
if(i<6) return;
eexit(1);
}
process1()
{
if (addr=='a') goto a1;
if (addr=='b') goto b1;
if (addr=='c') goto c1;
for (i=1;i<6; ++i){
printf("process1,calls P on semaphore2\n");
if(p(1,'a')) break; /// * process 1 is biocked * /
a1: printf("process 1 is execcuting on its cretical section\n");
if(timeint('b')) break;
b1: printf ("s2=%d\n", ++s2);
printf ("process1 calls V on semapore2 and quit cretical section\n");
if(v(1,'c')) break; // * wake up a biocked process * /
c1: printf("process1 cycien count=%d\n",i);
}
if(i<6) return;
eexit(2);
}
main ( )
{
int k;
printf ("* * * * process management * * * * \n\n");
init();
printf("s1=%d, s2=%d\n", s1, s2);
printf("process1, process2, process1 are all in ready ! \n");
for ( ; ;)
if ((k=scheduler())!=NIL)
switch(k)
{
case 0: process1();
break;
case 1: process2();
break;
case 2: process1();
break;
default: printf("process identifer error\n");
break;
}
else break;
printf ("s1=%d, s2=%d\n", s1, s2);
printf ("\n * * * * END * * * * \n");
}
转载于:https://www.cnblogs.com/Ricezhang/articles/3708254.html