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.

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Operating Systems Mehdi Naghavi Winter 1385.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
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.
Classic Synchronization Problems
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
What we will cover… Process Synchronization Basic Concepts
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
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.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Deadlock and Starvation
Dining Philosophers Five philosophers sit around a table
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Deadlock and Starvation
Chapter 6-7: Process Synchronization
Chapter 6: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Monitors Chapter 7.
Lecture 25 Syed Mansoor Sarwar
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Module 7a: Classic Synchronization
Critical section problem
Chapter 7: Synchronization Examples
Monitors Chapter 7.
Monitors Chapter 7.
CSE 542: Operating Systems
Presentation transcript:

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 oriented processes call procedures within a monitor but cannot access internal data can be built into programming languages synchronisation enforced by the compiler only one process allowed within a monitor at one time wait and signal operations on condition variables

Operating Systems: Monitors 2 Blocked processes go into a Holding Area Possibilities for running signalled and signalling processes –let newly signalled process run immediately, and make signalling process wait in holding area –let signalling process continue in monitor, and run signalled process when it leaves

Operating Systems: Monitors 3 Example: to acquire and release access to some data items/1 : –process A entering monitor to request permission to access data –receiving permission to access data –leaving monitor to access data status flags actual data actual data actual data A A A

Operating Systems: Monitors 4 –process A entering monitor to release access permission to data –releasing access permission to data –leaving monitor status flags actual data actual data actual data A A

Operating Systems: Monitors 5 Example: to acquire and release access to some data items/2 : –process A entering monitor to get permission to access to data –entering monitor and not receiving permission to access data –having to wait in holding area status flags actual data actual data actual data A A A

Operating Systems: Monitors 6 –process B entering monitor to release access to data –process B releasing access to data –process B entering holding area whilst process A re-enters monitor to get access permission to data status flags actual data actual data actual data B B A A B status flags A B A

Operating Systems: Monitors 7 –process A is accessing data –process B has left holding area and left the monitor –process A entering monitor to release access to data –process A releasing access to data –finally process A leaves monitor status flags actual data actual data actual data A status flags A A

Operating Systems: Monitors 8 Example: An Acquire and Release Monitor monitor AandR { Boolean : busy = false; condition : available; entry acquire { if (busy) wait (available); busy = true; } entry release { busy = false; signal (available); } }

Operating Systems: Monitors 9 Example: A Producer/Consumer Monitor monitor PC { condition : full, empty; int : count = 0; entry put { if (count==max) wait (full); insert item count = count+1; if (count==1) signal (empty); } entry get { if (count==0) wait (empty); remove item count = count-1; if (count==max-1) signal (full); } } producer process while (TRUE) { produce item PC.put; } consumer process while (true) { PC.get; consume item }

Operating Systems: Monitors 10 Example: The Dining Philosophers five philosophers –sometimes they sit and think –sometimes they just sit –sometimes they want to eat one bowl of spaghetti in centre of the table five forks –one between each place need two forks, one from each side, to eat spaghetti

Operating Systems: Monitors 11 When a philosopher gets hungry –he first gets a fork from his right –and then gets a fork from his left –he may have to wait for either or both forks if they are in use by a neighbour represented by : while (TRUE) { P(fork on right); P(fork on left); eat spaghetti V(fork on right); V(fork on left); } how to avoid possible deadlock? –Allow at most four philosophers to eat at once –only allow a philosopher to pick up the two forks if they are already available –use an asymmetric solution

Operating Systems: Monitors 12 monitor DP { condition : self [0:4]; typedef states = ( thinking, hungry, eating); states : state [0:4] = thinking(5); entry pickup { state [i] = hungry; test (i); if ( state [i] != eating ) wait ( self [i] ); } entry putdown { state [i] = thinking; test ( (I+4)%5 ); test ( (I+1)%5 ); } procedure test (int k) { if ( state [ (k+4)%5 ] != eating && state [k]==hungry && state [ (k+1)%5 ] != eating ) { state [k] = eating; signal ( self [k] ); }} Philosopher process DP.pickup (i); eat spaghetti dp.putdown (i); – Philosopher can still starve to death!

Operating Systems: Monitors 13 Example: The Sleeping Barber (Tanenbaum & Woodhull) –one barber, one barbers chair, n customer chairs –barber sleeps until a customer appears –first customer to appear wakes barber up –subsequent customers sit down until all chairs are occupied, otherwise they leave –how to program the barber and customers without race conditions ?

Operating Systems: Monitors 14 monitor SB { condition : customers, barbers; int waiting = 0; entry barber { wait(customers); waiting = waiting -1; signal(barbers); cut hair } entry customer { if (waiting < no. of chairs) { waiting = waiting+1; signal(customers); wait(barbers); get haircut } } }

Operating Systems: Monitors 15 Implementation of Monitors using semaphores For each monitor, there is a semaphore mutex, initialised to 1 a semaphore next, initialised to 0 –used by signalling process to suspend itself an integer next-count to count the number of processes in the holding area, waiting on next compiler generates monitor entry and exit code : P(mutex); monitor code if (next-count > 0) V(next); else V(mutex); each condition variable x has : –a semaphore x-sem, initialised to 0 –an integer x-count, initialised to 0

Operating Systems: Monitors 16 wait (x) { x-count = x-count+1; if (next-count > 0) V(next); else V(mutex); P(x-sem); x-count = x-count-1; } signal (x) { if (x-count > 0) { next-count = next-count+1; V(x-sem); P(next); next-count = next-count-1; } –version in which signalling process is held up in holding area whilst signalled process continues –in general, the process scheduler controls which released process runs next

Operating Systems: Monitors 17 Monitors in Java Every object of a class that has a synchronized method has a monitor associated with it Any such method is guaranteed by the Java Virtual Machine execution model to execute mutually exclusively from any other synchronized methods for that object Access to individual objects such as arrays can also be synchronized –also complete class definitions Based around use of threads One condition variable per monitor –wait() releases a lock I.e.enters holding area –notify() signals a process to be allowed to continue –notifyAll() allows all waiting processes to continue

Operating Systems: Monitors 18 no way to notify a particular thread (but threads can have a priority) synchronized methods can call other synchronized and non-synchronized methods monitor can be re-entrant i.e. can be re-entered recursively example: class DataBase { public synchronized void write (... ) {... } public synchronized read (... ) {... } public void getVersion() {... } } –once a thread enters either of the read or write methods, JVM ensures that the other is not concurrently entered for the same object –getVersion could be entered by another thread since not synchronized –code could still access a database safely by locking the call rather than by using synchronized methods: DataBase db = new DataBase(); synchronized(db) { db.write(... ); }

Operating Systems: Monitors 19 class ProCon { private int contents; private boolean available = false; public synchronized int get() { while (available==false) { try { wait(); } catch (InterruptedException e) { } } available = false; notify(); return contents; } public synchronized int put(int value) { while (available==true) { try { wait(); } catch (InterruptedException e) { } contents = value; available = true; notify(); } } Example: producer/consumer methods for a single item :

Operating Systems: Monitors 20 Java monitor implementation of User-level semaphores class Semaphore { private int value; Semaphore (int initial) { value = initial; }// constructor synchronized public void P() { while (value==0) { try { wait(); } catch (InterruptedException e) { } } value = value-1; } synchronized public void V() { value = value+1; notify(); } } since the thread calling notify() may continue, or another thread execute, and invalidate the condition, it is safer to retest the condition in a while loop

Operating Systems: Monitors 21 class BoundedSemaphore { private int value, bound; Semaphore (int initial, int bound) {// constructor value = initial; this.bound = bound; } synchronized public void P() { while (value==0) { try { wait(); } catch (InterruptedException e) { } } value = value-1; notify(); } synchronized public void V() { while (value==0) { try { wait(); } catch (InterruptedException e) { } } value = value+1; notify(); } }

Operating Systems: Monitors 22