CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
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.
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
Higher Level Mechanisms for Building Critical Sections zSemaphores yVery primitive (Specifying direct start and stop of threads) ySimple (Hard to program.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
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
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CSE 451: Operating Systems Winter 2014 Module 8 Semaphores, Condition Variables, and Monitors Mark Zbikowski Allen Center 476 ©
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.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
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
CS703 – Advanced Operating Systems
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
Chapter 5: Process Synchronization – Part II
CS510 Operating System Foundations
CSE 120 Principles of Operating
© 2013 Gribble, Lazowska, Levy, Zahorjan
Chapter 6-7: Process Synchronization
CSE451 Basic Synchronization Spring 2001
CS510 Operating System Foundations
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
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
Monitor Giving credit where it is due:
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:

CSE 451: Operating Systems Autumn 2004 Lecture 7 Semaphores and Monitors Hank Levy levy@cs.washington.edu Allen Center 596 1

Semaphores semaphore = a synchronization primitive A semaphore is: higher level than locks invented by Dijkstra in 1968, as part of the THE os A semaphore is: a variable that is manipulated atomically through two operations, signal and wait wait(semaphore): decrement, block until semaphore is open also called P(), after Dutch word for test, also called down() signal(semaphore): increment, allow another to enter also called V(), after Dutch word for increment, also called up() 2/25/2019 © 2003 Hank Levy

Blocking in Semaphores Each semaphore has an associated queue of processes/threads when wait() is called by a thread, if semaphore is “available”, thread continues if semaphore is “unavailable”, thread blocks, waits on queue signal() opens the semaphore if thread(s) are waiting on a queue, one thread is unblocked if no threads are on the queue, the signal is remembered for next time a wait() is called In other words, semaphore has history this history is a counter if counter falls below 0 (after decrement), then the semaphore is closed wait decrements counter signal increments counter 2/25/2019 © 2003 Hank Levy

Hypothetical Implementation type semaphore = record value: integer: L: list of processes; end wait(S): S.value = S.value - 1; if S.value < 0 then begin add this process to S.L; block; end; signal(S): S.value = S.value + 1; if S.value <= 0 remove a process P from S.L; wakeup P wait()/signal() are critical sections! Hence, they must be executed atomically with respect to each other. 2/25/2019 © 2003 Hank Levy

Two types of semaphores Binary semaphore (aka mutex semaphore) guarantees mutually exclusive access to resource only one thread/process allowed entry at a time counter is initialized to 1 Counting semaphore (aka counted semaphore) represents a resources with many units available allows threads/process to enter as long as more units are available counter is initialized to N N = number of units available 2/25/2019 © 2003 Hank Levy

Usage From the programmer’s perspective, P and V on a binary semaphore are just like Acquire and Release on a lock P(sem) . do whatever stuff requires mutual exclusion; could conceivably be a lot of code V(sem) same lack of programming language support for correct usage Important differences in the underlying implementation, however 2/25/2019 © 2003 Hank Levy

Example: bounded buffer problem AKA producer/consumer problem there is a buffer in memory with finite size N entries a producer process inserts an entry into it a consumer process removes an entry from it Processes are concurrent so, we must use synchronization constructs to control access to shared variables describing buffer state 2/25/2019 © 2003 Hank Levy

Bounded Buffer using Semaphores 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 consumer: wait(full) ;wait until there’s a full buffer wait(mutex) ;get access to pointers <remove item from buffer> signal(empty) ; note there’s an empty buffer <use the item> 2/25/2019 © 2003 Hank Levy

Example: Readers/Writers Basic problem: object is shared among several processes some read from it others write to it We can allow multiple readers at a time why? We can only allow one writer at a time 2/25/2019 © 2003 Hank Levy

Readers/Writers using Semaphores var mutex: semaphore ; controls access to readcount wrt: semaphore ; control entry to a writer or first reader readcount: integer ; number of readers write process: wait(wrt) ; any writers or readers? <perform write operation> signal(wrt) ; allow others read process: wait(mutex) ; ensure exclusion readcount = readcount + 1 ; one more reader if readcount = 1 then wait(wrt) ; if we’re the first, synch with writers signal(mutex) <perform reading> readcount = readcount - 1 ; one fewer reader if readcount = 0 then signal(wrt) ; no more readers, allow a writer 2/25/2019 © 2003 Hank Levy

Readers/Writers notes the first reader blocks if there is a writer any other readers will then block on mutex if a writer exists, last reader to exit signals waiting writer can new readers get in while writer is waiting? when writer exits, if there is both a reader and writer waiting, which one goes next is up to scheduler 2/25/2019 © 2003 Hank Levy

Problems with Semaphores 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 2/25/2019 © 2003 Hank Levy

Monitors A programming language construct that supports controlled access to shared data synchronization code added by compiler, enforced at runtime why does this help? Monitor is a software module that encapsulates: shared data structures procedures that operate on the shared data synchronization between concurrent processes that invoke those procedures Monitor protects the data from unstructured access guarantees only access data through procedures, hence in legitimate ways 2/25/2019 © 2003 Hank Levy

A monitor shared data waiting queue of processes trying to enter the monitor at most one process in monitor at a time operations (procedures) 2/25/2019 © 2003 Hank Levy

Monitor facilities Mutual exclusion only one process can be executing inside at any time thus, synchronization implicitly associated with monitor if a second process tries to enter a monitor procedure, it blocks until the first has left the monitor more restrictive than semaphores! but easier to use most of the time Once inside, a process may discover it can’t continue, and may wish to sleep or, allow some other waiting process to continue condition variables provided within monitor processes can wait or signal others to continue condition variable can only be accessed from inside monitor 2/25/2019 © 2003 Hank Levy

Condition Variables A place to wait; sometimes called a rendezvous point 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 wait queues signal(c) wake up at most one waiting process/thread if no waiting processes, signal is lost this is different than semaphores: no history! broadcast(c) wake up all waiting processes/threads 2/25/2019 © 2003 Hank Levy

Bounded Buffer using Monitors Monitor bounded_buffer { buffer resources[N]; condition not_full, not_empty; procedure add_entry(resource x) { while(array “resources” is full) wait(not_full); add “x” to array “resources” signal(not_empty); } procedure get_entry(resource *x) { while (array “resources” is empty) wait(not_empty); *x = get resource from array “resources” signal(not_full); 2/25/2019 © 2003 Hank Levy

Two Kinds of Monitors 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! Mesa monitors: signal(c) means waiter is made ready, but the signaller continues waiter runs when signaller leaves monitor (or waits) condition is not necessarily true when waiter runs again signaller need not restore invariant until it leaves the monitor being woken up is only a hint that something has changed must recheck conditional case 2/25/2019 © 2003 Hank Levy

Examples Hoare monitors Mesa monitors Mesa monitors easier to use if (notReady) wait(c) Mesa monitors while(notReady) 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 2/25/2019 © 2003 Hank Levy

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} 2/25/2019 © 2003 Hank Levy

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 2/25/2019 © 2003 Hank Levy

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 2/25/2019 © 2003 Hank Levy

Runtime system calls (cont.) 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 2/25/2019 © 2003 Hank Levy