Review: Monitor Semantics If P does X.wait() and later Q does X.signal(): –Hoare Semantics: Q blocks yielding the monitor immediately to P –Mesa Semantics:

Slides:



Advertisements
Similar presentations
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.
Advertisements

Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 6: Process Synchronization
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.
SPL/2010 Liveness And Performance 1. SPL/2010 Performance ● Throughput - How much work can your program complete in a given time unit? ● Example: HTTP.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
10 Deadlock Example Process 1 Process 2 Resource 1 Resource 2 Process holds the resource Process requests the resource.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
02/18/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
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.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Concurrency: Deadlock1 ©Magee/Kramer 2 nd Edition Chapter 6 Deadlock.
CSC321 §9 Deadlock 1 Section 9 Deadlock. CSC321 §9 Deadlock 2 Deadlock: four necessary and sufficient conditions  Serially reusable resources: the processes.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
1 Process Synchronization – Outline Why do processes need synchronization ? What is the critical-section problem ? Describe solutions to the critical-section.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
This Time - Deadlock Definition Conditions for deadlocks
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Deadlock cs550 Operating Systems David Monismith.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Deadlock CS Introduction to Operating Systems.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Deadlock and Starvation
Synchronization Deadlocks and prevention
Chapter 6 Deadlock.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization
Classical Synchronization Problems
Deadlock and Starvation
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Liveness And Performance
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
Monitor Giving credit where it is due:
Monitors and Inter-Process Communication
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Review: Monitor Semantics If P does X.wait() and later Q does X.signal(): –Hoare Semantics: Q blocks yielding the monitor immediately to P –Mesa Semantics: Q continues. P becomes runnable. P will eventually run, but only after Q exits –Brinch Hanson: Q can only signal as it exits, so P will run next. –Java synchrnoized methodsj: Only one reason to wait can exist. Notify() (the Java equivalent of signal() ) makes the blocked thread runnable as with Mesa semantics – it will run at a later time, after Q exits the monitor.

MONITOR prod-con { struct something buffer[n]; int in=0, out=0; int count=0; condition=full, empty; entry void add_item(data) { /* Mesa semantics require a while – why? Bounded wait? */ if/while (count==n) full.wait(); buffer[in] = data; in = (in + 1) % n; count++; empty.signal(); } /* continued */ Mesa vs. Hoare

Mesa vs. Hoare (cont.) /* continued */ entry remove_item (data_ptr) { /* Mesa semantics require a while – why? Bounded wait? */ if/while (count == n) empty.wait(); *data_ptr = buffer[out]; out = (out + 1) % n; count--; full.signal(); }

Dining philosophers P3P3 P1P1 P2P2 P0P0 P4P4 Rice Rules Philosophers alternate between thinking and eating 2 chopsticks are required to eat Philosophers never grab with both hands – they reach for one chopstick at a time. A philosopher is too polite to steal a chopstick from a colleague The philosophers cannot be allowed to starve More than one philosopher should be able to eat at a time (no token ring!)

Dining Philosophers: Approach 1 #define left(i) (i) #define right(i) ((i-1) % 5) MONITOR fork { int avail[5] = {2, 2, 2, 2, 2 }; /* forks available to each phil */ condition hungry[5]; entry pickup_fork (int phil) { if (avail[i] != 2) ready[i].wait(); avail[left(I)]--; avail[right(I)]--; } entry putdown_fork (int phil) { avail[left(i)]++: avail[right(i)]++; if (avail[left(i)] == 2) ready[left(i).signal(); if (avail[right(i)] == 2) ready[right(i)].signal(); }

Why Starvation? Each philosopher is waiting for a different condition, so we can’t ensure fairness. The solution is simple: all philosophers should wait for the same condition, such as their turn in a common queue.

Dining Philosophers: Approach 2 Semaphore chopstick[5] = { 1, 1, 1, 1, 1 }; while (1) { P(chopstick[i]); P(chopstick[(i+1) % 5]); >> V(chopstick[i]); V(chopstick[(i +1) % 5]); >> }

What is Deadlock? First, let’s define a resource: Resources are an abstraction of any reason to wait. Resources come in different types and we can have different numbers of each type. A process/thread can acquire a resource, use it, and then fee it. In this context, resources are unshareable – they are serially reusable. Now let’s formally define deadlock: The condition that arises when there exists a set of processes (or threads) such that each process holds a resource that another process in the set is waiting to acquire. The situation forces all processes in the set to wait forever.

Why Deadlock? Deadlock can occur if these conditions are satisfied: Mutual exclusion – at least one resource must be held by a process. Hold and wait – at least one process hold a resource while it is waiting for another resource. No preemption – one process can’t take another process’s resources in order to make progress (nor can the OS) Circular wait – there exists a circular chain of processes, each of which is waiting for a resource held by the next process in the chain.

Simple Defense: Serialization One simple defense against deadlock is to serialize the request of resources. 1.Enumerate all of the resources, giving each a number. 2.Require that all processes request resources in the order of this enumeration. That is to say that they are designed so that they never request a resource with a lower number than the highest numbered resource that they hold 3.Circular wait is now impossible, because the chain of waiting cannot wrap around from the greatest back to the beginning. It will eventually unfold.

Dining philosophers - Serialized P3P3 P1P1 P2P2 P0P0 P4P4 Rice Trace P 0 grabs Chopstick 0, blocking P 4 P 1 grabs Chopstick 1, blocking P 0 P 2 grabs Chopstick 2, blocking P 1 P 3 grabs Chopstick 3, blocking P 2 P 4 blocks trying to grab Chopstick 0 P 3 is free to grab Chopstick 4 P 3 can eat!

Dining Philosophers: Approach 3 Semaphore chopstick[5] = { 1, 1, 1, 1, 1 }; while (1) { if (i < ((i+1) % 5)) { P(chopstick[i]); P(chopstick[(i+1) % 5]); } else { P(chopstick[(i+1) % 5]); P (chopstick[i]); } >> V(chopstick[i]); V(chopstick[(i +1) % 5]); >> }