Download presentation
Presentation is loading. Please wait.
Published byCorey Peters Modified over 8 years ago
1
1 Deadlock
2
System Model Process must request a resource before using Process must release the resource when done Deadlock A set of processes is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set.
3
Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.
4
Deadlock Characterization Necessary but not sufficient conditions Mutual exclusion Hold and wait No preemption Required condition Circular wait - a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain Unresolvable circular wait is the definition of deadlock! All four conditions must hold for deadlock
5
Circular Wait Resource B Resource A Process P1 Requests Process P2 Held by Requests Held By
6
Describing Deadlock Deadlocks can be described using resource allocation graph Vertices Active processes{P1, P2, … } Resources{R1, R2, … } Edges A directed edge from Pi to Rj Process Pi requested an instance of resource Rj A directed edge from Rj to Pi Resource Rj has been allocated to process Pi Process are circles, Resources are rectangles
7
Resource Allocation Graph P1P2P3 R1R2 R4 R3
8
So? If a graph contains no cycles, then no process in the system is deadlocked If the graph contains a cycle, deadlock MAY exist If each resource has only one instance, then a cycle implies deadlock
9
Deadlock? P1P2P3 R1R2 R4 R3 Is there a cycle? Is there deadlock?
10
Deadlock Reusable Resources Can only be used by one process at a time. After use, can be reassigned to another process (printer, memory, files, etc.) Deadlock can occur with two processes copying from disk to tape Deadlock can occur with memory allocation when there is 200K available, both processes want 80K, then make second request for 70K Consumable Resources Can be created or destroyed (signals, messages) No fixed limit on # of resources Can deadlock if both waiting for a message from the other
11
Examples of Deadlock Deadlock occurs if receive blocks … u 200K bytes is available for allocation… Process 1Process 2 Request 80K bytes… …Request 70K bytes Request 60K bytes… …Request 80K bytes Process 1Process 2 Receive(P2)… …Receive(P1) Send(P2)… …Send(P1)
12
Handling Deadlock Deadlock Prevention Eliminate one of the 4 conditions Deadlock Avoidance Deadlock possible Make appropriate dynamic choices based on current state Deadlock Detection Take some action to recover
13
Deadlock Prevention Can prevent deadlock by forbidding one of the 4 conditions Forbid Mutual Exclusion Generally not reasonable Multiple read-only access but only exclusive access for writes Forbid Hold and Wait Ask for all resources at one time May block a process for a long time when it can be making progress Wait for resources it doesn ’ t yet need May tie up unneeded resources May hold resources that are not needed immediately May not know what to request Especially true for interactive processes May request resources because they are needed in some instances, but not always
14
Deadlock Prevention (continued … ) Forbid No Preemption Take resources away from waiting processes (require it to be released) Only feasible if state can be saved Examples: CPU, Memory Not usable for Files, Printer, etc. Forbid Circular Wait Define a linear order on items If it needs resources 3, 15, 6, 9, and 4 then must request in the order 3, 4, 6, 9, 15 Cannot have circular wait because a process cannot have 9 and request 5 May not be easy to define the order (files) May force an awkward order of requesting resources May have to request resource 3 in order to request 4, even when 4 is needed now but 3 is not needed until much later
15
Deadlock Avoidance Allow general requests, but make choices to avoid deadlock Assume we know the maximum requests (claims) for each process Process must state it needs a max of 5 A objects, 3 B objects, 2 C objects. Do not need to use its max claims Ok to set max=5 and only use 3 Can make requests at any time and in any order Process Initiation Denial Track current allocations Assume all processes may make maximum requests at the same time Only start process if it can ’ t result in deadlock regardless of allocations
16
Resource Allocation Denial Safe State – We can finish all processes by some scheduling sequence Example: Finish P1, P4, P2, P5, P3 Banker ’ s Algorithm (Dijkstra) Reject a request if it exceeds the processes ’ declared maximum claims Grant a request if the new state would be safe Determining if a state is safe Find any process P i for which we can meet it ’ s maximum requests Don't forget already allocated resources Mark P i as “ done ”, add its resources to available resource pool State is safe if we can mark all processes as “ done ” Block a process if the resources are not currently available or the new state is not safe
17
Avoidance Example Are we deadlocked? ABC 936 ABC 112 ABC P1P1 322 P2P2 613 P3P3 314 P4P4 422 Claim 200 P4P4 112 P3P3 115 P2P2 P1P1 001 CBA Allocation Available Resource 024 P4P4 301 P3P3 201 P2P2 P1P1 222 CBA C - A u P 1 wants 1 more A and 1 more C. Is it ok (safe state)? 200 P4P4 112 P3P3 115 P2P2 P1P1 001 CBA Allocation u Are we in a safe state? 024 301 201 121 110 200 112 115 102
18
Deadlock Detection Avoidance methods tend to limit access to resources Instead, grant arbitrary requests and note when deadlock happens Can vary how often we check Early detection vs. overhead of checks Algorithm Preparation: Create table of process requests, current allocations Note available resources Mark processes with no resources Mark any process whose requests can be met (requests available resources) Include resources from that process as ‘ available ’ (this process can finish) If multiple processes available, pick any If any processes cannot be marked, they are part of a deadlock
19
Banker’s Algorithm Multiple instances. Each process must a priori claim maximum use. When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time.
20
Data Structures for the Banker’s Algorithm Available: Vector of length m. If available [j] = k, there are k instances of resource type R j available. 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. Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j. 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]. m = number of resources types. n = number of processes.
21
Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 1,2, …, n. 2.Find an 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.
22
Resource-Request Algorithm for Process P i 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 i ; Allocation i = Allocation i + Request i ; Need i = Need i – Request i;; If safe the resources are allocated to P i. If unsafe P i must wait, and the old resource- allocation state is restored
23
Example of Banker’s Algorithm 5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5instances), and C (7 instances). Snapshot at time T 0 : Allocation Max Available A B C A B C A B C P 0 0 1 0 7 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 2 4 3 3
24
The content of the matrix. Need is defined to be Max – Allocation. AllocationNeed Available A B C A B C A B C P 0 0 1 0 7 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 2 4 3 1 The system is in a safe state since the sequence satisfies safety criteria.
25
Example P 1 Request (1,0,2) (Cont.) Check that Request Available (that is, (1,0,2) (3,3,2) true. AllocationNeedAvailable A B CA B CA B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 20 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Executing safety algorithm shows that sequence satisfies safety requirement. Can request for (3,3,0) by P 4 be granted? Can request for (0,2,0) by P 0 be granted?
26
Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme
27
Single Instance of Each Resource Type Maintain wait-for graph Nodes are processes. P i P j if P i is waiting for P j. Periodically invoke an algorithm that searches for a cycle in the graph. An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph.
28
Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph
29
Several Instances of a Resource Type Available: A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type. R j.
30
Detection Algorithm 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.
31
Detection Algorithm (Cont.) 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. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.
32
Example of Detection Algorithm Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T 0 : Allocation Request Available 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 Sequence will result in Finish[i] = true for all i.
33
Example (Cont.) 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 State of system? Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.
34
Detection-Algorithm Usage When, and how often, to invoke depends on: How often a deadlock is likely to occur? How many processes will need to be rolled back? 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.
35
1 E 0 D 000 CBA Temporary Available Detection Example 1 ABCDE 00001 RequestAllocation Available Resource ABCDE P1P1 01001 P2P2 00101 P3P3 00001 P4P4 10101 ABCDE P1P1 10110 P2P2 11000 P3P3 00010 P4P4 00000 ABCDE 21121 Mark P 4 (not holding anything someone else wants) 11000 u Mark P 3, new available: 0 0 0 1 1 u Cannot mark P 1 or P 2, so we are deadlocked
36
000 CBA Temporary Available Detection Example 2 Are we deadlocked? ABC 000 ABC P1P1 000 P2P2 202 P3P3 000 P4P4 100 P5P5 002 Requests Available ABC P1P1 010 P2P2 200 P3P3 303 P4P4 211 P5P5 002 AllocationResource ABC 726 010313 315427627
37
0 E 0 D 000 CBA Temporary Available Detection Example 3 ABCDE 00000 RequestAllocation Available Resource ABCDE P1P1 01000 P2P2 00100 P3P3 00010 P4P4 00001 P5P5 10000 ABCDE P1P1 10000 P2P2 01000 P3P3 00100 P4P4 00010 P5P5 00001 ABCDE 11111 u Cannot mark P 1 thru P 5, so we are deadlocked
38
0 E 0 D 000 CBA Temporary Available Detection Example 4 ABCDE 00000 RequestAllocation Available Resource ABCDE P1P1 01000 P2P2 00000 P3P3 00100 P4P4 00011 P5P5 10000 ABCDE P1P1 10000 P2P2 01100 P3P3 00010 P4P4 00000 P5P5 00001 ABCDE 11111 00110 u Mark P 4, (nothing allocated) u Mark P 2 00111, P 1, P 3, P 5 0111111111
39
Deadlock Detection Questions? Does it really work? How often do you run it? How often is deadlock likely to occur? How expensive is it?
40
Deadlock Recovery Several possible approaches Abort all deadlocked processes Simple but common Back up processes to a previously saved checkpoint, then restart Assumes we have checkpoints and a rollback mechanism Runs risk of repeating deadlock Assumes that the deadlock has enough timing dependencies it won ’ t happen Selectively abort processes until deadlock broken Preempt resources until deadlock broken Must roll back process to checkpoint prior to acquiring key resource
41
Deadlock recovery Process Termination Kill them all One at a time Consider priority Time computing Who has most resources Resource Preemption Who gets preempted Do you consider process rollback and starvation
42
Mixed Strategy May group resources into classes, have a different deadlock strategy for each class Swap Space Prevent Deadlocks by requiring all space to be allocated at once Avoidance also possible Tapes/Files Avoidance can be effective here Prevention by ordering resources also possible Main Memory Preemption a good approach Internal Resources (channels, etc.) Prevention by ordering resources Can use linear ordering between classes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.