Higher Level Mechanisms for Building Critical Sections zSemaphores yVery primitive (Specifying direct start and stop of threads) ySimple (Hard to program.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
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.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Process 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.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
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  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
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.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
OS Winter’03 Concurrency. OS Winter’03 Bakery algorithm of Lamport  Critical section algorithm for any n>1  Each time a process is requesting an entry.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
CIS Operating Systems Synchronization
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Chapter 6: Synchronization Tools
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization Hank Levy 1.
Outline Distributed Mutual Exclusion Introduction Performance measures
Lecture 2 Part 2 Process Synchronization
Critical section problem
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 Lecture 7 Semaphores and Monitors
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 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
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Higher Level Mechanisms for Building Critical Sections zSemaphores yVery primitive (Specifying direct start and stop of threads) ySimple (Hard to program with, but easy to get) zMonitors yHigher level than semaphores (language support). More abstract zMessages ySimple model of communication and synchronization based on atomic transfer of data across a channel yDirect application to distributed systems

Semaphores zAbstract data type with an internal counter that is accessed atomically yP(s) xwait for s.counter > 0; decrement s.counter. yV(s) xincrement s

Binary Semaphores zUsed for mutual exclusion yOnly one process can access a critical section at a time. yTo enter the critical section, P on the semaphore. yWhen leaving the critical section, V on the semaphore. zCounter can take on two values: 0,1 yinitially, count is 1. yP(s) : xif s.count > 0 then s.count = 0 xif s.count == 0 then wait yV(sem): count(sem) = 1;

Counting semaphores zOne “resource” with the mutual exclusion example. ythe resource is the critical section. zWhen you have a lot of resources and you want to hand them out zWant to use COUNTING SEMAPHORES: [0..N] yinitially, count == N. xP(sem): if count(sem) > 0, count(sem) --; if count(sem) == 0 wait; xV(sem): count(sem)++;

Bounded Buffer Semaphore Implementation 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 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 signal(mutex) ; //done with pointers signal(empty) ; //note there’s an empty buffer

Synchronization with Semaphores zSemaphores can be used to solve any of the traditional synchronization problems, but suffer from several problems: 1. semaphores are essentially shared global variables 2. there is no connection between the semaphore and the data being controlled by the semaphore 3. Use same mechanism for scheduling and synchronization. 4. they can be accessed from anywhere in the code 5. there is no control or guarantee of proper usage zSemaphores are sometimes hard to use and prone to bugs. zOne solution is to provide programming language support for synchronization. yWhy does putting something in the language make it harder to misuse?

Monitors zA monitor is a programming language construct that supports controlled access to shared data. zA monitor is a module that encapsulates: 1. some shared data structures 2. procedures that operate on that shared data 3. synchronization between concurrent processes that invoke those procedures zA monitor protects the data from unstructured access. zThe monitor guarantees that processes trying to access the data through its procedures interact only in legitimate ways.

Monitor Facilities zA monitor guarantees mutual exclusion yOnly one process can be executing within monitor at any time yIf a second process tries to enter a monitor procedure, it blocks until the first has left the monitor yMore restrictive than semaphores; easier to use most of the time zOnce in the monitor, a process may discover that it cannot continue, and may wish to sleep, or it may wish to allow a waiting process to continue. yCondition Variables provide synchronization within the monitor so that processes can wait or signal others to continue.

Condition Variables zThe actual logic is provided by the program, not by the condition variable BOOLEAN NotEnoughMilk, MilkInTransit; CONDITION MilkCondition IF (NotEnoughMilk AND MilkInTransit) THEN Condition.Wait(MilkCondition); zOperations on condition variables yCondition.Wait(c) xrelease monitor lock, wait for someone to signal condition yCondition.Signal(c) xwakeup one waiting thread

Basic Monitor Structure resource: monitor begin busy: boolean; free: condition; procedure acquire; begin if busy then free.wait; busy = true; end procedure release; begin busy=false; free.signal; end busy=false ; initialize busy end

Monitors and Semaphores zMonitors and Semaphores can be implemented in terms of each other. E.g., to implement monitors with semaphores, we need: ymutex : a semaphore to control entry to the monitor (initialize to 1) ynext : a semaphore to suspend a process when it signals another (initialize to 0) ynext-count : integer # of processes waiting due to signals yx-sem : a semaphore to suspend a process on a wait (initialized to 0) [one semaphore for each condition] yx-count: integer # of processes waiting due to waiting on condition [one for each condition]

Monitors implemented with Semaphores P(mutex); if next-count > 0 then V(next) else V(mutex); x.wait: x-count:=x-count+1; if next-count>0 then V(next) else V (mutex); P(x-sem); x-count:=xcount-1; x.signal if x-count>0 then begin next-count:=next-count+1; V(x-sem); P(next); next-count:=next-count-1; end; //General entry wrapper for all //operations.

Two kinds of Monitors zHOARE Monitors ySIGNAL(c) xRun waiter immediately. Signaler blocks right now xCondition is guaranteed to hold when blocker runs xBut, signaler must RESTORE MONITOR INVARIANTS before signaling zMESA Monitors ySIGNAL(c) xwaiter is made ready, but the signaler continues xCondition is not necessarily true when the waiter runs again xSignaler must not restore invariant until it leaves the MONITOR xWAKEUP is only a HINT that something must have changed

Examples zHOARE if (NotReady) Condition.Wait(C); zMESA while (NotReady) Condition.Wait(C); zHoare monitor simplifies proof of correctness and leaves less to “chance.” zMESA monitor easier to use ymore efficient xfewer switches xdirectly supports broadcast.

Synchronization with Monitors zThree patterns of deadlock using monitors yInside a single monitor, two processes do a WAIT, each expecting to be awakened by the other - logical error with the monitor yA cyclic calling pattern between two monitors - partial ordering yTwo-level data abstraction (M calls N, N waits for a condition to be set by another process that enters N through M) - break M into two parts zMonitor are intended as primitive building blocks for more complex scheduling policies zMonitors still have problems: yProcesses must make calls on monitor procedures in the correct order yProcesses cannot ignore control imposed by monitor and enter CS directly zSolution yMessage passing for synchronization yACL (access control list) and capabilities for protection

IPC (Inter-Processor communication) zShared memory - Communicate data implicitly via load and store operations zMessage passing - Communicate data by explicitly passing message among processors ySynchronous (blocking) xRPC (Remote Procedure Call) xSender requests, receiver replies yAsynchronous (non-blocking) xData sent directly to consumer process w/o requesting xReceiver blocks if it tries to receive message before it arrives xSender blocks if receiver has not consumed an earlier message xMPI (message passing interface)