PV操作之独木桥问题

一、东西向汽车过独木桥,为了保证安全,只要桥上无车,则允许一方的汽车过桥,待一方的汽车全部过完后,另一方的汽车才允许过桥。

semaphore wait,mutex1,mutex2;
mutex1=mutex2=1;wait=1; bridge=1;
int counter1,counter2; counter1=0;counter2=0;
semaphore S1,S2;S1=3;S2=0;

process P左() {
while(true) {
       P(mutex1);  
          count1++; 
          if (count1==1) P(wait); 
       V(mutex1);   
          过独木桥;
P(mutex1); 
  count1--;
          if(count1==0) V(wait); 
        V(mutex1); 
    }
} 	
process P右() {
while(true) {
       P(mutex2); 
          count2++; 
          if (count2==1) P(wait); 
       V(mutex2); 
          过独木桥;
P(mutex1); 
         count2--;  
         if(count2==0) V(wait); 
       V(mutex2); 
    }
} <strong>
</strong>

二、在独木桥问题1中,限制桥面上最多可以有k辆汽车通过。

答1: 
semaphore wait,mutex1,mutex2,bridge;
  mutex1=mutex2=1;bridge=k;wait=1;
  int counter1,counter2; counter1=0;counter2=0;
cobegin
process P东( ) {                       process P西( ) {
       P(mutex1);                           P(mutex2);
       count1++;                            count2++;
       if (count1==1)  P(wait);               if (count2==1)  P(wait);
       V(mutex1);                           V(mutex2);
       P(bridge);                            P(bridge);
       {过桥};                               {过桥};    
       V(bridge);                             V(bridge);        
       P(mutex1);                            P(mutex2);
       count1--;                              count2--;           
       if (count1==0)  V(wait);               if (count2==0)  V(wait);
       V(mutex1);                           V(mutex2);
     }                                   }
coend

答2:
cobegin
process P东( ) {                       process P西( ) {
       P(bridge1);                           P(bridge2);
       P(mutex1);                           P(mutex2);
       count1++;                            count2++;
      if (count1==1 )  P(wait);               if (count2==1)  P(wait);
       V(mutex1);                           V(mutex2);
       {过桥};                               {过桥};    
V(bridge1);                           V(bridge2);     
P(mutex1);                            P(mutex2);
count1--;                              count2--;           
if (count1==0) V(wait)                  if (count2==0 ) V(wait);    
V(mutex1);                           V(mutex2);
     }                                    }
coend

三、 在独木桥问题1中,以叁辆汽车为一组,要求保证左方和右方以组为单位交替通过汽车。

semaphore wait,mutex1,mutex2;
mutex1=mutex2=1;wait=1;
int counter1,counter2; counteru1=0; countd1=0; counteru2=0; counterd2=0;
semaphore S1,S2;S1=3;S2=0;

Process P左() {
while(true) {
   P(S1)  
       P(mutex1);  
          countu1 ++; 
          if (countu1==1) & (countd1==0)  P(wait); 
       V(mutex1);   
          过独木桥;
       V(S2)
P(mutex1); 
  countu1--;
  countd1 ++
if ((countu1==0)&(countd1==3) )
{countd1=0; V(wait); }
        V(mutex1); 
    }
} 	
Process P右() {
while(true) {
   P(S2)   
       P(mutex2); 
          countu2++; 
          if (countu2==1) & (countd2==0)  P(wait); 
       V(mutex2); 
          过独木桥;
V(S1)
P(mutex2); 
         countu2--;
 countd2++
if ((countu2==0)&(countd2==3))
{countd2=0; V(wait); }
       V(mutex2); 
    }
} 

左边过桥分为两种典型(不完全):

(1)PL1上桥, PL1下桥, PL2上桥, PL2下桥, PL3上桥, PL3下桥

如果PL1上桥, PL1下桥,countu1会减到0,但是countd1计数还是1,可以避免这时就由左边执行V(wait);PL2上桥, PL2下桥, countu1会减到0,countd1加到2,不执行V(wait);PL2上桥, PL2下桥, countu1会减到0,countd1累计到3,因此只有左边累计有3个下桥之后,才一次性将countd1改为0,并执行V(wait)唤醒右边。

(2)PL1上桥, PL2上桥, PL3上桥, PL1下桥, PL2下桥, PL3下桥

PL1,PL2, PL3依次上桥,此时countu1加到3;然后PL1下桥,countu1减到2,countd1加到1,不执行V(wait);PL2下桥,countu1减到1,countd1加到2,不执行V(wait);PL3下桥,countu1减到0,countd1加到3,执行V(wait) 唤醒右边。


四、 在独木桥问题1中,要求各方向的汽车串行过桥,但当另一方提出过桥时,应能阻止对方未上桥的后继车辆,待桥面上的汽车过完桥后,另一方的汽车开始过桥。

stop用于当另一方提出过桥时,应阻止对方未上桥的后继车辆。

  semaphore stop,wait,mutex1,mutex2;
    stop=mutex1=mutex2=1;wait=1;  
    int counter1,counter2; counter1=0;counter2=0;
cobegin
process P东( ) {                           process P西( ) {
  P(stop);                                P(stop);
       P(mutex1);                             P(mutex2);
       count1++;                              count2++;
     if (count1==1)  P(wait);                  if (count2==1)  P(wait);
V(mutex1);                           V(mutex2);
V(stop);                              V(stop);
{过桥};                              {过桥};    
      P(mutex1);                            P(mutex2);
       Count1--;                              count2--;           
    if (count1==0)  V(wait);                 if (count2==0)  V(wait);
       V(mutex1);                           V(mutex2);
    }                                   }
coend



评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值