Monitor Review and Deadlock! Presented by: Antonio Maiorano Paul Di Marco.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Ch 7 B.
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.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Reminders Homework 3 due next Monday
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Process Synchronization
Chapter 2.3 : Interprocess Communication
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.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/25/2004CSCI 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.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS Introduction to Operating Systems
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Atomic Operations David Monismith cs550 Operating Systems.
Tutorial 5 Even More Synchronization! presented by: Antonio Maiorano Paul Di Marco.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CSCD 330 Network Programming
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
CS510 Operating System Foundations
                                                                                                                                                                 
Critical Section and Critical Resources
Lecture 14: Pthreads Mutex and Condition Variables
Critical Section and Critical Resources
Threading And Parallel Programming Constructs
Another Means Of Thread Synchronization
CSCI1600: Embedded and Real Time Software
Monitor Giving credit where it is due:
Chapter 6 Synchronization Principles
Lecture 14: Pthreads Mutex and Condition Variables
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSCD 330 Network Programming
CSCI1600: Embedded and Real Time Software
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Monitor Review and Deadlock! Presented by: Antonio Maiorano Paul Di Marco

Monitors One can think of a monitor as another Abstract Data Type – like a structure or class – with functions and private data The key attribute of a monitor is that it can be accessed by only one thread at a time

Monitor Analogy Monitor call: … myMonitor.Foo() … Monitors are simpler! A monitor object can be thought of as an object where each access to it is protected by a mutex: Analogous call w/o Monitors: … mutex.wait(); myObject.Foo(); mutex.signal(); …

A Simple Monitor monitor SharedBalance { private int balance; public SharedBalance(int amt) {balance = amt;} public credit(int amt) {balance += amt;} public debit(int amt) {balance -= amt;} }

Show me the money Let’s say we only allow someone to complete a debit transaction when there is enough money to take: SharedBalance::debit(int amt) { while(balance < amt) { } // wait for the cash balance -= amt; }

Whoops! This won’t work if a debit() call occurs when there is not enough money: –debit() waits for someone to credit the account –credit() can’t run because debit is already executing in the monitor! –> DEADLOCK! We need to use Condition Variables…

Condition Variables Condition variables are used when: –a thread running in a monitor –encounters a condition that is not satisfied, –which can only be satisfied by another thread Ours has 3 operations: –wait() - block on a variable, give up monitor –signal() - wake up a thread blocked on this –queue() - asks “are there any threads blocked on you?”

Condition Variables You wait on a c.v. when you want to get another thread to do something for you. This is what happens when you wait(): –It temporarily blocks you, –Hands over “ownership” of the monitor you are running in to another thread –Gives you back the “ownership” of the monitor later on…

Regaining the Monitor There are two schemes for deciding when the blocked thread will regain the monitor: –Immediately, as soon as another thread signals the condition variable, or –Later on, when the thread (that signaled) finishes executing inside the monitor

Show me the money already! Using a new member variable condVar: SharedBalance::credit(int amt) { balance += amt; condVar.signal(); } SharedBalance::debit(int amt) { while(balance < amt) { condVar.wait(); } balance -= amt; }

Deadlock with Semaphores Thread 1: alpha.Wait(); beta.Wait(); DoSomething(); beta.Signal(); alpha.Signal(); Thread 2: beta.Wait(); alpha.Wait(); DoSomething(); alpha.Signal(); beta.Signal(); Semaphore alpha = new Semaphore(1); Semaphore beta = new Semaphore(1);

A Solution Thread 1: alpha.Wait(); beta.Wait(); DoSomething(); beta.Signal(); alpha.Signal(); Thread 2: alpha.Wait(); beta.Wait(); DoSomething(); beta.Signal(); alpha.Signal(); Semaphore alpha = new Semaphore(1); Semaphore beta = new Semaphore(1);

Things to Note Semaphores are powerful low-level synchronization tools  they can be difficult to use Guidelines: –Enforce strict coding standards –Use higher-level constructs if they’re available (i.e. AND-synchronization) –Stress test your code for deadlock

Deadlock with Events Thread 1: // Wait for var to be set alpha.Wait(); // Use var DoSomething(var); // We’re done with var beta.Signal(); Thread 2: // Set var var = InitVal(); // Let other thread use it alpha.Signal(); // Wait until it’s used beta.Wait(); int var; // Global shared variable Event alpha = new Event(0); Event beta = new Event(0);

A Couple of Solutions Impose a total ordering system: Use a semaphore to ensure Thread 1 starts before Thread 2 OR Use semaphores instead of events because what happens in the past counts (recall: semaphores are passive, events are active)

Things to Note With events, deadlock often occurs due to timing dependencies Must ensure that threads start in order if timing is important Consider using semaphores instead of events – use events only when the future is important, not the past