Download presentation
Presentation is loading. Please wait.
Published byWalter Johnson Modified over 9 years ago
1
Chapter 7: Deadlock 11.4.2008
2
Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class time. l Avoid conflict with other courses.
3
Outline n What is a deadlock? n How to deal with a deadlock? n Deadlock detection n Deadlock prevention n Single-instance: wait-for graph n Multi-instance: banker’s algorithm
4
4 Deadlocks n X(A) = request exclusive lock A l Wait on mutex semaphore A n T1 and T2 will make no further progress. n When can deadlock occur? n How to handle deadlock? l Prevent deadlocks from occurring. l Detection deadlocks and resolve them. n How to do deadlock prevention and detection? T1T2 X(A) X(B) Request X(B) Blocked! Request X(A) Blocked too!
5
Deadlock Characterization n Mutual exclusion: only one process at a time can use a resource. n Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. n No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. n Circular wait: there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0. Deadlock can arise if four conditions hold simultaneously.
6
6 Single-instance Deadlock Detection n Resource type: l Single-Instance: mutex l Multi-Instance: CPUs n Create a waits-for graph: l Nodes are processes/transactions l There is an edge from Ti to Tj if Ti is waiting for Tj to release a lock n Periodically check for cycles in the waits-for graph, l cycle = Deadlock l Resolve a deadlock by aborting a process a cycle. T1T2 S(A) R(A)S(B) R(B) X(B): W(B) X(A): W(A)
7
Single-Instance Wait-For-Graph 7 T1T2 T4T3 T1T2 T4T3 T1T2T3T4 S(A) R(A) X(B) W(B) S(B) S(C) R(C) X(C) X(B) X(A)
8
8 Single-instance Deadlock Prevention n Make sure that deadlock will not occur. n How to do it? l Assign priorities to transactions based on timestamps when they start up. (lower timestamps = higher priority) l Lower priorities cannot wait for higher-priority transactions.
9
9 Single-instance Deadlock Prevention n Say Ti wants a lock that Tj holds. What would a deadlock prevention policy do? l Wait-Die: If Ti has higher priority, Ti waits for Tj; otherwise Ti aborts (lower-priority T never waits for higher-priority T) l Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits. (higher-priority T never waits for lower-priority T) n Why these two policies prevent deadlocks? l The oldest transaction (highest priority one) will eventually get all the locks it requires!
10
10 Wait-Die Policy n Lower-priority process never waits for higher- priority process n If Ti has higher priority, Ti waits for Tj; n Otherwise Ti aborts T1T2T3T4 S(A) R(A) X(B) W(B) S(B) S(C) R(C) X(C) X(B) // abort X(A) // abort
11
11 Wound-Wait Policy n Higher-priority process never waits for lower-priority T n If Ti has higher priority, Tj aborts; n Otherwise Ti waits. T1T2T3T4 S(A) R(A) X(B) W(B) S(B) // Abort T2Abort
12
12 Single-instance Deadlock Prevention (2) n If a transaction re-starts, make sure it has its original timestamp. l It will not be forever aborted due to low priority.
13
Multi-instance Deadlock n Single instance of a resource type. l Detection: Use a wait-for graph n Multiple instances of a resource type – Banker’s algorithm l Deadlock-free or safe state detection : safety algorithm l Deadlock prevention: resource-request algorithm l Deadlock detection: detection algorithm l All three algorithms use similar data structure.
14
08:35 /4314 Multi-instance example P1P1 P2P2 P3P3 R 2 R 1
15
Common Data Structure n Let n = number of processes, and m = number of resources types. n Available: Vector of length m. If available [j] = k, there are k instances of resource type R j available. n Max: n x m matrix. If Max [i,j] = k, then process P i may request at most k instances of resource type R j. n Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j. n Need: n x m matrix. If Need[i,j] = k, then P i may need k more instances of R j to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j].
16
Safety Algorithm n Safety algorithm means … l Is the system (given Available, Need, Allocation matrices) deadlock free? l Does there exist a schedule for all processes to complete their execution without a deadlock? 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n- 1. 2. Find and i such that both: (a) Finish [i] = false (b) Need i Work If no such i exists, go to step 4. 3. Work = Work + Allocation i Finish[i] = true go to step 2. 4. If Finish [i] == true for all i, then the system is in a safe state.
17
Example of Safety Algorithm n 5 processes P 0 through P 4 ; n 3 resource types: l A (10 instances), B (5instances), and C (7 instances). n Snapshot at time T 0 : AllocationNeedAvailable A B CA B C A B C P 0 0 1 07 4 3 3 3 2 P 1 2 0 0 1 2 2 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 24 3 1 n A safe sequence
18
Resource-Request Algorithm for Process P i Can a request be safety granted without causing a deadlock? Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 1. If Request i Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2. If Request i Available, go to step 3. Otherwise P i must wait, since resources are not available. 3. Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available – Request; Allocation i = Allocation i + Request i ; Need i = Need i – Request i ; l If safe the resources are allocated to Pi. l If unsafe Pi must wait, and the old resource-allocation state is restored
19
Example of Resource-Request Algorithm n 5 processes P 0 through P 4 ; n 3 resource types: A (10 instances), B (5instances), and C (7 instances). n Snapshot at time T 0, say P 1 Request (1,0,2) AllocationNeedAvailable A B CA B C A B C P 0 0 1 07 4 3 3 3 2 P 1 2 0 0 1 2 2 P 2 3 0 2 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 24 3 1 n A safe sequence
20
Detection Algorithm Check if there is a deadlock in the system. 1. Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b) For i = 1,2, …, n, if Allocation i 0, then Finish[i] = false;otherwise, Finish[i] = true. 2. Find an index i such that both: (a) Finish[i] == false (b) Request i Work If no such i exists, go to step 4. 3. Work = Work + Allocation i Finish[i] = true go to step 2. 4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked.
21
Example of Detection Algorithm n Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). n Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 30 0 0 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2 n Sequence will result in Finish[i] = true for all i.
22
Example (Cont.) n P 2 requests an additional instance of type C. Request A B C P 0 0 0 0 P 1 2 0 1 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2 n State of system? l Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. l Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.
23
Detection-Algorithm Usage n When, and how often, to invoke depends on: l How often a deadlock is likely to occur? l How many processes will need to be rolled back? one for each disjoint cycle n If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.
24
Recovery from Deadlock: Process Termination n Abort all deadlocked processes. n Abort one process at a time until the deadlock cycle is eliminated. n In which order should we choose to abort? l Priority of the process. l How long process has computed, and how much longer to completion. l Resources the process has used. l Resources process needs to complete. l How many processes will need to be terminated. l Is process interactive or batch?
25
Summary n What is a deadlock? n How to deal with a deadlock? l Deadlock detection l Deadlock prevention n Single-instance: wait-for graph n Multi-instance: banker’s algorithm l Safety algorithm l Resource-allocation algorithm l Detection algorithm
26
End of Chapter 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.