Monitors.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
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.
Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
May 23, 2002Serguei A. Mokhov, 1 Synchronization COMP346/ Operating Systems Tutorial 3 Revision 1.2 October 7, 2003.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
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.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Atomic Operations David Monismith cs550 Operating Systems.
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.
Internet Software Development Controlling Threads Paul J Krause.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
pThread synchronization
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Background on the need for Synchronization
Process Synchronization
Synchronization and Scheduling
Synchronization.
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
Thread Synchronization
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Critical section problem
Monitors Chapter 7.
Monitors Chapter 7.
Monitor Giving credit where it is due:
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Monitors and Inter-Process Communication
Don Porter Portions courtesy Emmett Witchel
Synchronization and liveness
Presentation transcript:

Monitors

Monitors - definition 1 In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met. A monitor consists of a mutex (lock) object and condition variables

Monitors - definition 2 Another definition of monitor is a thread-safe class, object, or module that uses wrapped mutual exclusion in order to safely allow access to a method or variable by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion: At each point in time, at most one thread may be executing any of its methods.

Home made monitor class class Account { private semaphore accountSemaphore; // not static…one for every object public void withdraw(int amount) { P(accountSemaphore); balance = balance – amount; V(accountSemaphore); } public void deposit(int amount) balance = balance + amount;

Monitor class monitor class Account { public void withdraw(int amount) { balance = balance – amount; } public void deposit(int amount) balance = balance + amount;

Producer- Consumer Monitor monitor class dijkstraPC { public void producer(int amount) { while(1) while (queue.isFull()) { //busy waiting }; queue.enqueue(produced Stuff); }

Producer- Consumer Monitor public void consumer(int amount) { while(1) while (queue.isEmpty()) { // busy waiting }; queue.dequeue(comsume stuff); }

Spin-waiting In the producer-consumer monitor example, while the producer is waiting for the isFull() condition to be met, it is locking out the consumers from empting out the buffers. while (queue.isFull()) { V(monitor.semaphore); // a chance some other thread may empty a buffer P(monitor.semaphore); };

Condition Variables public void producer(int amount) { while(1) while (queue.isFull()) CP(monitor.semaphore, bufferFull.semaphore); }; queue.enqueue(produced Stuff); }

Synchronization in Java class X { private int a; private int b; public synchronized void addA(){ a++; } public synchronized void addB(){ b++; } // one thread is adding to a, it blocks thread adding to b

Synchronized method per object public void addA() { synchronized( a ) { a++; } public void addB() { synchronized( b ) { b++; } // not allowed in Java because a and b are primitives.

Java Atomic Primitives import java.util.concurrent.atomic.AtomicInteger; class X { AtomicInteger a; AtomicInteger b; public void addA(){ a.increment(); } public void addB(){ b.increment();