Review Implementation of Locks by Disabling Interrupts Key idea maintain a lock variable and impose mutual exclusion only during operations on that variable CS162 Operating Systems and Systems Programming Lecture 8 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 Readers Writers Language Support for Synchronization Friday 11 2010 Ion Stoica http inst eecs berkeley edu cs162 2 11 10 Review Implementation of Locks by Disabling Interrupts Thread A Thread B Acquire disable interrupts if value BUSY else value BUSY enable interrupts 2 11 10 Thread A Thread B Release disable interrupts if anyone on wait queue take thread off wait queue Place on ready queue else value BUSY CS162 UCB Fall 2009 Lec 8 2 Review Implementation of Locks by Disabling Interrupts value FREE Acquire disable interrupts if value BUSY put thread on wait queue Go to sleep Wait Enable interrupts Queue enable interrupts CS162 UCB Fall 2009 Release disable interrupts if anyone on wait queue else value FREE value FREE enable interrupts 2 11 10 Page 1 Thread B enable interrupts Thread B Lec 8 3 Ready Queue CS162 UCB Fall 2009 Lec 8 4 Review How to Re enable After Sleep In Nachos since ints are disabled when you call sleep Review Locks using test set Can we build test set locks without busy waiting Responsibility of the next thread to re enable ints When the sleeping thread wakes up returns to acquire and re enables interrupts Thread A Thread B Can t entirely but can minimize Idea only busy wait to atomically check lock value int guard 0 int value FREE disable ints context sleep Release Acquire 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 switch sleep return enable ints 2 11 10 sleep return enable ints disable int sleep context switch CS162 UCB Fall 2009 Why can t we do it just before or just after the sleep 2 11 10 Lec 8 5 Review Semaphores Definition a Semaphore has a non negative integer value and supports the following two operations CS162 UCB Fall 2009 Lec 8 6 Goals for Today Continue with Synchronization Abstractions Monitors and condition variables P an atomic operation that waits for semaphore to become positive then decrements it by 1 Readers Writers problem and solution Language Support for Synchronization 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 Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Gagne Many slides generated from lecture notes by Kubiatowicz Value 0 Value 1 Value 2 2 11 10 CS162 UCB Fall 2009 2 11 10 Lec 8 7 Page 2 CS162 UCB Fall 2009 Lec 8 8 Review Full Solution to Bounded Buffer Discussion about Solution 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 2 11 10 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 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 CS162 UCB Fall 2009 2 11 10 Lec 8 9 Motivation for Monitors and Condition Variables Semaphores are a huge step up but Lock lock Queue queue Both mutual exclusion and scheduling constraints Example the fact that flipping of P s in bounded buffer gives deadlock is not immediately obvious AddToQueue item lock Acquire queue enqueue item lock Release 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 Lock shared data Add item Release Lock RemoveFromQueue lock Acquire item queue dequeue lock Release return item Use of Monitors is a programming paradigm Some languages like Java provide monitors in the language The lock provides mutual exclusion to shared data Lock shared data Get next item or null Release Lock 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 Always acquire before accessing shared data structure Always release after finishing with shared data Lock initially free CS162 UCB Fall 2009 Lec 8 10 Simple Monitor Example version 1 Here is an infinite synchronized queue They are confusing because they are dual purpose 2 11 10 CS162 UCB Fall 2009 2 11 10 Lec 8 11 Page 3 CS162 UCB Fall 2009 Lec 8 12 Condition Variables Complete Monitor Example with condition variable Here is an infinite synchronized queue How do we change the RemoveFromQueue routine to wait until something is on the queue Lock lock Condition dataready Queue 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 AddToQueue item lock Acquire queue enqueue item dataready signal lock Release 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 RemoveFromQueue lock Acquire while queue isEmpty dataready wait lock item
View Full Document
Unlocking...