Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework 4.

Similar presentations


Presentation on theme: "Homework 4."— Presentation transcript:

1 Homework 4

2 Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based solution (in pseudocode) similar to the “Readers-Writers.” Make sure that you vigorously discuss the correctness and pitfalls of your solution.

3 Gas Station Problem There is a common price for gas
Consumers never modify the price – they just pump gas Owners read modify the price We want one owner at a time, but many consumers at the same time

4 Developing the Solution
Global states: activeConsumers = 0; activeOwners = 0; waitingConsumers = 0; waitingOwners = 0; Condition okToPump = NULL; Condition okToChangePrice = NULL; Lock lock = FREE;

5 Sample Code Solution – up to you to prove correctness
Consumer() { lock.Acquire(); while (activeOwners > 0 || waitingOwners > 0) { ++waitingConsumers; okToPump.Wait(&lock); --waitingConsumers; } ++activeConsumers; lock.Release(); // access price and pumps --activeConsumers; if (activeConsumers == 0 && waitingOwners > 0) { okToChangePrice.Signal(&lock); Writer() { lock.Acquire(); while (activeOwners > 0 || activeConsumers > 0) { ++waitingOwners; okToChangePrice.Wait(&lock); --waitingOwners; } ++activeOwners; lock.Release(); // access database --activeOwners; if (waitingOwners > 0) { okToChangePrice.Signal(&lock); } else if (waitingConsumers > 0) { okToPump.Broadcast(&lock);

6 Question #2 Find a creative/funny example of deadlock, and apply various deadlock prevention approaches to the live example. Describe the advantages and disadvantages of each approach.

7 A Tale of Two Fisherman Two fisherman are out fishing, but between them they only have one fishing pole and one bait. If one takes the pole, and the other takes the bait… They deadlock!

8 Solutions Infinite resources No sharing
Buy more poles and bait No sharing They each have their own poles and bait, fish alone Allocate all resources at the beginning They each either get both things or none

9 Solutions Allocate all resources at the beginning
They each either get both things or none semaphore pole= 1 semaphore bait= 1 lock = 1; fisher(int j) { while (TRUE) { P(s); P(pole); P(bait); // fish V(bait); V(pole); V(s); }

10 Solutions Make everyone use the same ordering
Fisherman 1 goes first, then fisherman 2 (round robin)

11 Question 3: Helicopter Problem
A helicopter ride has five seats, and it always carries a full load. Use lock(s) and condition variable(s) to write a procedure PersonArrives(), which is called whenever a person (thread) arrives. Once the load is full, one person (thread) should call UpAndAway(), and five threads should return from PersonArrives(). There should be no undue waiting: the helicopter should depart as soon as it has a full load.

12 Helicopter Problem int queue = 0; condition okToGo = NULL; Lock lock = FREE; PersonArrives() { lock.Acquire(); ++queue; if (queue < 4) { okToGo.wait(&lock); } else { GoGoGo(); queue = 0; okToGo.Signal(&lock); } lock.Release();

13 Helicopter Problem int queue = 0; condition okToGo = NULL; Lock lock = FREE; PersonArrives() { lock.Acquire(); ++queue; if (queue < 5) { okToGo.wait(&lock); } else { UpAndAway();= 0; okToGo.Signal(&lock); } lock.Release();

14 Helicopter Problem int queue = 0; condition okToGo = NULL; Lock lock = FREE; PersonArrives() { lock.Acquire(); ++queue; if (queue < 5) { okToGo.wait(&lock); } else { UpAndAway(); okToGo.Signal(&lock); queue = 0; } lock.Release();

15 Proving Correctness Mutual exclusion All shared variables are locked
e.g., queue Signal() and Wait() are invoked between lock.Acquire() and lock.Release()

16 Proving Correctness Liveness Maximum of 4 threads in okToGo.wait()
Every 5th thread wakes up exactly four threads already waiting in okToGo.wait() Additional threads wait at lock.Acquire() As long as we have 5n threads, every thread will make progress

17 Proving Correctness Fairness
As long as we have FIFO queues at lock.Acquire() The queuing policy at okToGo.wait() does not matter, since all four waiting threads will be awakened As long as we have 5n threads Every thread will get its chance

18 Common Pitfall Wait() requires lock on return No lock.Release()
int queue = 0; condition okToGo = NULL; Lock lock = FREE; PersonArrives() { lock.Acquire(); ++queue; if (queue < 5) { okToGo.wait(&lock); } else { UpAndAway(); okToGo.Signal(&lock); queue = 0; lock.Release(); } Wait() requires lock on return No lock.Release()


Download ppt "Homework 4."

Similar presentations


Ads by Google