Deadlock. Stepping on each other’s feet - I Thread T 1 b1:= allocate(); b2:= allocate(); …… release(b1); release(b2); Thread T 2 b1:= allocate(); b2:=

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Deadlock and Starvation
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
Chapter 7: Deadlocks.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock  deadlock prevention  deadlock avoidance  deadlock detection  recovery.
Deadlocks CS 3100 Deadlocks1. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
Deadlock April 19, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
1 Deadlock. 2 Concurrency Issues Past lectures:  Problem: Safely coordinate access to shared resource  Solutions:  Use semaphores, monitors, locks,
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Ch 3 –What is a deadlock ? –Conditions Hold and Wait Mutual Exclusion Non Preemption Circular Wait –Deadlock Models Single Unit Request AND Request OR.
Outline Objective for today: Formal treatment of deadlock.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
ICS Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources 6.2 Approaches to the Deadlock Problem 6.3 A System Model –Resource Graphs –State.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Deadlock. Examples You can't get a job without experience; you can't get experience without a job. A set of blocked processes each holding a resource.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
Chapter 7: Deadlocks.
CSE 120 Principles of Operating
Ch 3 What is a deadlock ? Conditions Deadlock Models Hold and Wait
Outline Objective for today: Formal treatment of deadlock. Bring Zax
Deadlock Management.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Process Deadlocks.
Lecture 6: Deadlocks, Deadlock Risk Management
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
CSc 552 Advanced Unix Process deadlock deadlock prevention
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Chapter 7: Deadlocks.
Deadlock Hank Levy 1.
OPERATING SYSTEMS DEADLOCKS.
CSE 380 Lecture Note 12 Insup Lee
Operating Systems Principles Process Management and Coordination Lecture 6: Deadlocks 主講人:虞台文.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Deadlock

Stepping on each other’s feet - I Thread T 1 b1:= allocate(); b2:= allocate(); …… release(b1); release(b2); Thread T 2 b1:= allocate(); b2:= allocate(); …… release(b1); release(b2); A serial execution is ok, but interdigitation can cause deadlock!

Stepping on each other’s feet - II The TENEX case If a process requests all systems buffers, operator console tries to print an error message To do so –lock the console –request a buffer DUH!

Stepping on each other’s feet - III Thread T 1 open(f 1, write); open(f 2, write); …… close(f 1,f 2 ); Thread T 2 open(f 2, write); open(f 1, write); …… close(f 1,f 2 ); A serial execution is ok, but interdigitation can cause deadlock!

Issues How can we tell if a system is deadlocked? How can a deadlocked system recover? How can deadlocks be prevented in the first place?

Model A set of resources –e.g. files, buffers, semaphores? System is defined by: –a set of states  each state represents the allocation status of the resources –a set of processes  each process p i can request, acquire, release resources each p i can be described by the set of state transitions it can generate (p i is a partial function from  to 2  )

Transitions If p i can change the state of the system from  1 to  2 we write  1  i  2 We write  a  *  b if  b can be reached from  a, i.e. if either: –  a =  b –  p i :  a  i  b –   c, p i :  a  i  c and  c  *  b

Key Definitions Blocked:p i is blocked in  a if   b :  a  i  b Deadlocked: p i is deadlocked in  a if  b :  a  *  b, p i is blocked in  b Deadlock state:  a is a deadlock state if  p i : p i is deadlocked in  a Safe state:  a is a safe state if   b :  a  *  b :  b is not a deadlock state

Example I  = {  1,  2,  3,  4,  5 }  = {p 1, p 2 } p 1 : { (  2, {  3 }), (  4, {  5 }), (  3, {  4 }) } p 2 : { (  2, {  1 }), (  3, {  2 }), (  5, {  4 }) }  1 : p 1 deadlocked, p 2 deadlocked: deadlock state  2 : not safe (  1 deadlocked), not deadlock  3 : not safe, not deadlock  4 : p 2 blocked, safe state  5 : p 2 blocked, safe state 11 22 33 44 5

Example 2 File allocation example from before: –, –1, –1, 1 –, 2 2, 2 1, No safe states One deadlock state 2 1

Necessary Conditions for Deadlock Mutual exclusion Hold and Wait No preemption Circular waiting

How to deal with deadlock Two basic approaches: –detect deadlock and abort (breaks no-preemption) –prevent deadlock (breaks one of the other conditions)

Reusable Resource Graphs Direct bipartite graphs –process nodes are circles –resource nodes are squares –single resources as circles within the corresponding resource node square t j = total number of resources at resource node r j |(a,b)| = number of edges from a to b Two requirements: –∑ i |(r j, p i )| ≤ t j (can’t allocate more than available) –  i,j : |(p i, r j )| + |(r j, p i )| ≤ t j (no stupid deadlocks)

Examples Requesting buffers p1p1 p2p2 r p1p1 p2p2 Allocating files r1r1 r2r2

Evolving the Resource Graph A resource graph changes as the state of the system changes: p i requests r j –p i must have no outstanding requests –p i may request multiple units (say u i ) –add u i edges from p i to r j acquisition –p i must have outstanding requests on r j, and all such requests must be satisfiable –reverse the direction of all edges release –p i must have no outstanding requests –p i may release any nonempty set of resources –erase the appropriate subset of edges from r j to p i

Example r p1p1 p2p2 p1p1 p2p2 p1p1 p2p2 p1p1 p2p2 p1p1 p1p1 p1p1 Request Acquisition Release

Deadlock Detection Deadlock implies that there is no sequence of state transitions that would allow processes to make progress Then, as long as there exists one sequence of actions through which some process can make progress, there is no deadlock IDEA: –Simulate the most favorable execution of each unblocked process. If that execution exibits deadlock, all will –Simulate by reducing resource graph

Reducing the resource graph A process p i is blocked if |(p i, r j )| + ∑ k |(r j, p k )| > t j for all r j Reduction Step –Find unblocked p i which is not isolated –Erase all edges incident on p i : this is equivalent to acquiring and releasing all p i ’s resources –When there is no such p i, the graph is irreducible –An irreducible graph that contains no edges is completely reducible

Example p2p2 r1r1 r2r2 p1p1 p2p2 r1r1 p1p1 r2r2 p2p2 r1r1 p1p1 r2r2 p 2 is blocked Reduce by p 1 Reduce by p 2 We obtained a fully reducible graph!

Deadlock Theorem Theorem A resource graph represents a deadlock state if and only if it is not completely reducible. Is this really useful? Yes, because, fortunately, the order in which reductions are performed does not affect the final outcome of the reduction process!

Observations: 1 If deadlock, then cycle in resource graph but the opposite is not true! p1p1 p2p2 The resource node had some of its resources non allocated: what if all resources had been allocated?

Observations: 2 A graph is expedient if all of its resources have been allocated. Is a cycle in an expedient graph a sufficient condition for deadlock? p1p1 p2p2 p3p3 NO

Knots A knot is a set of nodes that are mutually reachable from each other, and no other node is reachable from them. a b c e d a b c e d

Observations: 3 If expedient + knot, then deadlock but not necessary! p2p2 r1r1 p1p1 r2r2 p3p3 Expedient, no knot, but deadlock!

Observations: 4 If resources are single unit, (  j : t j = 1) then deadlock iff cycle

OK, we detected the deadlock: then, what? Select a victim and either –terminate the victim costly how is the victim selected? –preempt its resources rollback starvation

Deadlock Prevention - 1 Must make sure that at least one of the 4 necessary conditions does not hold  Mutual Exclusion –difficult to avoid: no collaboration! –sometime possible (read-only files)  Hold and Wait –Force each process to allocate all resources at the beginning –Allow a process to request resources only if it has none low resource utilization possible starvation for processes asking popular resources

Deadlock Prevention - 2  No preemption –preempt all resources held while waiting on a resource –when requesting for a resource: if it is available, allocate it if it is not available, but it is held by another process that is waiting, preempt the resource  Circular wait –impose a total order on all resource types –require each process to request resources in strictly increasing order

Deadlock Avoidance Determine whether, by allowing allocation, we could get a deadlock, i.e. check if by allowing allocation the system could enter a state that is not safe Simple way to do it: have each process declare the maximum number of resources of each type that it may need

Claims Graph Claim graph: Resource graphs that also contains edges that represent potential requests (each process needs to specify max claim to each resource) r p1p1 p2p2 p1p1 p2p2 p1p1 p2p2 p1p1 p2p2 Requests one unit Still completely reducible Requests one unit Not completely reducible (no deadlock though (yet))

Banker’s Algorithm (Dijkstra & Habermann) Variables: available[i]: units of r i not allocated c[a,i]: max claim of p a on r i alloc[a,i]: number of units of r i allocated to p a need[a,i]: number of potential requests: c[a,i] - alloc[a,i] finish[i]: one entry per process, true if process can finish work[i]: one entry per process Let p a attempt to allocate request units of r i if request > need[a,i] {error}; if request > available[i] {wait}; available[i] := available[i] - request; alloc[a,i] := alloc[a,i] + request; need[a,i] := need[a,i] - request; if (not safe()) {undo request and wait}; safe() work[i] := available[i]; while (  u: not finish[u] and (need(u,i) ≤ work[i])) { work[i] := work[i]+alloc[u]; finish[u]:= true; } safe :=  u: finish[u]