IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization April 14, 2000 Instructor Gary Kimura Slides courtesy of Hank Levy.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Lecture 6: Synchronization (II) – Semaphores and Monitors
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CSE 451: Operating Systems Winter 2014 Module 8 Semaphores, Condition Variables, and Monitors Mark Zbikowski Allen Center 476 ©
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
1 CSE451 Basic Synchronization Autumn 2002 Gary Kimura Lecture #7 & 8 October 14 & 16, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CSE 451: Operating Systems Spring 2006 Module 8 Semaphores and Monitors John Zahorjan Allen Center 534.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
© 2013 Gribble, Lazowska, Levy, Zahorjan
CSE451 Basic Synchronization Spring 2001
CS510 Operating System Foundations
Monitors Chapter 7.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization Hank Levy 1.
CSE 451: Operating Systems Autumn 2010 Module 8 Semaphores, Condition Variables, and Monitors Ed Lazowska Allen Center 570.
© 2013 Gribble, Lazowska, Levy, Zahorjan
Chapter 7: Synchronization Examples
Synchronization, Monitors, Deadlocks
More Synchronization Readers and Writers.
CSE 451: Operating Systems Winter 2004 Module 7+ Monitor Supplement
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitors Chapter 7.
Monitor Giving credit where it is due:
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Autumn Module 8 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
Synchronization: Monitors
CSE 153 Design of Operating Systems Winter 19
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Presentation transcript:

IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors Chia-Chi Teng ccteng@byu.edu CTB 265 1

Administrivia No HW this week Next week’s HW Next week’s reading Work on your project Catch up if you’re behind, including me! Next week’s HW Read web server scheduling paper & write up Next week’s reading Chapter 6 & 9, we’ll come back to 7 & 8 later Project #1 demo/presentation in two weeks During lab sections Project #2 intro 4/26/2017

Semaphores Semaphore = a synchronization primitive A semaphore is: higher level of abstraction than locks invented by Dijkstra in 1968, as part of the THE operating system A semaphore is: a variable that is manipulated through two operations, wait and signal wait(sem) (down) block until sem > 0, then subtract 1 from sem and proceed signal(sem) (up) add 1 to sem Do these operations atomically 4/26/2017

Blocking in semaphores Each semaphore has an associated queue of threads when wait(sem) is called by a thread, if sem was “available” (>0), decrement sem and let thread continue if sem was “unavailable” (<=0), place thread on associated queue; run some other thread when signal(sem) is called by a thread if thread(s) are waiting on the associated queue, unblock one place it on the ready queue might as well let the “signaling” thread continue execution otherwise (when no threads are waiting on the sem), increment sem the signal is “remembered” for next time wait(sem) is called Semaphores thus have history 4/26/2017

Abstract implementation wait/down(sem) acquire “real” mutual exclusion if sem is “available” (>0), decrement sem; release “real” mutual exclusion; let thread continue otherwise, place thread on associated (blocked) queue; release “real” mutual exclusion; run some other thread signal/up(sem) if thread(s) are waiting on the associated (blocked) queue, unblock one (place it on the ready queue) if no threads are on the queue, sem is incremented the signal is “remembered” for next time P(sem) is called release “real” mutual exclusion [the “signaling” thread continues execution] 4/26/2017

Two types of semaphores Binary semaphore (aka mutex semaphore) sem is initialized to 1 guarantees mutually exclusive access to resource (e.g., a critical section of code) only one thread/process allowed entry at a time mutex is basically a high level (non-busy-wait) lock, e.g. perl lock Counting semaphore sem is initialized to N N = number of units available represents resources with many (identical) units available allows threads to enter as long as more units are available 4/26/2017

Usage From the programmer’s perspective, wait and signal on a binary semaphore are just like Acquire and Release on a lock wait(sem) . do whatever stuff requires mutual exclusion; could conceivably be a lot of code signal(sem) same lack of programming language support for correct usage Important differences in the underlying implementation, however 4/26/2017

Pressing questions How do you acquire “real” mutual exclusion? Why is this any better than using a spinlock (test-and-set) or disabling interrupts (assuming you’re in the kernel) in lieu of a semaphore? What if some bozo issues an extra signal? What if some bozo forgets to wait? 4/26/2017

Example: Bounded buffer problem AKA “producer/consumer” problem there is a buffer in memory with N entries producer threads insert entries into it (one at a time) consumer threads remove entries from it (one at a time) Threads are concurrent so, we must use synchronization constructs to control access to shared variables describing buffer state head tail 4/26/2017

Bounded buffer using semaphores (both binary and counting) var mutex: semaphore = 1 ;mutual exclusion to shared data empty: semaphore = n ;count of empty buffers (all empty to start) full: semaphore = 0 ;count of full buffers (none full to start) producer: wait(empty) ; one fewer buffer, block if none available wait(mutex) ; get access to pointers <add item to buffer> signal(mutex) ; done with pointers signal(full) ; note one more full buffer Note 1: I have elided all the code concerning which is the first full buffer, which is the last full buffer, etc. Note 2: Try to figure out how to do this without using counting semaphores! consumer: wait(full) ;wait until there’s a full buffer wait(mutex) ;get access to pointers <remove item from buffer> signal(mutex) ; done with pointers signal(empty) ; note there’s an empty buffer <use the item> 4/26/2017

Example: Readers/Writers Description: A single object is shared among several threads/processes Sometimes a thread just reads the object Sometimes a thread updates (writes) the object We can allow multiple readers at a time why? We can only allow one writer at a time 4/26/2017

Readers/Writers using semaphores var mutex: semaphore = 1 ; controls access to readcount wrt: semaphore = 1 ; control entry for a writer or first reader readcount: integer = 0 ; number of active readers writer: wait(wrt) ; any writers or readers? <perform write operation> signal(wrt) ; allow others reader: wait(mutex) ; ensure exclusion readcount++ ; one more reader if readcount == 1 then wait(wrt) ; if we’re the first, sync with writers signal(mutex) <perform read operation> readcount-- ; one fewer reader if readcount == 0 then signal(wrt) ; no more readers, allow a writer 4/26/2017

Readers/Writers notes the first reader blocks on wait(wrt) if there is a writer any other readers will then block on wait(mutex) if a waiting writer exists, the last reader to exit signals the waiting writer can new readers get in while a writer is waiting? when writer exits, if there is both a reader and writer waiting, which one goes next? 4/26/2017

Semaphores vs. Locks Threads that are blocked at the level of program logic are placed on queues, rather than busy-waiting Busy-waiting may be used for the “real” mutual exclusion required to implement wait and signal but these are very short critical sections – totally independent of program logic 4/26/2017

Problems with semaphores (and locks) They can be used to solve any of the traditional synchronization problems, but: semaphores are essentially shared global variables can be accessed from anywhere (bad software engineering) there is no connection between the semaphore and the data being controlled by it used for both critical sections (mutual exclusion) and for coordination (scheduling) no control over their use, no guarantee of proper usage Thus, they are prone to bugs another (better?) approach: use programming language support 4/26/2017

One More Approach: Monitors A monitor is a programming language construct that supports controlled access to shared data synchronization code is added by the compiler why does this help? A monitor encapsulates: shared data structures procedures that operate on the shared data synchronization between concurrent threads that invoke those procedures Data can only be accessed from within the monitor, using the provided procedures protects the data from unstructured access Addresses the key usability issues that arise with semaphores 4/26/2017

A monitor shared data waiting queue of threads trying to enter the monitor at most one thread in monitor at a time operations (methods) 4/26/2017

Monitor facilities “Automatic” mutual exclusion only one thread can be executing inside at any time thus, synchronization is implicitly associated with the monitor – it “comes for free” if a second thread tries to execute a monitor procedure, it blocks until the first has left the monitor more restrictive than semaphores but easier to use (most of the time) But, there’s a problem… 4/26/2017

Example: Bounded Buffer Scenario Produce() Consume() Buffer is empty Now what? 4/26/2017

Example: Bounded Buffer Scenario Produce() Consume() Buffer is empty Now what? 4/26/2017

Condition variables A place to wait; sometimes called a rendezvous point “Required” for monitors So useful they’re often provided even when monitors aren’t available Three operations on condition variables wait(c) release monitor lock, so somebody else can get in wait for somebody else to signal condition thus, condition variables have associated wait queues signal(c) wake up at most one waiting thread if no waiting threads, signal is lost this is different than semaphores: no history! broadcast(c) wake up all waiting threads 4/26/2017

Bounded buffer using (Hoare*) monitors Monitor bounded_buffer { buffer resources[N]; condition not_full, not_empty; produce(resource x) { if (array “resources” is full, determined maybe by a count) wait(not_full); insert “x” in array “resources” signal(not_empty); } consume(resource *x) { if (array “resources” is empty, determined maybe by a count) wait(not_empty); *x = get resource from array “resources” signal(not_full); * Tony Hoare, senior research @ Microsoft 4/26/2017

Runtime system calls for (Hoare) monitors EnterMonitor(m) {guarantee mutual exclusion} ExitMonitor(m) {hit the road, letting someone else run} Wait(c) {step out until condition satisfied} Signal(c) {if someone’s waiting, step out and let him run} 4/26/2017

Bounded buffer using (Hoare) monitors Monitor bounded_buffer { buffer resources[N]; condition not_full, not_empty; procedure add_entry(resource x) { if (array “resources” is full, determined maybe by a count) wait(not_full); insert “x” in array “resources” signal(not_empty); } procedure get_entry(resource *x) { if (array “resources” is empty, determined maybe by a count) wait(not_empty); *x = get resource from array “resources” signal(not_full); EnterMonitor ExitMonitor EnterMonitor ExitMonitor 4/26/2017

There is a subtle issue with that code… Who runs when the signal()/notify() is done and there is a thread waiting on the condition variable? Hoare monitors: signal(c) means run waiter immediately signaller blocks immediately condition guaranteed to hold when waiter runs but, signaller must restore monitor invariants before signalling! cannot leave a mess for the waiter, who will run immediately! Mesa monitors: notify(c) means waiter is made ready, but the notifier continues waiter runs when notifier leaves monitor (or waits) notifier need not restore invariant until it leaves the monitor being woken up is only a hint that something has changed notified condition may no longer hold must recheck conditional case 4/26/2017

while (notReady) wait(c) Hoare vs. Mesa Monitors Hoare monitors: Mesa monitors: Mesa monitors easier to use more efficient fewer switches directly supports broadcast Hoare monitors leave less to chance when wake up, condition guaranteed to be what you expect if (notReady) wait(c) while (notReady) wait(c) 4/26/2017

Runtime system calls for Hoare monitors EnterMonitor(m) {guarantee mutual exclusion} if m occupied, insert caller into queue m else mark as occupied, insert caller into ready queue choose somebody to run ExitMonitor(m) {hit the road, letting someone else run} if queue m is empty, then mark m as unoccupied else move a thread from queue m to the ready queue insert caller in ready queue choose someone to run 4/26/2017

Wait(c) {step out until condition satisfied} if queue m is empty, then mark m as unoccupied else move a thread from queue m to the ready queue put the caller on queue c choose someone to run Signal(c) {if someone’s waiting, step out and let him run} if queue c is empty then put the caller on the ready queue else move a thread from queue c to the ready queue, and put the caller into queue m 4/26/2017

Runtime system calls for Mesa monitors EnterMonitor(m) {guarantee mutual exclusion} … ExitMonitor(m) {hit the road, letting someone else run} Wait(c) {step out until condition satisfied} Notify(c) {if someone’s waiting, give him a shot after I’m done} if queue c is occupied, move one thread from queue c to queue m return to caller 4/26/2017

Broadcast(c) {food fight!} move all threads on queue c onto queue m return to caller 4/26/2017

Monitor Summary Language supports monitors Compiler understands them compiler inserts calls to runtime routines for monitor entry monitor exit signal/notify wait Runtime system implements these routines moves threads on and off queues ensures mutual exclusion! 4/26/2017