1 Semaphores and Monitors: High-level Synchronization Constructs.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS510 Concurrent Systems Introduction to Concurrency.
Experience with Processes and Monitors in Mesa
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Lecture 6: Synchronization (II) – Semaphores and Monitors
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
1 Synchronization via Transactions. 2 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there?
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
CS703 – Advanced Operating Systems
Monitors, Condition Variables, and Readers-Writers
Operating Systems CMPSC 473
CS510 Operating System Foundations
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Anthony D. Joseph and Ion Stoica
Threading And Parallel Programming Constructs
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization: Monitors
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Monitors and Inter-Process Communication
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

1 Semaphores and Monitors: High-level Synchronization Constructs

2 Synchronization Constructs Synchronization  Coordinating execution of multiple threads that share data structures Past few lectures:  Locks: provide mutual exclusion  Condition variables: provide conditional synchronization Today: Historical perspective  Monitors  Alternate high-level language constructs  Semaphores  Introduced by Dijkstra in 1960s  Main synchronization primitives in early operating systems

3 Separate the concerns of mutual exclusion and conditional synchronization What is a monitor?  One lock, and  Zero or more condition variables for managing concurrent access to shared data General approach:  Collect related shared data into an object/module  Define methods for accessing the shared data Monitors first introduced as programming language construct  Calling a method defined in the monitor automatically acquires the lock  Examples: Mesa, Java (synchronized methods) Monitors also define a programming convention  Can be used in any language (C, C++, … ) Introducing Monitors

4 Critical Section: Monitors Basic idea:  Restrict programming model  Permit access to shared variables only within a critical section General program structure  Entry section  “Lock” before entering critical section  Wait if already locked  Key point: synchronization may involve wait  Critical section code  Exit section  “Unlock” when leaving the critical section Object-oriented programming style  Associate a lock with each shared object  Methods that access shared object are critical sections  Acquire/release locks when entering/exiting a method that defines a critical section

5 Remember Condition Variables Locks  Provide mutual exclusion  Support two methods  Lock::Acquire() – wait until lock is free, then grab it  Lock::Release() – release the lock, waking up a waiter, if any Condition variables  Support conditional synchronization  Three operations  Wait(): Release lock; wait for the condition to become true; reacquire lock upon return (Java wait())  Signal(): Wake up a waiter, if any (Java notify())  Broadcast(): Wake up all the waiters (Java notifyAll())  Two semantics for implementation of wait() and signal()  Hoare monitor semantics  Hansen (Mesa) monitor semantics

6 Coke Machine – Example Monitor Class CokeMachine{ … Lock lock; int count = 0; Condition notFull, notEmpty; } Class CokeMachine{ … Lock lock; int count = 0; Condition notFull, notEmpty; } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.signal(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.signal(); lock  release(); } Does the order of aquire/while(){wait} matter? Order of release/signal matter?

7 Every monitor function should start with what?  A. wait  B. signal  C. lock acquire  D. lock release  E. signalAll

8 Hoare Monitors: Semantics Hoare monitor semantics:  Assume thread T1 is waiting on condition x  Assume thread T2 is in the monitor  Assume thread T2 calls x.signal  T2 gives up monitor, T2 blocks!  T1 takes over monitor, runs  T1 gives up monitor  T2 takes over monitor, resumes Example fn1(…) … x.wait // T1 blocks // T1 resumes Lock  release(); fn4(…) … x.signal // T2 blocks T2 resumes

9 Hansen (Mesa) Monitors: Semantics Hansen monitor semantics:  Assume thread T1 waiting on condition x  Assume thread T2 is in the monitor  Assume thread T2 calls x.signal; wake up T1  T2 continues, finishes  When T1 get a chance to run,T1 takes over monitor, runs  T1 finishes, gives up monitor Example: fn1(…) … x.wait // T1 blocks // T1 resumes // T1 finishes fn4(…) … x.signal // T2 continues // T2 finishes

10 Tradeoff Hoare Claims:  Cleaner, good for proofs  When a condition variable is signaled, it does not change  Used in most textbooks …but  Inefficient implementation  Not modular – correctness depends on correct use and implementation of signal Hansen Signal is only a hint that the condition may be true  Need to check condition again before proceeding  Can lead to synchronization bugs Used by most systems (e.g., Java) Benefits:  Efficient implementation  Condition guaranteed to be true once you are out of while ! CokeMachine::Deposit(){ lock  acquire(); if (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); if (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lock  release(); }

11 Problems with Monitors Nested Monitor Calls What happens when one monitor calls into another?  What happens to CokeMachine::lock if thread sleeps in CokeTruck::Unload?  What happens if truck unloader wants a coke? CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } truck->unload(); Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } truck->unload(); Add coke to the machine; count++; notEmpty.signal(); lock  release(); } CokeTruck::Unload(){ lock  acquire(); while (soda.atDoor() != coke) { cokeAvailable.wait(&lock);} Unload soda closest to door; soda.pop(); Signal availability for soda.atDoor(); lock  release(); } CokeTruck::Unload(){ lock  acquire(); while (soda.atDoor() != coke) { cokeAvailable.wait(&lock);} Unload soda closest to door; soda.pop(); Signal availability for soda.atDoor(); lock  release(); }

12 More Monitor Headaches The priority inversion problem Three processes (P1, P2, P3), and P1 & P3 communicate using a monitor M. P3 is the highest priority process, followed by P2 and P1. 1. P1 enters M. 2. P1 is preempted by P2. 3. P2 is preempted by P3. 4. P3 tries to enter the monitor, and waits for the lock. 5. P2 runs again, preventing P3 from running, subverting the priority system. A simple way to avoid this situation is to associate with each monitor the priority of the highest priority process which ever enters that monitor.

13 Other Interesting Topics Exception handling  What if a process waiting in a monitor needs to time out? Naked notify  How do we synchronize with I/O devices that do not grab monitor locks, but can notify condition variables. Butler Lampson and David Redell, “Experience with Processes and Monitors in Mesa.”

14 Semaphores Study these for history and compatibility  Don’t use semaphores in new code A non-negative integer variable with two atomic and isolated operations We assume that a semaphore is fair  No thread t that is blocked on a P() operation remains blocked if the V() operation on the semaphore is invoked infinitely often  In practice, FIFO is mostly used, transforming the set into a queue. Semaphore  P() (Passeren; wait) If sem > 0, then decrement sem by 1 Otherwise “wait” until sem > 0 and then decrement Semaphore  V() (Vrijgeven; signal) Increment sem by 1 Wake up a thread waiting in P() Semaphore  P() (Passeren; wait) If sem > 0, then decrement sem by 1 Otherwise “wait” until sem > 0 and then decrement Semaphore  V() (Vrijgeven; signal) Increment sem by 1 Wake up a thread waiting in P()

15 Important properties of Semaphores Semaphores are non-negative integers The only operations you can use to change the value of a semaphore are P() and V() (except for the initial setup)  P() can block, but V() never blocks Semaphores are used both for  Mutual exclusion, and  Conditional synchronization Two types of semaphores  Binary semaphores: Can either be 0 or 1  General/Counting semaphores: Can take any non-negative value  Binary semaphores are as expressive as general semaphores (given one can implement the other)

16 How many possible values can a binary semaphore take?  A. 0  B. 1  C. 2  D. 3  E. 4

17 Using Semaphores for Mutual Exclusion Use a binary semaphore for mutual exclusion Using Semaphores for producer-consumer with bounded buffer Semaphore = new Semaphore(1); Semaphore  P(); Critical Section; Semaphore  V(); Semaphore  P(); Critical Section; Semaphore  V(); int count; Semaphore mutex; Semaphore fullBuffers; Semaphore emptyBuffers; int count; Semaphore mutex; Semaphore fullBuffers; Semaphore emptyBuffers; Use a separate semaphore for each constraint

18 Coke Machine Example Coke machine as a shared buffer Two types of users  Producer: Restocks the coke machine  Consumer: Removes coke from the machine Requirements  Only a single person can access the machine at any time  If the machine is out of coke, wait until coke is restocked  If machine is full, wait for consumers to drink coke prior to restocking How will we implement this?  How many lock and condition variables do we need?  A. 1 B. 2 C. 3 D. 4 E. 5

19 Revisiting Coke Machine Example Class CokeMachine{ … int count; Semaphore new mutex(1); Semaphores new fullBuffers(0); Semaphores new emptyBuffers(numBuffers); } Class CokeMachine{ … int count; Semaphore new mutex(1); Semaphores new fullBuffers(0); Semaphores new emptyBuffers(numBuffers); } CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } Does the order of P matter?Order of V matter?

20 Implementing Semaphores Semaphore::P() { if (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } else {value--;} } Semaphore::P() { if (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } else {value--;} } Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; } value++; } Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; } value++; } Does this work?

21 Implementing Semaphores Semaphore::P() { while (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } value--; } Semaphore::P() { while (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } value--; } Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; } value++; } Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; } value++; }

22 The Problem with Semaphores CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } Semaphores are used for dual purpose  Mutual exclusion  Conditional synchronization Difficult to read/develop code Waiting for condition is independent of mutual exclusion  Programmer needs to be clever about using semaphores

23 Comparing Semaphores and Monitors CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Deposit(){ emptyBuffers  P(); mutex  P(); Add coke to the machine; count++; mutex  V(); fullBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } CokeMachine::Remove(){ fullBuffers  P(); mutex  P(); Remove coke from to the machine; count--; mutex  V(); emptyBuffers  V(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); } Which is better? A. Semaphore B. Monitors

24 Summary Synchronization  Coordinating execution of multiple threads that share data structures Past lectures:  Locks  provide mutual exclusion  Condition variables  provide conditional synchronization Today:  Semaphores  Introduced by Dijkstra in 1960s  Two types: binary semaphores and counting semaphores  Supports both mutual exclusion and conditional synchronization  Monitors  Separate mutual exclusion and conditional synchronization