1. 程式人生 > >PV操作之獨木橋問題

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