Question

In: Physics

A single-lane bridge connects village A to village B. Farmers in the two villages use this...

A single-lane bridge connects village A to village B. Farmers in the two villages use this bridge to deliver their products to the neighboring town. The bridge can become deadlocked if a farmer from village A and a farmer from village B get on the bridge at the same time. Using semaphores and/or mutex locks (do not be concerned about starvation) implement your solution in Java. In particular, represent village A and village B farmers as separate threads. Once a farmer is on the bridge, the associated thread will sleep for a random period of time, representing traveling across the bridge.

Solutions

Expert Solution

a.

semaphore ok_to_cross = 1;

void enter_bridge() {

P(ok_to_cross);

}

void exit_bridge() {

V(ok_ to_cross);}

b.

/*the correct-direction enter function must be called before getting on

the bridge. The corresponding exit function must be called when the

thread is ready to leave the bridge.*/

monitor bridge {

int num_waiting_north = 0;

int num_waiting_south = 0;

int on_bridge = 0;

condition ok_to_cross;

int prev = 0;

void enter_bridge_north() {

num_waiting_north++;

while (on_bridge ||(prev == 0 && num_waiting_south > 0))

ok_to_cross.wait();

on_bridge=1;

num_waiting_north--;

prev = 0;

}

void exit_bridge_north() {

on_bridge = 0;

ok_to_cross.broadcast();

}

void enter_bridge_south() {

num_waiting_south++;

while (on_bridge ||(prev == 1 && num_waiting_north > 0))

ok_to_cross.wait();

on_bridge=1;

num_waiting_south--;

prev = 1;

}

void exit_bridge_south() {

on_bridge = 0;

ok_to_cross.broadcast();

}

}

Note that this problem can have many alternative solutions. One alternative solution is as follows

(adapted from one student’s homework submission):

Monitor Bridge {

int nWaiting=0, sWaiting=0, sOnBridge=0, nOnBridge=0;

condition north_turn, south_turn;

enterNorth() {

if (sWaiting>0 || sOnBridge>0)

{

nWaiting++;

wait(north_turn);

while (sOnBridge>0)

wait(north_turn);

nWaiting--;

}

nOnBridge++;

if (sWaiting==0)

signal(north_turn);

}

exitNorth() {

nOnBridge--;

if (nOnBridge ==0)

signal(south_turn);

}

enterSouth() {

if (nWaiting>0 || nOnBridge>0)

{

sWaiting++;

wait(south_turn);

while (nOnBridge>0)

wait(south_turn);

sWaiting--;

}

sOnBridge++;

if (nWaiting==0)

signal(south_turn);

}

exitSouth() {

sOnBridge- -;

if(sOnBridge == 0)

signal(north_turn);

}

}


Related Solutions

Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock;...
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class bridge { private final ReentrantLock myLock = new ReentrantLock(); public bridge(){ } public String cross(){ myLock.lock(); try { return "crossing the bridge"; } finally { myLock.unlock(); } } } public class lane extends Thread { int cars; bridge Bridge; public lane(int Cars1, bridge Bridge1) { cars = Cars1; Bridge = Bridge1; } public void run(){ for(int x=0;x //Important clue :...
In the small village in Tirol there are only two farmers, Herbert and Wolfgang, sellingthe milk...
In the small village in Tirol there are only two farmers, Herbert and Wolfgang, sellingthe milk to consumers. The cows in each herd are from the same ancestors and areeating the grass on the same fields, so the farmers are producing a homogeneous product.However, Herbert has chronic back pain, so the production of each additional litre ofmilk is more costly for him than for Wolfgang:CH= 20 + 5QH+Q2HandCW= 20 + 3QW+ 0.5Q2W,whereQHis the output level (in litres) of Herbert andQWis...
A single lane road is designed for a speed of 75kmph having a two-way traffic with...
A single lane road is designed for a speed of 75kmph having a two-way traffic with radius of 350m. Determine the following (use different reaction time for question i, ii & iii). Stopping Sight Distance for plain road. Stopping Sight Distance for ascending and descending gradient of 1/75. Overtaking sight distance. (take interpolated value for ‘a’) Sketch of maximum overtaking zone distance and sign posts. Setback distance for SSD (only plain road) and OSD for curve length of 150m and...
The estimates shown are for a bridge under consideration. Use the B/C ration method at an...
The estimates shown are for a bridge under consideration. Use the B/C ration method at an interest rate of 6% per year to determine which bridge, if either , should be built. East Location West Location Initial Cost, $ 11,000,000 27,000,000 Annual M&O, $/year 100,000 90,000 Benefits $/year 990,000 2,400,000 Disbenefits $/year 120,000 100,000 Life, years infinity infinity
a. Discuss the merits and demerits of using a single bus. b. How will the use...
a. Discuss the merits and demerits of using a single bus. b. How will the use of a mezzanine bus architecture solve this problem? c. Discuss various methods of bus arbitration.
Design a clocked synchronous state machine with two inputs, A and B, and a single output...
Design a clocked synchronous state machine with two inputs, A and B, and a single output Z that is 1 if (1) A had the “different” value at each of the two previous clock ticks, or (2) B has been 1 since the last time that the first condition was true. Otherwise, the output should be 0. Design state assignment using decomposed method. D Use flip-flops to a minimum, and design the next-state logic with a minimal 2-level NAND-NAND circuit....
Consider a clocked synchronous state machine with two inputs, A and B, and a single output...
Consider a clocked synchronous state machine with two inputs, A and B, and a single output Z that is 1 if (1) A had the “same” value at each of the “three“ previous clock ticks, or (2) B has been 1 since the last time that the first condition was true. Otherwise, the output should be 0. * Find the state/output table with the minimum state for this machine and draw the state diagram.
Design a clocked synchronous state machine with two inputs, A and B, and a single output...
Design a clocked synchronous state machine with two inputs, A and B, and a single output Z that is 1 if (1) A had the “different” value at each of the two previous clock ticks, or (2) B has been 1 since the last time that the first condition was true. Otherwise, the output should be 0. Design a State assignment using decomposed process. Use D flip-flops , Design next-state logic using minimal 2-level NAND-NAND. When designing Next-state logic, don't...
Design a clocked synchronous state machine with two inputs, A and B, and a single output...
Design a clocked synchronous state machine with two inputs, A and B, and a single output Z that is 1 if (1) A had the “different” value at each of the two previous clock ticks, or (2) B has been 1 since the last time that the first condition was true. Otherwise, the output should be 0. Design state assignment using decomposed method. D Use flip-flops to a minimum, and design the next-state logic with a minimal 2-level NAND-NAND circuit....
Use the following information to answer Part (a) and (b) Charlie, a single taxpayer, has gross...
Use the following information to answer Part (a) and (b) Charlie, a single taxpayer, has gross income of $115,000. He has following expenses for 2019: - Rent for his apartment: $14,400 - Advertising and maintenance expenses for a rental property he owns in the amount of $1,800. - Moving expenses to change jobs (assume Tyler is not reimbursed for his expenses) : - Cost to move furniture and home furnishings: $3,600 - House-hunting expenses: $800 - Tyler traveled 200 miles...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT