13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:

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.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
1 Semaphores and Monitors: High-level Synchronization Constructs.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
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.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Monitors CSCI 444/544 Operating Systems Fall 2008.
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
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CENG334 Introduction to Operating Systems
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.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CSE 451: Operating Systems Winter 2014 Module 8 Semaphores, Condition Variables, and Monitors Mark Zbikowski Allen Center 476 ©
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Synchronization Semaphores
Deadlock and Starvation
Deadlock and Starvation
January 29, 2004 Adrienne Noble
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Lecture 13: Producer-Consumer and Semaphores
Chapter 5: Process Synchronization (Con’t)
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Critical section problem
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CENG334 Introduction to Operating Systems
CSCI1600: Embedded and Real Time Software
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
Lecture 13: Producer-Consumer and Semaphores
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
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSCI1600: Embedded and Real Time Software
Monitors and Inter-Process Communication
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL: Monitors, Condvars, Deadlocks Topics: Monitors Condition Variables Deadlocks Dining philosopher problem

2 Midterm date Midterm on April 5th? Thursday..

3 Semaphores Higher-level synchronization construct ● Designed by Edsger Dijkstra in the 1960's, part of the THE operating system (classic stuff!) Semaphore is a shared counter Two operations on semaphores: P() or wait() or down() ● From Dutch “proeberen”, meaning “test” ● Atomic action: ● Wait for semaphore value to become > 0, then decrement it V() or signal() or up() ● From Dutch “verhogen”, meaning “increment” ● Atomic action: ● Increments semaphore value by 1. Semaphore Adapted from Matt Welsh’s (Harvard University) slides.

4 Producer/Consumer: naïve implementation Producer Consumer int count = 0; Producer() { int item; while (TRUE) { item = bake(); if (count == N) sleep(); insert_item(item); count = count + 1; if (count == 1) wakeup(consumer); } Consumer() { int item; while (TRUE) { if (count == 0) sleep(); item = remove_item(); count = count – 1; if (count == N-1) wakeup(producer); eat(item); } What's wrong with this code? What if we context switch right here?? Adapted from Matt Welsh’s (Harvard University) slides.

5 A fix using semaphores Producer Consumer Semaphore mutex = 1; Semaphore empty = N; Semaphore full = 0; Producer() { int item; while (TRUE) { item = bake(); P(empty); P(mutex); insert_item(item); V(mutex); V(full); } Consumer() { int item; while (TRUE) { P(full); P(mutex); item = remove_item(); V(mutex); V(empty); eat(item); } Adapted from Matt Welsh’s (Harvard University) slides.

6 Reader/Writers Let's go back to the problem at the beginning of lecture. ● Single shared object ● Want to allow any number of threads to read simultaneously ● But, only one thread should be able to write to the object at a time ● (And, not interfere with any readers...) Semaphore mutex = 1; Semaphore wrt = 1; int readcount = 0; Writer() { P(wrt); do_write(); V(wrt); } Reader() { P(mutex); readcount++; if (readcount == 1) { P(wrt); } V(mutex); do_read(); P(mutex); readcount--; if (readcount == 0) { V(wrt); } V(mutex); Why the test here?? Adapted from Matt Welsh’s (Harvard University) slides. ✔ A Reader should only wait for a Writer to complete its do_write(). ✔ A Reader should not wait for other Readers to complete their do_read(). ✔ The Writer should wait for the other Writers to complete their do_write(). ✔ The Writer should wait for all the Readers to complete their do_read().

7 Issues with Semaphores Much of the power of semaphores derives from calls to P() and V() that are unmatched ● See previous example! Unlike locks, acquire() and release() are not always paired. This means it is a lot easier to get into trouble with semaphores. ● “More rope” Would be nice if we had some clean, well-defined language support for synchronization... ● Java does! Adapted from Matt Welsh’s (Harvard University) slides.

8 Monitors ● A high-level abstraction that provides a convenient and effective mechanism for process synchronization ● Only one process may be active within the monitor at a time

9 Condition Variables ● condition x, y; ● Two operations on a condition variable: ● x.wait () – a process that invokes the operation is suspended. ● x.signal () – resumes one of processes (if any) that invoked x.wait ()

10 Monitor with Condition Variables

11 Java Synchronization Support Java synchronization based on locks and condition variables ● Every Java object can be used as a lock What's nice about this? ● Compiler ensures that lock is released before leaving the synchronized block ● Even if there is an exception!! Condition variable (CV): allows a thread to wait or wakeup other threads Three operations on condition variables: ● wait() -- Block until another thread calls notify() or notifyAll() on the CV ● notify() -- Wake up one thread waiting on the CV ● notifyAll() -- Wake up all threads waiting on the CV Object foo; synchronized (foo) { // Do some stuff with 'foo' locked... } Adapted from Matt Welsh’s (Harvard University) slides.

12 Bounded Buffer using CV's int theArray[ARRAY_SIZE], size; Object theLock; void put(int val) { synchronized(theLock) { while (size == ARRAY_SIZE) { theLock.wait(); } addItemToArray(val); size++; if (size == 1) theLock.notify(); } int get() { int item; synchronized(theLock) { while (size == 0) { theLock.wait(); } item = getItemFromArray() size--; if (size == ARRAY_SIZE-1) theLock.notify(); } return item; } Problems with this code?? Adapted from Matt Welsh’s (Harvard University) slides.

13 Bounded Buffer using CV's int theArray[ARRAY_SIZE], size; Object theLock; void put(int val) { synchronized(theLock) { while (size == ARRAY_SIZE) { theLock.wait(); } addItemToArray(val); size++; if (size == 1) theLock.notify(); } int get() { int item; synchronized(theLock) { while (size == 0) { theLock.wait(); } item = getItemFromArray() size--; if (size == ARRAY_SIZE-1) theLock.notify(); } return item; } Assumes only a single thread calling put() and get() at a time! If two threads call get(), then two threads call put(), only one will be woken up!! Adapted from Matt Welsh’s (Harvard University) slides.

14 How to fix this problem? Every notifyAll() will cause all threads to wake up and re-check the empty or full condition. ● Could be inefficient if a lot of threads are involved... int theArray[ARRAY_SIZE], size; Object theLock; void put(int val) { synchronized(theLock) { while (size == ARRAY_SIZE) { theLock.wait(); } addItemToArray(val); size++; if (size == 1) theLock.notifyAll(); } int get() { int item; synchronized(theLock) { while (size == 0) { theLock.wait(); } item = getItemFromArray() size--; if (size == ARRAY_SIZE-1) theLock.notifyAll(); } return item; } Adapted from Matt Welsh’s (Harvard University) slides.

15 Monitors This style of using locks and CV's to protect access to a shared object is often called a monitor ● Think of a monitor as a lock protecting an object, plus a queue of waiting threads. Shared data Methods accessing shared data Waiting threads At most one thread in the monitor at a time How is this different than a lock??? Adapted from Matt Welsh’s (Harvard University) slides.

16 Monitors Shared data Methods accessing shared data unlocked Adapted from Matt Welsh’s (Harvard University) slides.

17 Monitors Shared data Methods accessing shared data locked zzzz... Sleeping thread no longer “in” the monitor. (But not on the waiting queue either! Why?) Adapted from Matt Welsh’s (Harvard University) slides.

18 Monitors Shared data Methods accessing shared data locked Monitor stays locked! (Lock now owned by different thread...) zzzz... notify() Adapted from Matt Welsh’s (Harvard University) slides.

19 Monitors Shared data Methods accessing shared data locked notify() Adapted from Matt Welsh’s (Harvard University) slides.

20 Monitors Shared data Methods accessing shared data locked No guarantee which order threads get into the monitor. (Not necessarily FIFO!) Adapted from Matt Welsh’s (Harvard University) slides.

21 CVs in Java In Java, a thread can only call wait(), notify(), or notifyAll() if it holds the lock of the associated object Why? Object foo; /* NOT ALLOWED!!! */ foo.notify(); /* This is ok... */ synchronized(foo) { foo.notify(); } Adapted from Matt Welsh’s (Harvard University) slides.

22 CVs in Java In Java, a thread can only call wait(), notify(), or notifyAll() if it holds the lock of the associated object Why? ● Want to avoid common race conditions! Object foo; /* NOT ALLOWED!!! */ foo.notify(); /* This is ok... */ synchronized(foo) { foo.notify(); } if (foo.count < 0) { synchronized (foo) { foo.wait(); } /* Another thread... */ synchronized (foo) { foo.count++; foo.notify(); } Adapted from Matt Welsh’s (Harvard University) slides.

23 Hoare vs. Mesa Monitor Semantics The monitor notify() operation can have two different meanings: Hoare monitors (1974) ● notify(CV) means to run the waiting thread immediately ● Causes notifying thread to block Mesa monitors (Xerox PARC, 1980) ● notify(CV) puts waiting thread back onto the “ready queue” for the monitor ● But, notifying thread keeps running Adapted from Matt Welsh’s (Harvard University) slides.

24 Hoare vs. Mesa Monitor Semantics The monitor notify() operation can have two different meanings: Hoare monitors (1974) ● notify(CV) means to run the waiting thread immediately ● Causes notifying thread to block Mesa monitors (Xerox PARC, 1980) ● notify(CV) puts waiting thread back onto the “ready queue” for the monitor ● But, notifying thread keeps running What's the practical difference? ● In Hoare-style semantics, the “condition” that triggered the notify() will always be true when the awoken thread runs ● For example, that the buffer is now no longer empty ● In Mesa-style semantics, awoken thread has to recheck the condition ● Since another thread might have beaten it to the punch Adapted from Matt Welsh’s (Harvard University) slides.

25 The Big Picture The point here is that getting synchronization right is hard How to pick between locks, semaphores, condvars, monitors??? Locks are very simple for many cases. ● Issues: Maybe not the most efficient solution ● For example, can't allow multiple readers but one writer inside a standard lock. Condition variables allow threads to sleep while holding a lock ● Just be sure you understand whether they use Mesa or Hoare semantics! Semaphores provide pretty general functionality ● But also make it really easy to botch things up. Java captures a lot of useful common operations with its use of monitors (compiler checking is nice too) ● But, not possible to implement everything directly with Java's primitives. Adapted from Matt Welsh’s (Harvard University) slides.