lec07-deadlockx4 - CS162 Operating Systems and Systems...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks February 8, 2012 Anthony D. Joseph and Ion Stoica http://inst.eecs.berkeley.edu/~cs162
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lec 7.2 2/8/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Recap: Synchronization Goals • Mutual exclusion: – Arbitrate access to critical section (e.g., shared data) – Only a single thread in critical section at a given time » If one thread in critical section all other threads that want to enter the critical section need to wait • Scheduling constraint: – A thread waiting for an event to happen in another thread Wait instruction: – Don ʼ t want busy-waiting, so sleep() – Waiting threads are woken up when the condition they are waiting on becomes FALSE Thread 1 Thread 2 wait signal
Background image of page 2
Lec 7.3 2/8/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Recap: Synchronization Primitives • Locks: Implement mutual exclusion – Lock.Acquire(): acquire lock before entering critical section; wait if lock not free – Lock.Release(): release lock after leaving critical section; wake up threads waiting for lock • Semaphores: Like integers with restricted interface – P(): Wait if zero; decrement when becomes non-zero – V(): Increment and wake a sleeping task (if exists) – Use a semaphore for each scheduling constraint and mutex • Monitors: A lock plus one or more condition variables – Condition variable: a queue of threads waiting inside critical section for an event to happen – Use condition variables to implement sched. constraints – Three Operations: Wait() , Signal() , and Broadcast()
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lec 7.4 2/8/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Recap: Monitors • Monitors represent the logic of the program – Wait if necessary – Signal when change something so any waiting threads can proceed • Basic structure of monitor-based program: lock.Acquire() while (need to wait) { condvar.wait(&lock); } lock.Rlease() do something so no need to wait lock.Acquire() condvar.signal(); lock.Release() Check and/or update state variables Wait if necessary (release lock when waiting) Check and/or update state variables
Background image of page 4
Lec 7.5 2/8/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Can 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(); } • Does this work better? Wait(Lock lock) { lock.Release(); semaphore.P(); lock.Acquire(); } Signal() { semaphore.V(); } – No: Condition vars have no history, semaphores have history: » What if thread signals and no one is waiting? NO-OP » What if thread later waits? Thread Waits » What if thread V ʼ s and no one is waiting? Increment » What if thread later does P? Decrement and continue
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lec 7.6 2/8/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Construction of Monitors from Semaphores (con ʼ t) • Problem with previous try: – P and V are commutative – result is the same no matter what order they occur – Condition variables are NOT commutative • Does this fx the problem?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/12/2012 for the course CS 162 taught by Professor Kubiatowicz during the Spring '02 term at University of California, Berkeley.

Page1 / 55

lec07-deadlockx4 - CS162 Operating Systems and Systems...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online