CS162 Operating Systems and Systems Programming Lecture 6 Semaphores Conditional Variables Deadlocks February 7 2011 Ion Stoica http inst eecs berkeley edu cs162 Review Definition of Monitor Semaphores are confusing because dual purpose Both mutual exclusion and scheduling constraints Cleaner idea Use locks for mutual exclusion and condition variables for scheduling constraints Monitor a lock and zero or more condition variables for managing concurrent access to shared data Lock provides mutual exclusion to shared data Always acquire before accessing shared data structure Always release after finishing with shared data Condition Variable a queue of threads waiting for something inside a critical section Key idea allow sleeping inside critical section by atomically releasing lock at time we go to sleep Can t wait inside critical section Lec 1 2 Contrast to semaphores 2 7 11 Ion Stoica CS162 UCB Spring 2011 Review Readers Writers Problem W R R R Motivation Consider a shared database Two classes of users Readers never modify database Writers read and modify database Is using a single lock on the whole database sufficient Like to have many readers at the same time Only one writer at a time 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 3 Review Code for a Reader 2 7 11 Reader First check self into system lock Acquire while AW WW 0 Is it safe to read WR No Writers exist okToRead wait lock Sleep on cond var WR No longer waiting AR Now we are active lock release Perform actual read only access AccessDatabase ReadOnly Now check out of system lock Acquire AR No longer active if AR 0 WW 0 No other active readers okToWrite signal Wake up one writer lock Release Ion Stoica CS162 UCB Spring 2011 Lec 1 4 Review Code for a Writer Writer First check self into system lock Acquire while AW AR 0 Is it safe to write WW No Active users exist okToWrite wait lock Sleep on cond var WW No longer waiting AW Now we are active lock release Perform actual read write access AccessDatabase ReadWrite Now check out of system lock Acquire AW No longer active if WW 0 Give priority to writers okToWrite signal Wake up one writer else if WR 0 Otherwise wake reader okToRead broadcast Wake all readers lock Release 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 5 Simulation of Readers Writers solution Consider the following sequence of operators R1 R2 W1 R3 AR WR AW WW 0 On entry each reader checks the following while AW WW 0 Is it safe to read WR No Writers exist okToRead wait lock Sleep on cond var WR No longer waiting AR Now we are active First R1 comes along AR 1 WR 0 AW 0 WW 0 Next R2 comes along AR 2 WR 0 AW 0 WW 0 Now readers may take a while to access database 2 7 11 Situation Locks released Only AR is non zero Ion Stoica CS162 UCB Spring 2011 Lec 1 6 Simulation 2 Next W1 comes along while AW AR 0 Is it safe to write WW No Active users exist okToWrite wait lock Sleep on cond var WW No longer waiting AW Can t start because of readers so go to sleep AR 2 WR 0 AW 0 WW 1 Finally R3 comes along AR 2 WR 1 AW 0 WW 1 Now say that R2 finishes before R1 AR 1 WR 1 AW 0 WW 1 Finally last of first two readers R1 finishes and wakes up writer if AR 0 WW 0 okToWrite signal 2 7 11 No other active readers Wake up one writer Ion Stoica CS162 UCB Spring 2011 Lec 1 7 Simulation 3 When writer wakes up get AR 0 WR 1 AW 1 WW 0 Then when writer finishes if WW 0 Give priority to writers okToWrite signal Wake up one writer else if WR 0 Otherwise wake reader okToRead broadcast Wake all readers Writer wakes up reader so get AR 1 WR 0 AW 0 WW 0 When reader completes we are finished 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 8 Questions Can readers starve Consider Reader entry code while AW WW 0 WR okToRead wait lock WR AR Is it safe to read No Writers exist Sleep on cond var No longer waiting AR if AR 0 WW 0 okToWrite signal No longer active No other active readers Wake up one writer AR okToWrite broadcast No longer active Wake up one writer Now we are active What if we erase the condition check in Reader exit Further what if we turn the signal into broadcast Finally what if we use only one condition variable call it okToContinue instead of two separate ones 2 7 11 Both readers and writers sleep on this variable Must use broadcast instead of signal Ion Stoica CS162 UCB Spring 2011 Lec 1 9 Can we construct Monitors from Semaphores Locking aspect is easy Just use a mutex Can we implement condition variables this way Wait semaphore P Signal semaphore V Doesn t work Wait may sleep with lock held Does this work better Wait Lock lock lock Release semaphore P lock Acquire Signal semaphore V No Condition vars have no history semaphores have history 2 7 11 What if thread signals and no one is waiting NO OP What if thread later waits Thread Waits What if thread V s and noone is waiting Increment What if thread later does P Decrement and continue Ion Stoica CS162 UCB Spring 2011 Lec 1 10 Construction of Monitors from Semaphores con t Problem with previous try P and V are commutative result is the same no matter what order they occur Condition variables are NOT commutative Does this fix the problem Wait Lock lock lock Release semaphore P lock Acquire Signal if semaphore queue is not empty semaphore V Not legal to look at contents of semaphore queue There is a race condition signaler can slip in after lock release and before waiter executes semaphore P It is actually possible to do this correctly Complex solution for Hoare scheduling in book with simpler Mesa scheduled solution Can you come up 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 11 Monitor Conclusion Monitors represent the logic of the program Wait if necessary Signal when change something so any waiting threads can proceed Basic structure of monitor based program lock while need to wait condvar wait unlock Check and or update state variables Wait if necessary do something so no need to wait lock condvar signal Check and or update state variables unlock 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 12 C Language Support for Synchronization C language Pretty straightforward synchronization Just make sure you know all the code paths out of a critical section int Rtn lock acquire if exception lock release return errReturnCode lock release return OK 2 7 11 Ion Stoica CS162 UCB Spring 2011 Lec 1 13 C Language Support for Synchronization Languages with exceptions like C Languages that support exceptions are problematic easy to make a non local exit without …
View Full Document
Unlocking...