CS510 Operating System Foundations

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CHAPTER3 Higher-Level Synchronization and Communication
Ch 7 B.
Chapter 6: Process Synchronization
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
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.
1 Administrivia  Assignments 0 & 1  Class contact for the next two weeks  Next week  midterm exam  project review & discussion (Chris Chambers) 
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
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.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Jonathan Walpole Computer Science Portland State University
1 OMSE 510: Computing Foundations 7: More IPC & Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
Jonathan Walpole Computer Science Portland State University
Monitors CSCI 444/544 Operating Systems Fall 2008.
CS533 Concepts of Operating Systems Class 3 Monitors.
1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and.
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.
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.
CS510 Concurrent Systems Introduction to Concurrency.
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.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
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.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
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 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
Synchronicity II Introduction to Operating Systems: Module 6.
CS533 Concepts of Operating Systems Class 2a Monitors.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
© 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
Background on the need for Synchronization
CIS Operating Systems Synchronization
CS533 Concepts of Operating Systems Class 3
Jonathan Walpole Computer Science Portland State University
CS510 Operating System Foundations
Operating Systems CMPSC 473
© 2013 Gribble, Lazowska, Levy, Zahorjan
Chapter 5: Process Synchronization
CSE 451: Operating Systems Autumn 2010 Module 8 Semaphores, Condition Variables, and Monitors Ed Lazowska Allen Center 570.
© 2013 Gribble, Lazowska, Levy, Zahorjan
Critical section problem
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
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 19
CS333 Intro to Operating Systems
Oregon Health and Science University
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Monitors and Inter-Process Communication
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

CS510 Operating System Foundations Jonathan Walpole

Monitors

Programming Complexity There are many ways to introduce bugs using locks and semaphores forget to lock somewhere in the program forget to unlock maybe in an error path or conditional non-unique association between data and locks reuse of global value copied to a local initialize lock or semaphore to wrong value incorrect ordering of down and up

Can The Compiler Help? If the set of locking rules is well-defined, why can’t the compiler generate the correct synchronization code for us? We need high level language abstractions for concurrency

Monitors Related shared/global variables are grouped together and protected by the same lock Encapsulation and mutual exclusion can be enforced by the compiler or by programming convention - Encapsulation Local data variables are accessible only via the monitor’s “entry procedures” (i.e., external methods) - Mutual exclusion Threads must acquire the monitor’s mutex lock before invoking one of its procedures

Mutual Exclusion in Monitors A monitor consists of its data and the methods that access it Each monitor has a mutex lock associated with it, called the monitor’s mutex The monitor’s mutex is acquired at the start of every monitor method and released at the end Only one thread is active in the monitor at any time!

Condition Variables What if a thread needs to wait during its computation? maybe it can’t proceed until one of the monitor’s variables reaches a certain value i.e., producer can’t proceed if buffer is full another thread must run in the monitor while this one waits, so it must give up the mutex! How will this thread wake up?

Condition Variables Condition variables (cv) for use within monitors cv.wait() Caller thread is blocked It is enqueued on this variable’s waiting threads list It must not block while holding the monitor’s mutex! The monitor’s mutex must be released in wait, so another thread can access the monitor’s data cv.signal() Dequeues the thread at the head of the waiting list Unblocks it and places it on the ready list

Condition Variables vs Semaphores Condition variables look like semaphores The key difference: condition variables do not have an integer value a wait always causes the thread to block a signal only wakes up a thread if one is waiting a signal that occurs when no threads are waiting is lost! Consider the implications of this for the Dining Philosopher’s solution in assignment 2!

Overview of Monitor Structure monitor’s shared data monitor entry queue x condition variables (these are also shared monitor variables!) y Each condition variable has an associated list of waiting threads List of threads waiting to enter the monitor “entry” methods Can be called from outside the monitor. Only one active at any moment. local methods initialization code

Compiler Support for Monitors If monitors are part of the programming language, the compiler can generate the monitor lock and unlock code automatically programmer may simply define the type of a data item to be monitor (or synchronized) the monitor mutex to be released in wait is identified implicitly by the condition variable’s monitor If monitors are unknown to the compiler (as in Blitz), the programmer writes the monitor mutex code - wait takes the monitor mutex as a parameter

Producer-Consumer with Monitors process Producer begin loop <produce char “c”> BoundedBuffer.deposit(c) end loop end Producer monitor: BoundedBuffer var buffer : ...; nextIn, nextOut :... ; entry deposit(c: char) begin ... end entry remove(var c: char) end BoundedBuffer process Consumer begin loop BoundedBuffer.remove(c) <consume char “c”> end loop end Consumer

Use of Condition Variables monitor : BoundedBuffer var buffer : array[0..n-1] of char nextIn,nextOut : 0..n-1 := 0 fullCount : 0..n := 0 notEmpty, notFull : condition entry deposit(c:char) entry remove(var c: char) begin begin if (fullCount = n) then if (fullCount = n) then wait(notFull) wait(notEmpty) end if end if buffer[nextIn] := c c := buffer[nextOut] nextIn := nextIn+1 mod n nextOut := nextOut+1 mod n fullCount := fullCount+1 fullCount := fullCount-1 signal(notEmpty) signal(notFull) end deposit end remove end BoundedBuffer

Observations In this example, monitor types are part of the programming language The compiler is enforcing mutual exclusion among accesses to a monitor type the monitor mutex lock and unlock operations are not visible in the source code the monitor lock is not passed as a parameter to wait

Condition Variables in Blitz Monitors are not known to the KPL compiler in Blitz! Condition class is used to implement monitors - Condition.wait (mutex) - Condition.signal (mutex) Condition.broadcast(mutex) Mutex must be passed to condition methods to indicate which monitor the condition is in Mutex lock/unlock code must be written by the programmer on entry/exit to/from monitor methods

Condition Variable Semantics Mutual exclusion requirement: - Only one thread at a time can execute in the monitor Scenario: - Thread A is executing in the monitor - Thread A does a signal waking up thread B Which thread runs in the monitor next? A and B must run in some order Which one runs, which one blocks, and how (on what queue)?

Option 1: Hoare Semantics What happens when A signals B? A is suspended (added to waiting thread list for monitor mutex) B wakes up and runs immediately (i.e., nothing else runs in the monitor between the signal and the wakeup of B) A hands B the monitor mutex directly A can only run when B leaves the monitor on exit or in another wait - A only resumes immediately if it is at the head of the mutex list - If not, other threads run before it

Memory Invariance under Hoare Semantics When B wakes up, the state of the monitor’s variables are the same as when A issued the signal When A resumes after signal, the state of the monitor’s variables may have changed! Implications: - Memory invariance is lost across wait - Memory invariance is lost across signal - Memory invariance is preserved across signal to wakeup from wait

Option 2: MESA Semantics What happens when A signals B? A continues executing in the monitor B tries to lock the monitor mutex, and takes its place at the back of the list B resumes when its turn comes around

Memory Invariance under MESA Semantics When B wakes up, the state of the monitor’s variables may not be the same as when A issued the signal! the signal is more like a hint the waking thread must check to see if it should continue or wait again When A continues after signal, the state of the monitor’s variables have not changed, because A retained the mutex Implications: - Memory invariance is lost across wait - Memory invariance is lost across signal - Memory invariance is preserved across signal to wakeup from wait

Memory Invariance under MESA Semantics Implications: - Memory invariance is lost across wait - Memory invariance is not lost across signal Memory invariance is not preserved across signal to wakeup from wait The different implications for memory invariance affect how you write code that uses condition variables You really need to know the semantics of your condition variables!!!

Example Use of Hoare Semantics monitor BoundedBuffer var buffer: array[n] of char nextIn, nextOut: int = 0 cntFull: int = 0 notEmpty: Condition notFull: Condition entry deposit(c: char) if cntFull == N notFull.Wait() endIf buffer[nextIn] = c nextIn = (nextIn+1) mod N cntFull = cntFull + 1 notEmpty.Signal() endEntry entry remove() ... endMonitor Hoare Semantics

Example Use of Mesa Semantics monitor BoundedBuffer var buffer: array[n] of char nextIn, nextOut: int = 0 cntFull: int = 0 notEmpty: Condition notFull: Condition entry deposit(c: char) while cntFull == N notFull.Wait() endWhile buffer[nextIn] = c nextIn = (nextIn+1) mod N cntFull = cntFull + 1 notEmpty.Signal() endEntry entry remove() ... endMonitor MESA Semantics

Example Use of Hoare Semantics monitor BoundedBuffer var buffer: array[n] of char nextIn, nextOut: int = 0 cntFull: int = 0 notEmpty: Condition notFull: Condition entry deposit(c: char) ... entry remove() if cntFull == 0 notEmpty.Wait() endIf c = buffer[nextOut] nextOut = (nextOut+1) mod N cntFull = cntFull - 1 notFull.Signal() endEntry endMonitor Hoare Semantics

Example Use of Mesa Semantics monitor BoundedBuffer var buffer: array[n] of char nextIn, nextOut: int = 0 cntFull: int = 0 notEmpty: Condition notFull: Condition entry deposit(c: char) ... entry remove() while cntFull == 0 notEmpty.Wait() endWhile c = buffer[nextOut] nextOut = (nextOut+1) mod N cntFull = cntFull - 1 notFull.Signal() endEntry endMonitor MESA Semantics

Monitors in Blitz They have MESA semantics When a waiting thread resumes, you can’t assume that the state of the monitor’s variables is the same as when signal was called! There is a broadcast call in addition to signal broadcast wakes up all threads waiting on the condition variable broadcast makes no sense for condition variables with Hoare semantics!

Implementing Hoare Semantics In Assignment 4 you must modify Blitz condition variables to have Hoare semantics: - Do not modify the mutex methods provided, because future code will use them - Create new classes: MonitorLock -- similar to Mutex HoareCondition -- similar to Condition You must write your own test code!