1 / 12

Helicopter Problem: PersonArrives and UpAndAway Procedure using Locks and Condition Variables

This program solves the Helicopter Problem using locks and condition variables to control arrivals and departures of persons in the helicopter. It ensures mutual exclusion, liveness, and fairness.

mstacey
Download Presentation

Helicopter Problem: PersonArrives and UpAndAway Procedure using Locks and Condition Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 6 Sarah Diesburg Operating Systems CS 3430

  2. 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.

  3. 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); okToGo.Signal(&lock); okToGo.Signal(&lock); } lock.Release(); }

  4. 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); okToGo.Signal(&lock); okToGo.Signal(&lock); okToGo.Signal(&lock); } lock.Release(); }

  5. 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); okToGo.Signal(&lock); okToGo.Signal(&lock); okToGo.Signal(&lock); queue = 0; } lock.Release(); }

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

  7. 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

  8. 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

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

  10. Common Pitfalls int queue = 0; condition okToGo = NULL; Lock lock = FREE; PersonArrives() { lock.Acquire(); ++queue; lock.Release(); if (queue < 5) { lock.Acquire(); okToGo.wait(&lock); lock.Release(); } else { lock.Acquire(); UpAndAway(); okToGo.Signal(&lock); okToGo.Signal(&lock); okToGo.Signal(&lock); okToGo.Signal(&lock); queue = 0; lock.Release(); } } Unprotected states Multiple threads may wait here

  11. Kernel Modules • A kernel module is driver code that is dynamically loaded into the driver when it is needed • Also can be unloaded • The kernel is an executable file • Can contain built-in driver code, but makes the executable bigger

  12. Module Commands • insmod – insert a module into the running kernel • rmmod – remove a module from the running kernel • modprobe – inserts a module, along with all other modules it depends on • Only using insmod for our project

More Related