Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
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 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
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.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
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.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
CS533 Concepts of Operating Systems Class 3 Monitors.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
University of Pennsylvania 9/28/00CSE 3801 Concurrent Programming (Critical Regions, Monitors, and Threads) CSE 380 Lecture Note 6 Insup Lee.
Higher Level Mechanisms for Building Critical Sections zSemaphores yVery primitive (Specifying direct start and stop of threads) ySimple (Hard to program.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Monitors: An Operating System Structuring Concept
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Experience with Processes and Monitors in Mesa
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
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.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
CS533 Concepts of Operating Systems Class 2a Monitors.
Chapter 71 Monitor for Reader/Writers  Synchronizes access if all processes follow.  If some don’t go through monitor and database is accessed directly,
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
CIS Operating Systems Synchronization
CS533 Concepts of Operating Systems Class 3
Jonathan Walpole Computer Science Portland State University
© 2013 Gribble, Lazowska, Levy, Zahorjan
CS510 Operating System Foundations
Chapter 5: Process Synchronization (Con’t)
Semaphore Originally called P() and V() wait (S) { while S <= 0
CSE 451: Operating Systems Autumn 2010 Module 8 Semaphores, Condition Variables, and Monitors Ed Lazowska Allen Center 570.
© 2013 Gribble, Lazowska, Levy, Zahorjan
Readers and Writers and Deadlock
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
CS533 Concepts of Operating Systems Class 3
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 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: Monitors
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Monitors and Inter-Process Communication
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Synchronization: Monitors Hank Levy

6/21/20152 Synchronization with Semaphores Semaphores 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 So, semaphores are sometimes hard to use and prone to bugs. One solution is to provide programming language support for synchronization. –Why does putting something in the language make it harder to misuse?

6/21/20153 Monitors A monitor is a programming language construct that supports controlled access to shared data. –leverage language, compiler, runtime. –advantages? A 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 A monitor protects the data from unstructured access. The monitor guarantees that processes trying to access the data through its procedures interact only in legitimate ways.

A Monitor 4 shared data operations (procedures) A monitor encapsulates shared data and the procedures that operate on it. waiting queue of processes trying to enter the monitor

6/21/20155 Monitor Facilities A monitor guarantees mutual exclusion only one process can be executing within the monitor at any instant –semaphore 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 easier to use most of the time Once 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. Condition Variables provide synchronization within the monitor so that processes can wait or signal others to continue.

6/21/20156 Condition Variables A place to wait. Sometimes called a “rendezvous point” The actual logic is provided by the program, not by the condition variable BOOLEAN NoteEnoughMilk, MilkInTransit; CONDITION MilkCondition IF (NotEnoughMilk AND MilkInTransit) THEN Condition.Wait(MilkCondition); Three operations on condition variables –Condition.Wait(c) release monitor lock, wait for someone to signal condition –Condition.Signal(c) wakeup 1 waiting thread –Condition.Broadcast(c) wakeup all waiting threads

Basic Monitor Structure 6 resource: monitor begin busy: boolean; free: condition; procedure acquire; begin if busy then CONDITION.Wait(free); end procedure release; begin busy=false; CONDITION.Signal(free); end busy=false ; initialize busy end

6/21/20158 Basic Ideas the monitor is controlled by a lock; only 1 process can enter the monitor at a time; others are queued condition variables provide a way to wait; when a process blocks on a condition variable, it givesup the lock. a process signals when a resource or condition has become available; this causes a waiting process to resume immediately. The lock is automatically passed to the waiter; the original process blocks.

Monitors Have Several Associated Queues 8 shared data operations (procedures) waiting queue of processes trying to enter the monitor x.cond y.cond waiting queue of processes who released the monitor on signals condition variable wait queues

Bounded Buffer Monitor Example 9 bounded buffer: monitor begin buffer: array 0..N-1 of portion; lastpointer: 0..N-1; count: 0..N; nonempty, nonfull: condition; procedure append(x: portion) begin if count = N then CONDITION.Wait(nonfull); buffer[lastpointer] := x; lastpointer:=(lastpointer+1) MOD N; count:=count+1; CONDITION.Signal(nonempty); end; procedure remove(result x:portion) begin if count = 0 then CONDITION.Wait(nonempty); x:=buffer[(lastpointer-count) MOD N]; count:=count-1; CONDITION.Signal(nonfull); end; count:=0; lastpointer:=0; end bounded buffer;

6/21/ Monitors and Semaphores Monitors and Semaphores can be implemented in terms of each other. E.g., to implement monitors with semaphores, we need: mutex : a sema to control entry to the monitor (init to 1) next : a sema to suspend a process when it signals another (init to 0) next-count : integer # of processes waiting due to signals x-sem : a sema to suspend a process on a wait (init to 0) [one semaphore for each condition] x-count: integer # of proc. waiting due to waiting on condition [one for each condition]

Monitors implemented with Semaphores 11 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.

Example of Wait/Signal 12 alarmclock: monitor begin now: integer wakeup: condition; proc wake (n:int); wake me in n clock ticks begin alarmsetting: int; alarmsetting := now + n; while now < alarmsetting do CONDITION.Wait(alarmsetting); CONDITION.Signal(wakeup); end; proc tick; clock tick called by external clock begin now = now + 1; CONDITION.Signal(wakeup); end; end alarmclock;

6/21/ Two kinds of Monitors HOARE Monitors –SIGNAL(c) Run waiter immediately. Signaller blocks right now. Condition is guaranteed to hold when blocker runs. But, signaller must RESTORE MONITOR INVARIANTS before signalling. MESA Monitors –SIGNAL(c) waiter is made ready, but the signaller continues. Condition is not necessarily true when the waiter runs again. Signaller must not restore invariant until it leaves the MONITOR –either with a WAIT or an explicit return/ WAKEUP is only a HINT that something must have changed. –must recheck conditional case.

6/21/ Examples HOARE if (NotReady) Condition.Wait(C); MESA while (NotReady) Condition.Wait(C); MESA monitors easier to use –more efficient fewer switches directly supports broadcast. Hoare monitors leave less to “chance.”

6/21/ In Summary... MONITORS –Use different mechanism for scheduling and mutual exclusion –are higher level than semaphores Protect CODE and not data –consider the difference May require some higher level language support... Mutexes are an alternative... –hybrid of monitors and semaphores

6/21/ Mutex Example mutex_t mu condition_t co; boolean ready; … foo() { mutex_lock(mu) if (!ready) condition_wait(co, mu); } … ready = TRUE; condition_signal(mu); mutex_unlock(mu); }