CSC321 §9 Deadlock 1 Section 9 Deadlock. CSC321 §9 Deadlock 2 Deadlock: four necessary and sufficient conditions  Serially reusable resources: the processes.

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 Mehdi Naghavi Winter 1385.
Deadlock Prevention, Avoidance, and Detection
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Deadlock Prevention, Avoidance, and Detection.  The Deadlock problem The Deadlock problem  Conditions for deadlocks Conditions for deadlocks  Graph-theoretic.
DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection.
5. Liveness and Guarded Methods Prof. O. Nierstrasz Selected material © Magee and Kramer.
Slide 1 Concurrency, Dining Philosophers Lecture 14 COMP 201.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Deadlock Emery Berger and Mark Corner University of Massachusetts.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
CP — Concurrent Programming 5. Safety and Liveness Properties Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Deadlock CS Introduction to Operating Systems.
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.
Chapter 6 Concurrency: Deadlock and Starvation
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 7 – Deadlock and Indefinite Postponement Outline 7.1 Introduction 7.2Examples of Deadlock.
Starvation and Deadlock
Classical Problems of Concurrency
10 Deadlock Example Process 1 Process 2 Resource 1 Resource 2 Process holds the resource Process requests the resource.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
02/18/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.
Process Synchronization
Chapter 7 – Deadlock and Indefinite Postponement
Concurrency: Deadlock ©Magee/Kramer Claus Brabrand University of Aarhus Deadlock Concurrency.
Definitions Process – An executing program
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Deadlocks Resources Introduction Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Concurrency: Deadlock ©Magee/Kramer Claus Brabrand University of Aarhus Deadlock Concurrency.
Concurrency: Deadlock1 ©Magee/Kramer 2 nd Edition Chapter 6 Deadlock.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
This Time - Deadlock Definition Conditions for deadlocks
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Deadlock cs550 Operating Systems David Monismith.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
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:
Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Concurrency (2) CSE 132. Question The following four numbers are in 8-bit 2’s complement form: Which order below represents.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.
November COMP60621 Designing for Parallelism Lecture 14 Deadlock + Channels in Promela John Gurd, Graham Riley Centre for Novel Computing School.
Synchronization Deadlocks and prevention
Chapter 6 Deadlock.
Deadlock Detection & recovery
Chapter 5: Process Synchronization – Part 3
ITEC 202 Operating Systems
COMP60611 Fundamentals of Parallel and Distributed Systems
Chapter 7 – Deadlock and Indefinite Postponement
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Chapter 5: Process Synchronization (Con’t)
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Conditions for Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
Formal Methods for Software Engineering
Lecture 26 Syed Mansoor Sarwar
Operating System 6 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
CENG334 Introduction to Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

CSC321 §9 Deadlock 1 Section 9 Deadlock

CSC321 §9 Deadlock 2 Deadlock: four necessary and sufficient conditions  Serially reusable resources: the processes involved share resources which they use under mutual exclusion.  Incremental acquisition: processes hold on to resources already allocated to them while waiting to acquire additional resources.  No pre-emption: once acquired by a process, resources cannot be pre-empted (forcibly withdrawn) but are only released voluntarily.  Wait-for cycle: a circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire.

CSC321 §9 Deadlock 3 Wait-for cycle A B C D E Has A awaits B Has B awaits C Has C awaits D Has D awaits E Has E awaits A

CSC321 §9 Deadlock Deadlock analysis - primitive processes  deadlocked state is one with no outgoing transitions  in FSP: STOP process MOVE = (north->(south->MOVE|north->STOP)). Trace to DEADLOCK: north  animation to produce a trace.  analysis using LTSA: (shortest trace to STOP )

CSC321 §9 Deadlock 5 deadlock analysis - parallel composition  in systems, deadlock may arise from the parallel composition of interacting processes. RESOURCE = (get->put->RESOURCE). P = (printer.get->scanner.get ->copy ->printer.put->scanner.put ->P). Q = (scanner.get->printer.get ->copy ->scanner.put->printer.put ->Q). ||SYS = (p:P||q:Q ||{p,q}::printer:RESOURCE ||{p,q}::scanner:RESOURCE ). Deadlock Trace? Avoidance?

CSC321 §9 Deadlock 6 deadlock analysis - avoidance  acquire resources in the same order?  Timeout: P = (printer.get-> GETSCANNER), GETSCANNER = (scanner.get->copy->printer.put ->scanner.put->P |timeout -> printer.put->P ). Q = (scanner.get-> GETPRINTER), GETPRINTER = (printer.get->copy->printer.put ->scanner.put->Q |timeout -> scanner.put->Q ). Deadlock? Progress?

CSC321 §9 Deadlock Dining Philosophers Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the centre of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti One fork is placed between each pair of philosophers and they agree that each will only use the fork to his immediate right and left.

CSC321 §9 Deadlock 8 Dining Philosophers - model FORK = (get -> put -> FORK). PHIL = (sitdown->right.get->left.get ->eat->right.put->left.put ->arise->PHIL). ||DINERS(N=5)= forall [i:0..N-1] (phil[i]:PHIL || {phil[((i-1)+N)%N].right,phil[i].left}::FORK ). Table of philosophers: Can this system deadlock?

CSC321 §9 Deadlock 9 Dining Philosophers - model analysis Trace to DEADLOCK: phil.0.sitdown phil.0.right.get phil.1.sitdown phil.1.right.get phil.2.sitdown phil.2.right.get phil.3.sitdown phil.3.right.get phil.4.sitdown phil.4.right.get This is the situation where all the philosophers become hungry at the same time, sit down at the table and each philosopher picks up the fork to his right. The system can make no further progress since each philosopher is waiting for a fork held by his neighbour i.e. a wait-for cycle exists!

CSC321 §9 Deadlock 10 Dining Philosophers Deadlock is easily detected in our model. How easy is it to detect a potential deadlock in an implementation?

CSC321 §9 Deadlock 11 Dining Philosophers - implementation in Java  philosophers: active entities - implement as threads  forks: shared passive entities - implement as monitors

CSC321 §9 Deadlock 12 Dining Philosophers - Fork monitor class Fork { private boolean taken=false; private int identity; Fork(int id) {identity = id;} synchronized void put() { taken=false; System.out.println(“Fork “ + identity + “ returned”); notify(); } synchronized void get() throws java.lang.InterruptedException { while (taken) wait(); taken=true; System.out.println(“Fork “ + identity + “taken”); } taken encodes the state of the fork

CSC321 §9 Deadlock 13 Dining Philosophers - Philosopher implementation class Philosopher extends Thread { private int identity; private Fork left; private Fork right; Philosopher(int id, Fork r, Fork l) { identity = id; left = l; right = r; } public void run() { … } }

CSC321 §9 Deadlock 14 Dining Philosophers - Philosopher implementation public void run() { try { while (true) { // thinking System.out.println(“Ph“+identity+” thinking”); Time.delay(RandomGenerator.integer(500, 1000)); // hungry System.out.println(“Ph“+identity+” seeks right”); right.get(); // gotright chopstick System.out.println(“Ph“+identity+” got right”); Time.delay(RandomGenerator.integer(500, 1000)); System.out.println(“Ph“+identity+” seeks left”); left.get(); System.out.println(“Ph“+identity+” got left); System.out.println(“Ph“+identity+” eating”); Time.delay(RandomGenerator.integer(500, 1000)); right.put(); left.put(); } } catch (java.lang.InterruptedException e){} } Follows from the model (sitting down and leaving the table have been omitted).

CSC321 §9 Deadlock 15 Dining Philosophers - implementation in Java for (int i =0; i<N; ++i) fork[i] = new Fork(i); for (int i =0; i<N; ++i){ phil[i] = new Philosopher (i,fork[(i-1+N)%N],fork[i]); phil[i].start(); } Code to create the philosopher threads and fork monitors:

CSC321 §9 Deadlock 16 Dining Philosophers To ensure deadlock occurs eventually, the slider control may be moved to the left. This reduces the time each philosopher spends thinking and eating. This "speedup" increases the probability of deadlock occurring.

CSC321 §9 Deadlock 17 Deadlock-free Philosophers Deadlock can be avoided by ensuring that a wait-for cycle cannot exist. How? PHIL(I=0) = (when (I%2==0) sitdown ->left.get->right.get ->eat ->left.put->right.put ->arise->PHIL |when (I%2==1) sitdown ->right.get->left.get ->eat ->left.put->right.put ->arise->PHIL ). Introduce an asymmetry into our definition of philosophers. Use the identity I of a philosopher to make even numbered philosophers get their left forks first, odd their right first. Other strategies?

CSC321 §9 Deadlock 18 Maze example - shortest path to “deadlock” We can exploit the shortest path trace produced by the deadlock detection mechanism of LTSA to find the shortest path out of a maze to the STOP process! We must first model the MAZE. Each position can be modelled by the moves that it permits. The MAZE parameter gives the starting position. eg.MAZE(Start=8) = P[Start], P[0] = (north->STOP|east->P[1]),...

CSC321 §9 Deadlock 19 Maze example - shortest path to “deadlock” ||GETOUT = MAZE(7). Shortest path escape trace from position 7 ? Trace to DEADLOCK: east north west north