Monitors: An Operating System Structuring Concept

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
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.
Operating Systems Part III: Process Management (Process Synchronization)
CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Ch 7 B.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Secure Operating Systems Lesson 5: Shared Objects.
CS533 Concepts of Operating Systems Class 3 Monitors.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS533 Concepts of Operating Systems Class 3 Monitors.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
Monitors 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.
What we will cover… Process Synchronization Basic Concepts
CS533 Concepts of Operating Systems Class 3 Monitors.
CS533 - Concepts of Operating Systems 1 Class Discussion.
02/17/2010CSCI 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.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
CS510 Concurrent Systems Introduction to Concurrency.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Monitors: An Operating System Structuring Concept Paper by: C. A. R. Hoare Presented by: Sabrina Brick.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Java Thread and Memory Model
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
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.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS533 Concepts of Operating Systems Class 2a Monitors.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
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.
CS703 - Advanced Operating Systems
Background on the need for Synchronization
Deadlock and Starvation
CS533 Concepts of Operating Systems Class 3
Monitors, Condition Variables, and Readers-Writers
Semaphore Originally called P() and V() wait (S) { while S <= 0
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Implementing Mutual Exclusion
CS533 Concepts of Operating Systems Class 3
Monitor Giving credit where it is due:
Implementing Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Monitors and Inter-Process Communication
Presentation transcript:

Monitors: An Operating System Structuring Concept Paper by C. A. R. Hoare Presentation by Emerson Murphy-Hill

The Problem An OS’s main task is to control access to a machine’s resources (data, devices, time, space) If resources are not tightly controlled, “chaos will ensue” race conditions

The Solution Monitors provide control by allowing only one process to access a critical resource at a time A class/module/package Contains procedures and data

An Abstract Monitor name : monitor … some local declarations … initialize local data procedure name(…arguments) … do some work … other procedures

Monitor Rules Any process can access any monitor procedure at any time Only one process may enter a monitor procedure No process may directly access a monitor’s local variables A monitor may only access it’s local variables

Things Needed to Enforce Monitor “wait” operation Forces running process to sleep “signal” operation Wakes up a sleeping process A condition Something to store who’s waiting for a particular reason Implemented as a queue

A Running Example – Emerson’s Kitchen kitchen : monitor occupied : Boolean; occupied := false; nonOccupied : condition; procedure enterKitchen if occupied then nonOccupied.wait; occupied = true; procedure exitKitchen occupied = false; nonOccupied.signal; Monitor Declaration Declarations / Initialization Procedure Procedure

Multiple Conditions Sometimes desirable to be able to wait on multiple things Can be implemented with multiple conditions Example: Two reasons to enter kitchen - cook (remove clean dishes) or clean (add clean dishes) Two reasons to wait: Going to cook, but no clean dishes Going to clean, no dirty dishes

Emerson’s Kitchen kitchen : monitor cleanDishes, dirtyDishes : condition; dishes, sink : stack; dishes := stack of 10 dishes sink := stack of 0 dishes procedure cook if dishes.isEmpty then cleanDishes.wait sink.push ( dishes.pop ); dirtyDishes.signal; procedure cleanDish if sink.isEmpty then dirtyDishes.wait dishes.push (sink.pop) cleanDishes.signal Notice that the condition of one person in the kitchen is now relaxed. Two new rules: there must be one dish in the sink to clean a dish there must be one dish in dishes to cook

Condition Queue Checking if any process is waiting on a condition: “condition.queue” returns true if a process is waiting on condition Example: Doing dishes only if someone is waiting for them

Emerson’s Kitchen kitchen : monitor cleanDishes, dirtyDishes : condition; dishes, sink : stack; dishes := stack of 10 dishes sink := stack of 0 dishes procedure cook if dishes.isEmpty then cleanDishes.wait sink.push ( dishes.pop ); dirtyDishes.signal; procedure cleanDishes if sink.isEmpty then dirtyDishes.wait if cleanDishes.queue dishes.push (sink.pop); cleanDishes.signal;

Scheduled Waits Gives a waiting thread a number Lowest number gets woken up first (inverse priority) Example: People who want to cook are prioritized: Highest: Me! Medium: Family Lowest: Vagrants/Friends So rather than a condition being a regular queue, it’s an ordered queue.

Emerson’s Kitchen kitchen : monitor cleanDishes, dirtyDishes : condition; dishes, sink : stack; dishes := stack of 10 dishes sink := stack of 0 dishes procedure cook( p : Person ) if dishes.isEmpty then if p.isEmerson then cleanDishes.wait(1); if p.isFamily then cleanDishes.wait(2); if p.isFriendAndOrVagrant then cleanDishes.wait(3); sink.push ( dishes.pop ); dirtyDishes.signal; procedure cleanDishes if sink.isEmpty then dirtyDishes.wait; while(cleanDishes.queue) dishes.push (sink.pop); cleanDishes.signal;

Summary Advantages Disadvantages: Data access synchronization simplified (vs. semaphores or locks) Better encapsulation Disadvantages: Deadlock still possible (in monitor code) Programmer can still botch use of monitors No provision for information exchange between machines

Other Issues Discussed Power of Monitors SemaphoreMonitor MonitorSemaphore Proof Rules I (b.wait) I & B I & B (b.signal) I OS Examples Bounded Buffer Alarm clock Buffer allocation Disk-head Scheduler Reader/Writer

Mutual Exclusion: Implementation Disabling Interrupts kitchen : monitor occupied : Boolean; occupied := false; nonOccupied : condition; procedure enterKitchen disableInterrupts(); if occupied then nonOccupied.wait; occupied = true; enableInterrupts(); procedure exitKitchen occupied = false; nonOccupied.signal; Mutex Insertion kitchen : monitor occupied : Boolean; occupied := false; nonOccupied : condition; lock1 : lock; procedure enterKitchen lock1.acquire(); if occupied then nonOccupied.wait; occupied = true; lock1.release(); procedure exitKitchen occupied = false; nonOccupied.signal;

Monitors In Context Multithreaded code could be implemented with monitors (w/language support) Enforcement of mutex locking conventions Abstraction: deadlock can be avoided, but of course, only if the monitor is written correctly!

Conclusion Monitors are a synchronization mechanism A higher level, easier to use abstraction, better encapsulation vs. semaphores/locks Monitors still suffer from various afflictions

References “Monitors: An Operating System Structuring Concept,” Hoare. Modern Operating Systems, Second Edition, Tannenbaum, pp. 115-119. Jon Walpole, correspondence.

Questions What is the essential implementation difference between monitors and semaphores? What problems can still arise with the use of monitors? What is the most specialized compiler mechanism required to implement monitors? Generally, monitors allow us to encapsulate mutex use and avoid the spread of mutex across code. Can you think of a case where this is not possible?

Possible Answers Atomicity of blocked code- reentrance in monitors Deadlock, inconsistent use (eg, acquire without release) Mutual exclusion in monitor methods When we need uninterrupted access to 2 or more resources located in different monitor proceedures. Inconsistent use = no return