用信号量进程同步与互斥

6、

semaphore mutex=A, customer_count=0:
Cobegin
Customeri(){
p(mutex);
取号码,进入队列;
v(mutex);
v(customer_count);
}


serversi(){
while(A){
p(customer_count);
p(mutex);
从队列中取下一个号码;
v(mutex);
为该号码持有者服务;
}
}
Coend

7、

Cobegin
Var x:integer; s:semaphore;
s:=A


Process PA
Var y, z:integer;
Begin
p(s);
x:=A;
y:=0;
if x>=A then y:=y+A;
v(s);
z:=y;
End


Process PB
Var t, u:integer;
Begin
p(s);
x:=0;
t:=0;
if x<A then t:=t+B;
v(s);
u:=t;
End


Coend

 

 

8、

semaphore S1,S2;
S1=1;S2=0;
cobegin
      process P1(){
            begin
            repeat
            P(S1);
            拣白子
            V(S2);
            until false;
           end
        }
      process P2(){
           begin
           repeat
           P(S2);
           拣黑子
           V(S1);
           until false;
           end
       }
coend.

posted @ 2019-04-30 17:48  李健国  阅读(260)  评论(0编辑  收藏  举报