One, the thing to the car through the bridge, in order to ensure safety, as long as there is no car on the axle, allowing one side of the car to cross the bridge, to one side of the car after all, the other side of the car to allow the bridge.
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 Left () {
while (true) {
P (mutex1);
count1++;
if (count1==1) P (wait);
V (MUTEX1);
Cross-bridge;
P (MUTEX1);
count1--;
if (count1==0) V (wait);
V (MUTEX1);
}
}
Process P Right () {
while (true) {
P (mutex2);
count2++;
if (count2==1) P (wait);
V (MUTEX2);
Cross-bridge;
P (MUTEX1);
count2--;
if (count2==0) V (wait);
V (MUTEX2);
}
} <strong>
</strong>
Second, in the question 1 of the bridge, the limit on the deck can be up to K cars through.
Answer 1:
semaphore Wait,mutex1,mutex2,bridge;
Mutex1=mutex2=1;bridge=k;wait=1; int counter1,counter2;
counter1=0;counter2=0; Cobegin Process P East () {process P West () {p (MUTEX1);
P (MUTEX2); count1++;
count2++; if (count1==1) P (wait);
if (count2==1) P (wait); V (MUTEX1);
V (MUTEX2); P (bridge);
P (bridge); {Bridge};
{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
Answer 2:
cobegin
Process P East () { process P West () {
P (bridge1); P (bridge2);
P (MUTEX1); P (MUTEX2);
count1++; count2++;
if (count1==1) P (wait); if (count2==1) P (wait);
V (MUTEX1); V (MUTEX2);
{Bridge}; {Bridge};
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
Three
In question 1 of the Plank Bridge, a group of three vehicles is required to ensure that the left and right are alternately passed through the car in groups.
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 Left () {
while (true) {
P (S1)
P (mutex1);
COUNTU1 + +;
if (countu1==1) & (countd1==0) P (wait);
V (MUTEX1);
Cross-bridge;
V (S2)
P (MUTEX1);
countu1--;
COUNTD1 + +
if ((countu1==0) & (countd1==3))
{countd1=0; V (wait); }
V (MUTEX1);
}
}
Process P Right () {
while (true) {
P (S2)
P (mutex2);
countu2++;
if (countu2==1) & (countd2==0) P (wait);
V (MUTEX2);
Cross-bridge;
V (S1)
P (MUTEX2);
countu2--;
countd2++
if ((countu2==0) & (countd2==3))
{countd2=0; V (wait); }
V (MUTEX2);
}
}
The left bridge is divided into two typical (incomplete):
(1) PL1 Bridge, PL1 Bridge, PL2 Bridge, PL2 Lower Bridge, PL3 Upper Bridge, PL3 Lower Bridge
if the PL1 on the bridge, PL1 down the bridge, COUNTU1 will be reduced to 0, but the countd1 count is 1, can be avoided by the left to execute V (wait), PL2 on the bridge, PL2 down the bridge, COUNTU1 will be reduced to 0,COUNTD1 add to 2, do not execute V (wait) ; PL2 on the bridge, PL2 down the bridge, COUNTU1 will be reduced to 0,COUNTD1 cumulative to 3, so only the left accumulated 3 down the bridge, only once countd1 to 0, and perform v (wait) wake to the right.
(2) PL1 Bridge, PL2 Bridge, PL3 Bridge, PL1 Lower Bridge, PL2 Lower Bridge, PL3 Lower Bridge
PL1,PL2, PL3 on the bridge in turn, at this time Countu1 added to 3, then PL1 down the bridge, countu1 to 2,COUNTD1 to 1, do not perform V (wait), PL2 down the bridge, countu1 to 1,COUNTD1 to 2, do not perform V ( wait); PL3 down the bridge, COUNTU1 to 0,COUNTD1 plus 3, execute V (wait) to wake to the right.
Four, in the question 1 of the bridge, requires the car serial across the road, but when the other side of the bridge, should be able to prevent the other side not on the bridge of the subsequent vehicles, after the bridge on the bridge, the other side of the car began to cross the bridge.
Stop is used when the other party raises a bridge and should be prevented from succeeding the vehicle on the bridge.
semaphore Stop,wait,mutex1,mutex2;
Stop=mutex1=mutex2=1;wait=1;
int counter1,counter2; counter1=0;counter2=0;
Cobegin
Process P East () { process P West () {
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);
{Bridge}; {Bridge};
P (MUTEX1); P (MUTEX2);
count1--; count2--;
if (count1==0) V (wait); if (count2==0) V (wait);
V (MUTEX1); V (MUTEX2);
} }
Coend