📄 新建 文本文档.txt
字号:
#include<stdio.h>
#include<stdlib.h>
#define MAXPRI 100
#define NIL -1
int s1,s2;
typedef struct messagetp {
int num;
struct messagetp *next;
int pro;
} *message;
struct procb{
int id;
char status;
int nextwr;
int priority;
message mess;
int inum;
char addr;};
struct semaphore{
int value;
int firstwr;
};
struct procb pcb[4];
struct semaphore sem[3];
char addr;
int i,seed,exe;
int style;
void send(int sender,int receiver,int snum)
{
struct messagetp *p,*q;
p=(struct messagetp*)malloc(sizeof( struct messagetp));
p->num=snum;
p->next=NULL;
p->pro=sender;
printf("send message to process%d.",receiver);
printf("process%d already run %d times.",sender,snum);
q=pcb[receiver].mess;
if(q==NULL)
pcb[receiver].mess=p;
else
{
while(q->next!=NULL)
{
q=q->next;
q->next=p;
}
}
}
void receive( int receiver )
{
message p;
p=pcb[receiver].mess;
if(p!=NULL)
while(p!=NULL)
{
printf("\n");
printf(" receive message from process %d .",p->pro);
printf(" process %d is already run %d times.\n",p->pro,p->num);
p=p->next;
}
pcb[receiver].mess=NULL;
}
void init()
{
int j;
for(j=1;j<=3;j++)
{
pcb[j].id=j;
pcb[j].status='r';
pcb[j].nextwr=-1;
printf("process %d priority?",j);
scanf("%d",&i);
pcb[j].priority=i;
pcb[j].mess=NULL;
pcb[j].inum=0;
pcb[j].addr='0';
}
sem[1].value=1;
sem[1].firstwr=-1;
sem[2].value=1;
sem[2].firstwr=-1;
exe=-1;
}
double random()
{
int m;double t;
if(seed<=0)
m=-seed;
else
{ m=seed;
seed=(25173*seed+13849)%65536;
t=m/32767.0;
}return t;
}
int find()
{
int j,pd,w;
pd=-1;
w=MAXPRI;
for(j=1;j<=3;j++)
if(pcb[j].status=='r')
if(pcb[j].priority<w)
{
w=pcb[j].priority;
pd=j;
}
if(pd==-1)
for(j=1;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;
pd=find();
if((pd==-1)&&(exe==-1))
return -1;
else
{
if(pd!=-1)
{
if(exe==-1)
{
pcb[pd].status='e';
exe=pd;
printf("process %d is executing.",exe);
}
else
if(pcb[pd].priority<pcb[exe].priority)
{
pcb[exe].status='r';
printf("process %d enter into ready.",exe);
pcb[pd].status='e';
exe=pd;
printf("process %d is executing.",exe);
}
}
i=pcb[exe].inum;
addr=pcb[exe].addr;
return exe;
}
}
void block(int se)
{
int w;
printf(" process %d is blocked.",exe);
pcb[exe].status='w';
pcb[exe].nextwr=-1;
w=sem[se].firstwr;
if(w==-1)
sem[se].firstwr=exe;
else
while(pcb[w].nextwr!=-1)
{
w=pcb[w].nextwr;
pcb[w].nextwr=exe;
}
}
int p(int se,char ad)
{
sem[se].value--;
if( sem[se].value>=0)
return 0;
else
{
block(se);
pcb[exe].inum=i;
pcb[exe].addr=ad;
exe=-1;
return 1;
}
}
void wakeup(int se)
{
int w;
w=sem[se].firstwr;
if(w!=-1)
{
sem[se].firstwr=pcb[w].nextwr;
pcb[se].status='r';
printf("process %d is waken up." ,w);
}
}
int v(int se ,char ad)
{
sem[se].value++;
if( sem[se].value>0)
return 0;
else
{
wakeup(se);
pcb[exe].inum=i;
pcb[exe].addr=ad;
return 1;
}
}
int timeint(char ad)
{
double x;
x=random();
if((x<0.3)&&(exe==1))
return 0;
else
if((x<0.6)&&(exe==2))
return 0;
else
if((x<1.0)&&(exe==3))
return 0;
else
{
pcb[exe].inum=i;
pcb[exe].addr=ad;
pcb[exe].status='t';
printf("Time silce interrupt.\n");
printf("process %d enter into ready",exe);
exe=-1;
return 1;
}
}
void eexit(int n)
{
pcb[n].status='c';
printf("process %d is completed!",n);
exe=-1;
}
void 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;
while(i<5)
{
receive(1);
printf("process 1 calls P on semaphore 1.");
if(p(1,'a'))
goto stop1;
a1:{printf("process 1 is executing on its cretical section 1 .");
if(timeint('b'))
goto stop1;}
b1:{s1=s1+1;
printf("\ns1=%d\n",s1);
printf("process1 calls V on semaphore 1 and quit cretical section 1.");
if(v(1,'c'))
goto stop1;}
c1:{printf("process 1 calls P on semaphore 2.");
if(p(2,'d'))
goto stop1;}
d1:{printf("process 1 is executing cretical section 2.");
if(timeint('e'))
goto stop1;}
e1:{s2=s2+1;
printf("\ns2=%d\n",s2);
printf("process1 calls V on semaphore 2 and quit cretical section 2.");
if(v(2,'f'))
goto stop1;}
f1:{printf("process 1 cyclen count=%d",i+1);
i=i+1;
send(1,2,i);
send(1,3,i);
}
}
stop1:{if(i<5)
goto end1;
eexit(1);}
end1:;
}
void 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;
while(i<5)
{
receive(2);
printf("process 2 calls P on semaphore 2.");
if(p(2,'a'))
goto stop2;
a2:printf("process 2 is executing on its cretical section 2 .");
if(timeint('b'))
goto stop2;
b2:s2=s2+1;
printf("\ns2=%d\n",s2);
printf("process 2 calls V on semaphore 2 and quit cretical section 2.");
if(v(2,'c'))
goto stop2;
c2:printf("process 2 calls P on semaphore 1.");
if(p(1,'d'))
goto stop2;
d2:printf("process 2 is executing cretical section 1.");
if(timeint('e'))
goto stop2;
e2:s1=s1+1;
printf("\ns1=%d\n",s1);
printf("process 2 calls V on semaphore 1 and quit cretical section 1.");
if(v(1,'f'))
goto stop2;
f2:printf("process 2 cyclen count=%d",i+1);
i=i+1;
send(2,1,i);
send(2,3,i);
}
stop2:if(i<5)
goto end2;
eexit(2);
end2:;
}
void process3()
{
if(addr=='a')
goto a3;
if(addr=='b')
goto b3;
if(addr=='c')
goto c3;
while(i<5)
{
receive(3);
printf("process 3 calls P on semaphore 2.");
if(p(2,'a'))
goto stop3;
a3:printf("process 3 is executing on its cretical section 2 .");
if(timeint('b'))
goto stop3;
b3:s2=s2+1;
printf("\ns2=%d\n",s2);
printf("process 3 calls V on semaphore 2 and quit cretical section 2.");
if(v(2,'c'))
goto stop3;
c3:printf("process 3 cyclen count=%d",i+1);
i=i+1;
send(3,1,i);
send(3,2,i);
}
stop3:if(i<5)
goto end3;
eexit(3);
end3:;
}
/*
void creat(int n)
{
procb a;
a.id=j;
a.status='r';
a.nextwr=-1;
a.priority=n;
a.mess=NULL;
a.inum=0;
a.addr='0';
}
*/
void main()
{
int k;
printf("**********************C.O.S Example one******************************\n");
init();
s1=0;
s2=0;
printf("\ns1=%d,s2=%d\n",s1,s2);
printf("process1,process2,process3 are all in ready!\n");
ll:k=scheduler();
if(k!=-1)
{ switch(k)
{
case 1:{ process1(); goto ll ;break;}
case 2:{ process2(); goto ll ;break;}
case 3:{ process3(); goto ll ;break;}
default:printf("process identifer error.\n");
}
}
printf("\n\n");
printf("\ns1=%d,s2=%d\n",s1,s2);
printf("********************** Completed!! ******************************\n");
getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -