CS162 Operating Systems and Systems Programming Lecture 8 Readers Writers Language Support for Synchronization September 23 2009 Prof John Kubiatowicz http inst eecs berkeley edu cs162 Review Implementation of Locks by Disabling Interrupts Key idea maintain a lock variable and impose mutual exclusion only during operations on that variable int value FREE Acquire Release disable interrupts disable interrupts if value BUSY if anyone on wait queue take thread off wait queue put thread on wait queue Place on ready queue Go to sleep else Enable interrupts value FREE else value BUSY enable interrupts enable interrupts 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 2 Review How to Re enable After Sleep In Nachos since ints are disabled when you call sleep Responsibility of the next thread to re enable ints When the sleeping thread wakes up returns to acquire and re enables interrupts Thread AThread B contex disable ints t s w itch sleep sleep return enable ints t contex disable int sleep switch sleep return enable ints 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 3 Review Locks using test set Can we build test set locks without busy waiting Can t entirely but can minimize Idea only busy wait to atomically check lock value int guard 0 int value FREE Acquire Release Short busy wait time Short busy wait time while test set guard while test set guard if anyone on wait queue if value BUSY take thread off wait queue put thread on wait queue Place on ready queue go to sleep guard 0 else else value FREE value BUSY guard 0 guard 0 Note sleep has to be sure to reset the guard variable Why 9 23 09 can t weKubiatowicz do it just before CS162 UCB or Fall just 2009 after the sleep Lec 8 4 Review Semaphores Definition a Semaphore has a non negative integer value and supports the following two operations P an atomic operation that waits for semaphore to become positive then decrements it by 1 Think of this as the wait operation V an atomic operation that increments the semaphore by 1 waking up a waiting P if any This of this as the signal operation Only time can set integer directly is at initialization time Semaphore from railway analogy Here is a semaphore initialized to 2 for resource control Value 0 Value 1 Value 2 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 5 Goals for Today Continue with Synchronization Abstractions Monitors and condition variables Readers Writers problem and solutoin Language Support for Synchronization Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 6 Gagne Many slides Gagne generated from my lecture notes Review Full Solution to Bounded Buffer Semaphore fullBuffer 0 Initially no coke Semaphore emptyBuffers numBuffers Initially num empty slots Semaphore mutex 1 No one using machine Producer item emptyBuffers P mutex P Enqueue item mutex V fullBuffers V Consumer fullBuffers P mutex P item Dequeue mutex V emptyBuffers V return item 9 23 09 Wait until space Wait until buffer free Tell consumers there is more coke Check if there s a coke Wait until machine free tell producer need more Kubiatowicz CS162 UCB Fall 2009 Lec 8 7 Discussion about Solution Why asymmetry Producer does emptyBuffer P fullBuffer V Consumer does fullBuffer P emptyBuffer V Is order of P s important Yes Can cause deadlock Producer item mutex P Wait until buffer free emptyBuffers P Could wait forever Enqueue item mutex V fullBuffers V Tell consumers more coke Is order of V s important No except that it might affect scheduling efficiency What if we have 2 producers or 2 consumers Do we need to change anything 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 8 Motivation for Monitors and Condition Variables Semaphores are a huge step up but They are confusing because they are dual purpose Both mutual exclusion and scheduling constraints Example the fact that flipping of P s in bounded buffer gives deadlock is not immediately obvious Cleaner idea Use locks for mutual exclusion and condition variables for scheduling constraints Definition Monitor a lock and zero or more condition variables for managing concurrent access to shared data Use of Monitors is a programming paradigm Some languages like Java provide monitors in the language The lock provides mutual exclusion to shared data Always acquire before accessing shared data structure Always release after finishing with shared data Lock initiallyKubiatowicz free 9 23 09 CS162 UCB Fall 2009 Lec 8 9 Simple Monitor Example version 1 Here is an infinite synchronized queue Lock lock Queue queue AddToQueue item lock Acquire queue enqueue item lock Release Lock shared data Add item Release Lock RemoveFromQueue lock Acquire Lock shared data item queue dequeue Get next item or null lock Release Release Lock return item Might return null Not very interesting use of Monitor It only uses a lock with no condition variables Cannot put consumer to sleep if no work 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 10 Condition Variables How do we change the RemoveFromQueue routine to wait until something is on the queue Could do this by keeping a count of the number of things on the queue with semaphores but error prone 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 Contrast to semaphores Can t wait inside critical section Operations Wait lock Atomically release lock and go to sleep Re acquire lock later before returning Signal Wake up one waiter if any Broadcast Wake up all waiters Rule Must hold lock when doing condition variable ops In Birrell paper he says can perform signal outside of CS162 UCB Fall 2009 lock IGNOREKubiatowicz HIM this is only an optimization Lec 8 11 9 23 09 Complete Monitor Example with condition variable Here is an infinite synchronized queue Lock lock Condition dataready Queue queue AddToQueue item lock Acquire queue enqueue item dataready signal lock Release Get Lock Add item Signal any waiters Release Lock RemoveFromQueue lock Acquire Get Lock while queue isEmpty dataready wait lock If nothing sleep item queue dequeue Get next item lock Release Release Lock return item 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 12 Mesa vs Hoare monitors Need to be careful about precise definition of signal and wait Consider a piece of our dequeue code while queue isEmpty dataready wait lock If nothing sleep item queue dequeue Get next
View Full Document
Unlocking...