Download presentation
Presentation is loading. Please wait.
Published byWesley Richards Modified over 8 years ago
1
Outline Objective for today: Formal treatment of deadlock.
2
Deadlock Thread Lecture context switch = Timer interrupt goes off + Save current lecture state ( ESC out of current powerpoint slideshow) + Load state of other lecture (choose powerpoint window) + Reset timer + Run new lecture (Click on slideshow) The program counter
3
Dealing with Deadlock It can be prevented by breaking one of the prerequisite conditions (review): –Mutually exclusive use of resources Example: Allowing shared access to read-only files (readers/writers problem from readers point of view) –circular waiting Example: Define an ordering on resources and acquire them in order (lower numbered fork first) –hold and wait –no pre-emption
4
Dealing with Deadlock (cont.) Let it happen, then detect it and recover –via externally-imposed preemption of resources Avoid dynamically by monitoring resource requests and denying some. –Banker’s Algorithm...
5
The Zax Deadlock Example
6
Deadlock Theory State of resource allocation captured in Resource Graph –Bipartite graph model with a set P of vertices representing processes and a set R for resources. –Directed edges R i P j means R i alloc to P j P j R i means P j requests R i –Resource vertices contain units of the resource Resource R 0 Resource R 1 Process P 0 Process P 1 Request Arc Alloc Arc Reusable Resources
7
Deadlock Theory State transitions by operations: –Granting a request –Making a new request if all outstanding requests satisfied Deadlock defined on graph: –P i is blocked in state S if there is no operation P i can perform –P i is deadlocked if it is blocked in all reachable states from S –S is safe if no reachable state is a deadlock state (i.e., having some deadlocked process) Resource R 0 Resource R 1 Process P 0 Process P 1 Request Arc Alloc Arc
8
Deadlock Theory Cycle in graph is a necessary condition –no cycle no deadlock. No deadlock iff graph is completely reducible –Intuition: Analyze graph, asking if deadlock is inevitable from this state by simulating most favorable state transitions. R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc P3P3
9
The Zax Deadlock Example Z0Z0 Z1Z1 B0B0 B1B1
10
Deadlock Detection Algorithm Let U be the set of processes that have yet to be reduced. Initially U = P. Consider only reusable resources. while (there exist unblocked processes in U) { Remove unblocked P i from U; Cancel P i ’s outstanding requests; Release P i ’s allocated resources; /* possibly unblocking other P k in U */} if ( U != ) signal deadlock;
11
Deadlock Detection Example P0P0 R0R0 R2R2 R3R3 R4R4 R1R1 P3P3 P4P4 P2P2 P1P1
12
P0P0 R0R0 R2R2 R3R3 R4R4 R1R1 P3P3 P4P4 P2P2 P1P1
13
R0R0 R2R2 R3R3 R4R4 R1R1 P3P3 P4P4 P2P2 P0P0
14
R0R0 R2R2 R3R3 R4R4 R1R1 P3P3 P4P4 P2P2 P0P0
15
R0R0 R2R2 R3R3 R4R4 R1R1 P4P4 P2P2 P0P0
16
P0P0 R0R0 R2R2 R3R3 R4R4 R1R1 P4P4 P2P2
17
P0P0 R0R0 R2R2 R3R3 R4R4 R1R1 P4P4
18
P0P0 R0R0 R2R2 R3R3 R4R4 R1R1 P4P4
19
P0P0 R0R0 R2R2 R3R3 R4R4 R1R1
20
R0R0 R2R2 R3R3 R4R4 R1R1 Completely Reducible
21
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc P2P2 With and without P 2
22
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc With and without P 2 Is there an unblocked process to start with?
23
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc With and without P 2
24
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc With and without P 2
25
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc With and without P 2
26
Another Example R0R0 R1R1 P0P0 P1P1 Request Arc Alloc Arc P2P2 With and without P 2 Is there an unblocked process to start with?
27
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
28
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 2 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
29
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 2 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1 Not reducible
30
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 2 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
31
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 2 –Start with P 1 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
32
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 1 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
33
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 1 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1
34
Consumable Resources Not a fixed number of units, operations of producing and consuming (e.g. messages) Ordering matters on applying reductions –Reducing by producer makes “enough” units, –Start with P 1 P0P0 P1P1 P2P2 Producer Arc Producer Arc R0R0 R1R1 Reducible
35
Deadlock Detection & Recovery Continuous monitoring and running this algorithm are expensive. What to do when a deadlock is detected? –Abort deadlocked processes (will result in restarts). –Preempt resources from selected processes, rolling back the victims to a previous state (undoing effects of work that has been done) –Watch out for starvation.
36
Avoidance - Banker’s Algorithm Each process must declare its maximum claim on each of the resources and may never request beyond that level. When a process places a request, the Banker decides whether to grant that request according to the following criteria: –“If I grant this request, then there is a run on the bank (everyone requests the remainder of their maximum claim), will we have deadlock?”
37
Representing the State n processes, m resources avail[m] -avail[m] - avail[i] is the number of available units of R i max[n,m] - max[i,j] is claim of P i for R j alloc[n,m] - alloc[i,j] is current allocation of R j to P i need[n,m] = max[n,m] - alloc[n,m] - the rest that can be requested. 4 7 2 5 2 2
38
Basic Outline of Algorithm if (request[i,j] > avail[j]) defer; //Sufficient resources for request //pretend to grant request avail[j] = avail [j] - request [i,j]; alloc[i,j] = alloc[i,j] + request [i,j]; need[i,j] = need[i,j] - request [i,j]; if (safe state) grant; else defer; 4 7-x 2 5 2 2 x request in ? if (x = = 1)? if (x = = 2)?
39
Basic Outline of Algorithm if (request[i,j] > avail[j]) defer; //Sufficient resources for request //pretend to grant request avail[j] = avail [j] - request [i,j]; alloc[i,j] = alloc[i,j] + request [i,j]; need[i,j] = need[i,j] - request [i,j]; if (safe state) grant; else defer; 4 6 2 5 2 2 1 request in ? if (x = = 1)?
40
Basic Outline of Algorithm if (request[i,j] > avail[j]) defer; //Sufficient resources for request //pretend to grant request avail[j] = avail [j] - request [i,j]; alloc[i,j] = alloc[i,j] + request [i,j]; need[i,j] = need[i,j] - request [i,j]; if (safe state) grant; else defer; 4 6 5 2 1 request in ?
41
Basic Outline of Algorithm if (request[i,j] > avail[j]) defer; //Sufficient resources for request //pretend to grant request avail[j] = avail [j] - request [i,j]; alloc[i,j] = alloc[i,j] + request [i,j]; need[i,j] = need[i,j] - request [i,j]; if (safe state) grant; else defer; 6 2 1 request in ?
42
Basic Outline of Algorithm if (request[i,j] > avail[j]) defer; //Sufficient resources for request //pretend to grant request avail[j] = avail [j] - request [i,j]; alloc[i,j] = alloc[i,j] + request [i,j]; need[i,j] = need[i,j] - request [i,j]; if (safe state) grant; else defer; 4 5 2 5 2 2 2 request in ? if (x = = 2)?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.