Document Sample

University of Pennsylvania Deadlock CSE 380 Lecture Note 12 Insup Lee 11/7/00 CSE 380 1 Deadlock University of Pennsylvania 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 1 Mutual Exclusion: Processes claim exclusive control of the resources they require. 2 Hold and Wait: Processes hold resources already allocated to them while waiting for additional resources. 3 No Preemption: Resources cannot be forcibly removed from the process holding them until the resources are used to completion. 4 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 2 University of Pennsylvania Examples Narrow street crossing example semaphore S1, S2 P(S1) P(S2) P(S2) P(S1) 11/7/00 CSE 380 3 University of Pennsylvania 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? o Avoidance: Can we avoid deadlock through careful allocation scheme? 11/7/00 CSE 380 4 University of Pennsylvania 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 5 University of Pennsylvania 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 6 University of Pennsylvania Example P = {S,T,U,V} 1 S T P = {P1,P2} 2 P1(S) = {T,U} 1 2 P1(U) = {V} 2 … 1 P2(S) = {U} U V 1 … 11/7/00 CSE 380 7 University of Pennsylvania 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=PR 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 8 University of Pennsylvania 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 9 University of Pennsylvania 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. 1 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 S T. i 3 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 10 University of Pennsylvania State Transitions (con’d) 2 Acquisition. A system can change from state S to state T by an acquisition operation by S T iff Pi has outstanding i requests and all such requests can be satisfied; for all resources Rj such that (Pi , Rj) E, we have |(P,R j )| |(R j ,Pk )| t j i k 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 |( P , R j )| |( R j , Pk )| t j i k 11/7/00 CSE 380 11 University of Pennsylvania 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 12 University of Pennsylvania 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 S T , then T i is a deadlock state iff the operation by Pi is a request and Pi is deadlocked in T. 11/7/00 CSE 380 13 University of Pennsylvania Data structures for RAG RAG can be represented by 1 An allocation matrix A, where Aij=| ( Pi , Rj ) | for i = 1,…,n, j = 1,…,m. 2 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 ). 3 An available vector T, where Ti = # of available unit for Ri, i = 1,...,n. 11/7/00 CSE 380 14 University of Pennsylvania 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 15 University of Pennsylvania Example A | R1 R2 R3 B | R1 R2 R3 --------------- --------------- P1 | 1 1 1 P1 | 3 2 1 P2 | 1 1 1 P2 | 2 2 1 P3 | 1 1 1 P3 | 1 1 1 P4 | 1 1 1 P4 | 0 0 0 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 16 University of Pennsylvania Recovery • Recovery through preemption • Recovery through rollback • Recovery through killing processes 11/7/00 CSE 380 17 University of Pennsylvania Prevention • Eliminate possibilities • Techniques – Serialization (Prevention) – One-shot allocation (Prevention) – Hierarchical allocation (Prevention) – Banker’s algorithm (Avoidance) 11/7/00 CSE 380 18 University of Pennsylvania Serialization 1 Only one process may hold resources at any time. 2 Very inefficient use of resources, eliminates multiprocessing. 11/7/00 CSE 380 19 University of Pennsylvania One-shot Allocation 1 A process may only request all its resources at one time. It is blocked until the entire request can be satisfied. 2 Multiprocessing is permitted, but resources are locked even if they are not in use. 3 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 20 University of Pennsylvania Hierarchical Allocation Algorithm: 1. Resources are grouped into levels. 2. A process may only request resources at levels higher than any resource currently held by that process. 3. Resources may be released in any order. 11/7/00 CSE 380 21 University of Pennsylvania 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: 1. Suppose a process has requested resources at level i-1.. 2. It will be delayed if other processes have those resources. 3. Each of these other processes must release them eventually, or be blocked waiting for resources at level i or higher. 4. By induction hypothesis, this blockage cannot last forever. 11/7/00 CSE 380 22 University of Pennsylvania Properties 1. When all requests are at the same level, this method is equivalent to one-shot allocation. 2. 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. 3. 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 23 University of Pennsylvania 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 24 University of Pennsylvania 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 25 University of Pennsylvania Safe state o 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. o 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.) o In general, Pi can finish if Pi-1 releases its current holding. o 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 26 University of Pennsylvania Example (One resource class only) process holding max claims A 4 6 B 2 7 C 4 11 unallocated: 2 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 27 University of Pennsylvania Unsafe state 1. 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.) 2. In general, Pi might finish if Pi-1 does. 3. 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 28 University of Pennsylvania Example process holding max claims outstanding requests A 4 6 2 B 2 9 6 C 4 11 7 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 29 University of Pennsylvania 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 30 University of Pennsylvania 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 31 University of Pennsylvania Example Allocation Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 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 P1 3 0 2 0 2 0 2 3 0 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 32

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 10 |

posted: | 11/24/2011 |

language: | English |

pages: | 32 |

OTHER DOCS BY w3OacgU

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.