DOC PREVIEW
Berkeley COMPSCI 162 - Lecture 6 Semaphores, Conditional Variables, Deadlocks

This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 6 Semaphores, Conditional Variables, DeadlocksReview: Definition of MonitorReview: Readers/Writers ProblemReview: Code for a ReaderReview: Code for a WriterSimulation of Readers/Writers solutionSimulation(2)Simulation(3)QuestionsCan we construct Monitors from Semaphores?Construction of Monitors from Semaphores (con’t)Monitor ConclusionC-Language Support for SynchronizationC++ Language Support for SynchronizationC++ Language Support for Synchronization (con’t)Java Language Support for SynchronizationJava Language Support for Synchronization (con’t)Java Language Support for Synchronization (2/2)Summary: Semaphores and Cond. Variables5min BreakTips for Programming in a Project TeamBig ProjectsTechniques for Partitioning TasksCommunicationCoordinationHow to Make it Work?Suggested Documents for You to MaintainTest ContinuouslySlide 29ResourcesStarvation vs DeadlockConditions for DeadlockBridge Crossing ExampleTrain Example (Wormhole-Routed Network)Slide 35Dining Philosopher ProblemFour requirements for DeadlockSummary: DeadlockCS162Operating Systems andSystems ProgrammingLecture 6Semaphores, Conditional Variables, DeadlocksFebruary 7, 2011Ion Stoicahttp://inst.eecs.berkeley.edu/~cs162Lec 1.22/7/11 Ion Stoica CS162 ©UCB Spring 2011Review: 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–Contrast to semaphores: Can’t wait inside critical sectionLec 1.32/7/11 Ion Stoica CS162 ©UCB Spring 2011Review: Readers/Writers Problem•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 timeRRRWLec 1.42/7/11 Ion Stoica CS162 ©UCB Spring 2011Review: Code for a Reader Reader() {// First check self into systemlock.Acquire();while ((AW + WW) > 0) { // Is it safe to read?WR++; // No. Writers existokToRead.wait(&lock); // Sleep on cond varWR--; // No longer waiting}AR++; // Now we are active!lock.release();// Perform actual read-only accessAccessDatabase(ReadOnly);// Now, check out of systemlock.Acquire();AR--; // No longer activeif (AR == 0 && WW > 0) // No other active readersokToWrite.signal(); // Wake up one writerlock.Release();}Why Release the Lock here?Lec 1.52/7/11 Ion Stoica CS162 ©UCB Spring 2011 Writer() {// First check self into systemlock.Acquire();while ((AW + AR) > 0) { // Is it safe to write?WW++; // No. Active users existokToWrite.wait(&lock); // Sleep on cond varWW--; // No longer waiting}AW++; // Now we are active!lock.release();// Perform actual read/write accessAccessDatabase(ReadWrite);// Now, check out of systemlock.Acquire();AW--; // No longer activeif (WW > 0){ // Give priority to writersokToWrite.signal(); // Wake up one writer} else if (WR > 0) { // Otherwise, wake readerokToRead.broadcast(); // Wake all readers}lock.Release();}Why Give priority to writers?Review: Code for a WriterWhy broadcast() here instead of signal()?Lec 1.62/7/11 Ion Stoica CS162 ©UCB Spring 2011Simulation 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 existokToRead.wait(&lock); // Sleep on cond varWR--; // 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–Situation: Locks released–Only AR is non-zeroLec 1.72/7/11 Ion Stoica CS162 ©UCB Spring 2011Simulation(2)•Next, W1 comes along:while ((AW + AR) > 0) { // Is it safe to write?WW++; // No. Active users existokToWrite.wait(&lock); // Sleep on cond varWW--; // 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) // No other active readersokToWrite.signal(); // Wake up one writerLec 1.82/7/11 Ion Stoica CS162 ©UCB Spring 2011Simulation(3)•When writer wakes up, get:AR = 0, WR = 1, AW = 1, WW = 0•Then, when writer finishes:if (WW > 0){ // Give priority to writersokToWrite.signal(); // Wake up one writer} else if (WR > 0) { // Otherwise, wake readerokToRead.broadcast(); // Wake all readers}–Writer wakes up reader, so get:AR = 1, WR = 0, AW = 0, WW = 0•When reader completes, we are finishedLec 1.92/7/11 Ion Stoica CS162 ©UCB Spring 2011Questions•Can readers starve? Consider Reader() entry code:while ((AW + WW) > 0) { // Is it safe to read?WR++; // No. Writers existokToRead.wait(&lock); // Sleep on cond varWR--; // No longer waiting}AR++; // Now we are active!•What if we erase the condition check in Reader exit?AR--; // No longer activeif (AR == 0 && WW > 0) // No other active readersokToWrite.signal(); // Wake up one writer •Further, what if we turn the signal() into broadcast()AR--; // No longer activeokToWrite.broadcast(); // Wake up one writer •Finally, what if we use only one condition variable (call it “okToContinue”) instead of two separate ones?–Both readers and writers sleep on this variable–Must use broadcast() instead of signal()Lec 1.102/7/11 Ion Stoica CS162 ©UCB Spring 2011Can 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() {


View Full Document

Berkeley COMPSCI 162 - Lecture 6 Semaphores, Conditional Variables, Deadlocks

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Download Lecture 6 Semaphores, Conditional Variables, Deadlocks
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 6 Semaphores, Conditional Variables, Deadlocks 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 6 Semaphores, Conditional Variables, Deadlocks 2 2 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?