SynchReview - Sample Synchronization Problems CS 414/415,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sample Synchronization Problems CS 414/415, Spring 2001 Emin Gün Sirer 1. You have been hired to coordinate people trying to cross a river. There is only a single boat, capable of holding at most three people. The boat will sink if more than three people board it at a time. Each person is modeled as a separate thread, executing the function below: Person(int location) // location is either 0 or 1; // 0 = left bank, 1 = right bank of the river { ArriveAtBoat(location); BoardBoatAndCrossRiver(location); GetOffOfBoat(location); } Synchronization is to be done using monitors and condition variables in the two procedures ArriveAtBoat and GetOffOfBoat . Provide the code for ArriveAtBoat and GetOffOfBoat . The BoardBoatAndCrossRiver() procedure is not of interest in this problem since it has no role in synchronization. ArriveAtBoat must not return until it safe for the person to cross the river in the given direction (it must guarantee that the boat will not sink, and that no one will step off the pier into the river when the boat is on the opposite bank). GetOffOfBoat is called to indicate that the caller has finished crossing the river; it can take steps to let other people cross the river. Boatlocation = 0; // 0 for left bank, 1 for right Boatcount = 0; ConditionVariable Boatarrived, boatfull; Monitor ArriveAtBoat(int location) { while(true) { boatcount++; if(boatcount < 3) Condition.wait(boatfull); Else Condition.broadcast(boatfull); return; } else { Condition.wait(boatarrived); } } } Monitor GetOffOfBoat(int location) { --boatcount; if(boatcount == 0) { // we arrived on the other side, update boat direction boatlocation = not location; Condition.broadcast(boatarrived); } } 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Note that this solution may lead to starvation. The readers is encouraged to develop a starvation-free solution. 2. Ping and pong are two separate threads executing their respective procedures. The code below is intended to cause them to forever take turns, alternately printing “ping” and “pong” to the screen. The minithread_stop () and minithread_start () routines operate as they do in your project implementations: minithread_stop () blocks the calling thread, and minithread_start () makes a specific thread runnable if that thread has previously been stopped, otherwise its behavior is unpredictable. void ping() { while(true) { minithread_stop(); Printf(“ping is here\n”); minithread_start(pongthread); } } void pong() { while(true) { Printf(“pong is here\n”); minithread_start(pingthread); minithread_stop(); } } a. The code shown above exhibits a wellknown synchronization flaw. Briefly outline a scenario in which this code would fail, and the outcome of that scenario. b. Show how to fix the problem by replacing the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

SynchReview - Sample Synchronization Problems CS 414/415,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online