Download presentation
Presentation is loading. Please wait.
1
CSE 380 Lecture Note 12 Insup Lee
Deadlock CSE 380 Lecture Note 12 Insup Lee 11/7/00 CSE 380
2
Deadlock Def. A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Necessary Conditions for Deadlock Mutual Exclusion: Processes claim exclusive control of the resources they require. Hold and Wait: Processes hold resources already allocated to them while waiting for additional resources. No Preemption: Resources cannot be forcibly removed from the process holding them until the resources are used to completion. Circular Wait: A circular chain of processes exists such that each process hold one or more resources that are being requested by the next process in the chain. 11/7/00 CSE 380
3
Examples Narrow street crossing example
semaphore S1, S2 P(S1) P(S2) P(S2) P(S1) 11/7/00 CSE 380
4
Dealing with Deadlock Three principle strategies for dealing with deadlock Detection: How can deadlock be identified? Recovery: What are the “best” ways to recover from deadlock? Prevention (and Avoidance): How can deadlock be prevented in the first place? Avoidance: Can we avoid deadlock through careful allocation scheme? 11/7/00 CSE 380
5
Relevant Events A process follows the following sequence to use resources: Request (resource) Use (resource) Release (resource) The three important events are when the process requests, acquires, and releases resources. 11/7/00 CSE 380
6
A System Model A system is a pair (S,P) where S is a set of system states {S,T,U,V,...} and P is a set of processes {P1 ,P2,...}. A process Pi is a partial function from system states into nonempty subsets of system states, Pi: S ® 2S Def. A process Pi is blocked in state S if there exists no T such that S ¾® i T. (A process is blocked in a given state if it can't change state.) Def. A process Pi is deadlocked in state S if for all T such that S ¾®* T, Pi is blocked inT. Ex1. P2 is blocked (and deadlocked) in both U and V. Ex2. P1 is blocked but not deadlocked in T. Def. A state S is called a deadlock state if there exists a process Pi that is deadlocked in S. Def. A state S is a safe state if for all T such that S ¾® i T, T is not a deadlock state. 11/7/00 CSE 380
7
Example P = {S,T,U,V} P = {P1,P2} 1 P1(S) = {T,U} S T P1(U) = {V} 2 …
P2(S) = {U} 1 S T 2 1 2 2 1 U V 1 11/7/00 CSE 380
8
Resource (Allocation) Graph (RAG)
A directed graph is a pair (N,E), where N is a set of nodes and E is a set of ordered pairs (a,b), a,b Î N, called edges. Def. A RAG is a directed graph with N = P È R where P = {P1 ,...,Pn} a set of process nodes and R = {R1 ,...,Rm} a set of resource nodes. The graph is “bipartite” with respect to P and R. An edge (Pi ,Rj) is called a request edge (request by Pi for 1 unit of Rj). An edge (Rj ,Pi) is called an assignment edge (allocation of 1 unit of Rj to Pi). For each resource Ri Î R, there exists a non-negative integer tI denoting the number of units of Ri. 11/7/00 CSE 380
9
Invariants on RAG Let |(a,b)| be the number of edges directed for node a to node b. Then S j |(Ri ,Pj)| £ ti for all i. (No more than ti assignments (allocation) may be made for Ri.) |(Ri ,Pj)| + |(Pj ,Ri)| £ ti for all i and j. (The sum of the requests and allocation of any process for a particular resource cannot exceed the available units.) 11/7/00 CSE 380
10
State Transitions The system state is changed to a new state only as a result of requests, releases, or acquisitions of resources by a single process. Request. If a system is in state S and process Pi has no requests outstanding (no request edges), then Pi may request any # of resources. The system then enters state T, say Release. Pi can cause a state change from S to T by a release operation iff Pi has no requests and some allocations. Pi may release any nonempty subset of its resources in this operation. 11/7/00 CSE 380
11
State Transitions (con’d)
Acquisition. A system can change from state S to state T by an acquisition operation by iff Pi has outstanding requests and all such requests can be satisfied; for all resources Rj such that (Pi , Rj) Î E, we have A process Pi is blocked if it is unable to perform any of these operations: 1, 2, or 3. That is, if there exists at least one resource Rj such that 11/7/00 CSE 380
12
Reduction on RAG A RAG is reduced by a process Pi , which us neither blocked nor an isolated node, by removing all edges to and from Pi . A RAG is irreducible if the graph cannot be reduced by any process. A RAG is completely reducible if there exists a sequence of reductions that deletes all edges of the graph. 11/7/00 CSE 380
13
Theorems Theorem 1: S is a deadlock state iff the RAG of S is not completely reducible. Cor. 1: A process Pi is not deadlocked iff a series of reductions leaves a state in which Pi is not blocked. Cor. 2: If S is a deadlock state, then at least two processes are deadlocked in S. Theorem 2: A cycle in a RAG is a necessary condition for deadlock. Theorem 3: If S is not a deadlock state and then T is a deadlock state iff the operation by Pi is a request and Pi is deadlocked in T. 11/7/00 CSE 380
14
Data structures for RAG
RAG can be represented by An allocation matrix A, where Aij=| ( Pi , Rj ) | for i = 1,…,n, j = 1,…,m. A request matrix B, where Bij=| ( Pi , Rj ) | for i = 1,…,n, j = 1,...,m. will use Bi to denote i-th row, i.e., Bi = ( Bi1 ,..., Bim ). An available vector T, where Ti = # of available unit for Ri, i = 1,...,n. 11/7/00 CSE 380
15
Deadlock Detection Algorithm
L := {}; repeat L' := L; for i:=1 to n do if Pi not in L and Bi <= T then T := T + Ai; L := L U {Pi}; end if end for until L = L'; Deadlock := not( L = {Pi, ..., Pn}) 11/7/00 CSE 380
16
Example A | R1 R2 R3 B | R1 R2 R P1 | P1 | P2 | P2 | P3 | P3 | P4 | P4 | T = (0, 0, 0). Inspection order P1, P2, ... Reduction order Pn, Pn-1, ... # of process inspections = n + (n-1) = n(n+1)/2 So worst-case exec. time = O(mn2) 11/7/00 CSE 380
17
Recovery Recovery through preemption Recovery through rollback
Recovery through killing processes 11/7/00 CSE 380
18
Prevention Eliminate possibilities Techniques
Serialization (Prevention) One-shot allocation (Prevention) Hierarchical allocation (Prevention) Banker’s algorithm (Avoidance) 11/7/00 CSE 380
19
Serialization Only one process may hold resources at any time.
Very inefficient use of resources, eliminates multiprocessing. 11/7/00 CSE 380
20
One-shot Allocation A process may only request all its resources at one time. It is blocked until the entire request can be satisfied. Multiprocessing is permitted, but resources are locked even if they are not in use. This method may be necessary for real-time processes that must be guaranteed not to wait for resource allocation once they are underway. O.w., it is too conservative. 11/7/00 CSE 380
21
Hierarchical Allocation
Algorithm: Resources are grouped into levels. A process may only request resources at levels higher than any resource currently held by that process. Resources may be released in any order. 11/7/00 CSE 380
22
Proof that deadlock cannot occur
Proof by Induction: Assume N is highest and 0 is lowest. Induction hypothesis: Resources requested at levels ³ i will always be acquired and released in a finite time. (No circular wait is possible.) Induction basis: The hypothesis is true for i = highest level N. Induction step: Suppose a process has requested resources at level i-1.. It will be delayed if other processes have those resources. Each of these other processes must release them eventually, or be blocked waiting for resources at level i or higher. By induction hypothesis, this blockage cannot last forever. 11/7/00 CSE 380
23
Properties When all requests are at the same level, this method is equivalent to one-shot allocation. Resources at lower levels are blocked for longer periods, but those at higher levels are shared well. Thus, place the scariest resources at the highest levels so that requests for them will be made only when they are actually needed by a process. This method works well when the resources are semaphores. semaphore S1,S2,S3 P(S1,S2,S3) P(S1) P(S2) P(S2) P(S3) P(S3) P(S1) V(S1,S2,S3) order of V's doesn't matter 11/7/00 CSE 380
24
Avoidance The question is: “Is there an algorithm that can always avoid deadlock by making the right choice all the time?” Deadlock is the result of granting a resource. Banker’s algorithm 11/7/00 CSE 380
25
Banker's Algorithm Each process starts with a claim. A process my never request more than its claim. (However, the sum of the claims of all process may exceed the number of resources.) The current allocation state is kept separately for each resource type: (a) For each process: (1) claim (2) holdings (acquired resources) (3) outstanding request (if process is blocked for allocation) (b) Amount of unallocated resources. 11/7/00 CSE 380
26
Safe state An allocation state is realizable if (a) each claim £ maximum available. (b) each process is holding £ its claim. (c) the total amount of held resources is £ the total available. Otherwise, the allocation state is unrealizable. A realizable state is safe if there is a sequence of processes, P1 ,...,Pn ,(a safe sequence) such that: P1 can finish (i.e., there are enough unallocated resources to satisfy its claim.) In general, Pi can finish if Pi-1 releases its current holding. The state is safe because we can avoid deadlock at the moment by blocking any new processes (or any new claims) until all the current processes have finished in the safe order. 11/7/00 CSE 380
27
Example (One resource class only)
process holding max claims A B C unallocated: safe sequence: A,B,C If B should have a claim of 9 instead of 7, there is no safe sequence. 11/7/00 CSE 380
28
Unsafe state An unsafe state is deadlock free if there is a sequence of processes, P1 ,...,Pn , (a deadlock free sequence) such that P1 might finish. (There are enough unallocated resources to satisfy its current outstanding requests, but not necessarily its entire claim.) In general, Pi might finish if Pi-1 does. The state is deadlock free since no process is waiting. However, it may be unsafe because the processes may now request resource that put them in a deadlock state, no matter what action the allocator takes. 11/7/00 CSE 380
29
Example process holding max claims outstanding requests A B C unallocated: 2 deadlock-free sequence: A,B,C However, this sequence is not safe: if B should have 7 instead of 6 outstanding requests, deadlock exists. 11/7/00 CSE 380
30
Banker’s algorithm The Banker's Algorithm: satisfy a request iff the resulting state is safe. The Banker's Algorithm is conservative: it cautiously avoids entering an unsafe state even if this unsafe state has no deadlock. It also requires prior claims. deadlock unsafe safe 11/7/00 CSE 380
31
Implementation method
Find a safe sequence whenever a request is made. If unsuccessful, block the requester. When a resource is released, consider again allocating resources to blocked processes. The cost of finding a sequence is O(n2) not O(n!). If more than one resource type, the same sequence must work for resources of all types. There is more efficient algorithm by Habermann. Example. five processes: P0 , P1 , P2 , P3 , P4 three resource types: A, B, C with 10, 5, 7 units. At time T0 (Max = Allocation + Need) 11/7/00 CSE 380
32
Example Allocation Max Need Available A B C A B C A B C A B C P P P P P safe sequence <P1, P3, P4, P2, P0> Suppose that P1 requests (1,0,2). To decide whether or not to grant this request, Allocation Need P Again, safe seq <P1, P3, P4, P0, P2> In this new state, P4 requests (3,3,0) not enough available resources P0 requests (0,2,0) unsafe state? Why? 11/7/00 CSE 380
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.