Unformatted text preview:

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 Readers Writers Language Support for Synchronization September 23 2009 Prof John Kubiatowicz http inst eecs berkeley edu cs162 Acquire Release disable interrupts disable interrupts 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 else Enable interrupts value FREE else value BUSY enable interrupts enable interrupts 9 23 09 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 A Thread B disable ints contex sleep t sleep return enable ints disable int sleep context 9 23 09 switch Kubiatowicz CS162 UCB Fall 2009 Lec 8 2 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 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 switch sleep return enable ints Kubiatowicz CS162 UCB Fall 2009 Note sleep has to be sure to reset the guard variable Why can t we do it just before or just after the sleep Lec 8 3 9 23 09 Kubiatowicz CS162 UCB Fall 2009 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 Goals for Today Continue with Synchronization Abstractions Monitors and condition variables Readers Writers problem and solutoin Language Support for Synchronization 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 my lecture notes by Kubiatowicz Value 0 Value 1 Value 2 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 5 9 23 09 Review Full Solution to Bounded Buffer 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 6 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 Kubiatowicz CS162 UCB Fall 2009 Lec 8 7 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 Simple Monitor Example version 1 Here is an infinite synchronized queue 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 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 Always acquire before accessing shared data structure Always release after finishing with shared data Lock initially free 9 23 09 Kubiatowicz CS162 UCB Fall 2009 Lec 8 9 Condition Variables How do we change the RemoveFromQueue routine to wait until something is on the queue 9 23 09 Kubiatowicz CS162 UCB Fall 2009 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 AddToQueue item lock Acquire queue enqueue item dataready signal lock Release Condition Variable a queue of threads waiting for something inside a 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 Lec 8 11 RemoveFromQueue lock Acquire while queue isEmpty dataready wait lock item queue dequeue lock Release return item Rule Must hold lock when doing condition variable ops Kubiatowicz CS162 UCB Fall 2009 Lec 8 10 Complete Monitor Example with condition variable Here is an infinite synchronized queue Lock lock Condition dataready Queue queue 9 23 09 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 Could do this by keeping a count of the number of things on the queue with semaphores but error prone In Birrell paper he says can perform signal outside of lock IGNORE HIM this is only an optimization Lock shared data Add item Release Lock 9 23 09 Get Lock Add item Signal any waiters Release Lock Get Lock If nothing sleep Get next item Release Lock 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


View Full Document

Berkeley COMPSCI 162 - Lecture 8 Readers-Writers Language Support for Synchronization

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 8 Readers-Writers Language Support for Synchronization and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 8 Readers-Writers Language Support for Synchronization and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?