120 likes | 132 Views
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.
E N D
Homework 6 Sarah Diesburg Operating Systems CS 3430
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.
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(); }
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(); }
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(); }
Proving Correctness • Mutual exclusion • All shared variables are locked • e.g., queue • Signal() and Wait() are invoked between lock.Acquire() and lock.Release()
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
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
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()
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
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
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