Unformatted text preview:

Goals for Today Continue with Synchronization Abstractions CS162 Operating Systems and Systems Programming Lecture 5 Monitors and condition variables Readers Writers problem and solution Language Support for Synchronization Tips for Programming in a Project Team Semaphores Conditional Variables February 2 2011 Ion Stoica http inst eecs berkeley edu cs162 Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Many slides generated from lecture notes by Kubiatowicz 2 2 11 Where are we going with synchronization Programs Shared Programs Higherlevel API Locks Semaphores Monitors Send Receive Hardware Load Store Ion Stoica CS162 UCB Spring 2011 Lec 5 2 Review Implementing Locks with test set Simple solution int value 0 Free Acquire while test set value while busy Release value 0 Advantage Disable Ints Test Set Comp Swap Simple Disadvantage Busy wait until previous thread exists critical section We are going to implement various higher level synchronization primitives using atomic operations Example thread T1 in critical section T2 T3 waiting to enter Everything is pretty painful if only atomic primitives are load and store Need to provide primitives useful at user level 2 2 11 Ion Stoica CS162 UCB Spring 2011 CPU T1 T2 T3 T1 T2 T3 T1 T2 T3 Waiting wasting CPU time 2 2 11 Lec 5 3 Page 1 Ion Stoica CS162 UCB Spring 2011 Lec 5 4 Review Better Locks using test set Review Better 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 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 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 T1 T1 CPU T1 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 Example T2 and T3 go to sleep immediately Previous example T2 T3 suspended 2 2 11 Ion Stoica CS162 UCB Spring 2011 T2 go to sleep T3 go to sleep 2 2 11 Lec 5 5 Higher level Primitives than Locks Ion Stoica CS162 UCB Spring 2011 Lec 5 6 Semaphores Semaphores are a kind of generalized lock Synchronization is a way of coordinating multiple concurrent activities that are using shared state First defined by Dijkstra in late 60s Main synchronization primitive used in original UNIX This lecture and the next presents a couple of ways of structuring the sharing Definition a Semaphore has a non negative integer value and supports the following two operations Good primitives and practices important P an atomic operation that waits for semaphore to become positive then decrements it by 1 Since execution is not entirely sequential really hard to find bugs since they happen rarely UNIX is pretty stable now but up until about mid 80s 10 years after started systems running UNIX would crash every week or so concurrency bugs 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 Note that P stands for proberen to test and V stands for verhogen to increment in Dutch 2 2 11 Ion Stoica CS162 UCB Spring 2011 2 2 11 Lec 5 7 Page 2 Ion Stoica CS162 UCB Spring 2011 Lec 5 8 Semaphores Like Integers Except Producer consumer with a bounded buffer Semaphores are like integers except Producer No negative values Only operations allowed are P and V can t read or write value except to set it initially Operations must be atomic Buffer Consumer Problem Definition Producer puts things into a shared buffer Consumer takes them out Need synchronization to coordinate producer consumer Two P s together can t decrement value below zero Similarly thread going to sleep in P won t miss wakeup from V even if they both happen at same time Don t want producer and consumer to have to work in lockstep so put a fixed size buffer between them Need to synchronize access to this buffer Producer needs to wait if buffer is full Consumer needs to wait if buffer is empty Semaphore from railway analogy Here is a semaphore initialized to 2 for resource control Example 1 GCC compiler cpp cc1 cc2 as ld Example 2 Coke machine Producer can put limited number of cokes in machine Consumer can t take cokes out if machine is empty Value 2 Value 0 Value 1 2 2 11 Ion Stoica CS162 UCB Spring 2011 2 2 11 Lec 5 9 Correctness constraints for solution Full Solution to Bounded Buffer Consumer must wait for producer to fill buffers if none full scheduling constraint Producer must wait for consumer to empty buffers if all full scheduling constraint Only one thread can manipulate buffer queue at a time mutual exclusion 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 General rule of thumb Use a separate semaphore for each constraint Semaphore fullBuffers consumer s constraint Semaphore emptyBuffers producer s constraint Semaphore mutex mutual exclusion Ion Stoica CS162 UCB Spring 2011 Lec 5 10 Semaphore fullBuffer 0 Initially no coke Semaphore emptyBuffers numBuffers Initially num empty slots Semaphore mutex 1 No one using machine Correctness Constraints 2 2 11 Ion Stoica CS162 UCB Spring 2011 2 2 11 Lec 5 11 Page 3 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 Ion Stoica CS162 UCB Spring 2011 Lec 5 12 Motivation for Monitors and Condition Variables Discussion about Solution Semaphores are a huge step up just think of trying to do the bounded buffer with only loads and stores Why asymmetry Producer does emptyBuffer P fullBuffer V Consumer does fullBuffer P emptyBuffer V Problem is that semaphores are dual purpose They are used for both mutex and scheduling constraints Example the fact that flipping of P s in bounded buffer gives deadlock is not immediately obvious How do you prove correctness to someone Is order of P s important Yes Can cause


View Full Document

Berkeley COMPSCI 162 - Lecture 5 Semaphores, Conditional Variables

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 5 Semaphores, Conditional Variables 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 5 Semaphores, Conditional Variables 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?