CSC501 Operating Systems Principles Deadlock 1
Last Lecture q Priority Inversion Q Priority Inheritance Protocol q Today Q Deadlock 2
The Deadlock Problem q Definition Q A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Q None of the processes can proceed or back-off (release resources it owns) q Example Q semaphores A and B, initialized to 1 P 0 P 1 wait (A); wait (B); wait(b) wait(a) 3
Deadlock Characterization q Deadlock can arise if four conditions hold simultaneously Q Mutual exclusion: only one process at a time can use a resource Q Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes Q No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task Q 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 n is waiting for a resource that is held by P 0. 4
Methods for Handling Deadlocks q Ignore the problem and pretend that deadlocks would never occur. q Prevent the system from entering a deadlock state. q Allow the system to enter a deadlock state and then detect/recover. 5
The IGNORE Approach q Pretend there is no problem Q Unfortunately they can occur (an example) Q Reasonable if v Deadlocks occur very rarely cost of prevention is high q Do your typical OSes take this approach? q It is a trade off between Q Convenience Q Correctness 6
The PREVENT Approach q Restrain the ways requests can be made to break one of the four necessary conditions for deadlocks q Attacking the mutual exclusion condition: Q Some devices (such as printer) can be spooled v only the printer daemon uses printer resource v thus deadlock for printer eliminated Q Not all devices can be spooled 7
The PREVENT Approach q Attacking the Hold and Wait Condition: Q Require processes to request all resources before starting q Problems Q may not know required resources at start of run Q also ties up resources other processes could be using q Variation: Q before a process requests for a new resource, it must give up all resources and then request all resources needed 8
The PREVENT Approach q Attacking the No Preemption Condition: Q When a process holding some resources and waiting for others, its resources may be preempted to be used by others q Problems Q Many resources may not allow preemption; i.e., preemption will cause process to fail 9
The PREVENT Approach q Attacking the Circular Wait Condition: Q Impose a total order of all resource types; and require that all processes request resources in the same order 10
Deadlock Avoidance q When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state q System is in safe state if there exists a sequence <P 1, P 2,, P n > of all processes, such that Q Q For each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i That is: v v v If P i resource needs are not immediately available, then P i can wait until all P j have finished When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate When P i terminates, P i +1 can obtain its needed resources, and so on 11
Deadlock Avoidance q If a system is in safe state no deadlocks q If a system is in unsafe state possibility of deadlock q Avoidance ensure that a system will never enter an unsafe state. 12
Resource-Allocation Graph q A set of vertices V and a set of edges E. Q V is partitioned into two types: v P = {P 1, P 2,, P n }, the set consisting of all the processes in the system v R = {R 1, R 2,, R m }, the set consisting of all resource types in the system Q E is partitioned into two types: v request edge directed edge P i R j v assignment edge directed edge R j P i 13
Resource-Allocation Graph q Process q Resource type with 4 instances q P i requests instance of R j P i R j q P i is holding an instance of R j P i Rj 14
Examples q If graph contains no cycles safe q If graph contains a cycle unsafe 15
Resource-Allocation Graph Algorithm q Suppose that process P i requests a resource R j q The request can be granted only if Q Converting the request edge to an assignment edge does not result in the formation of a cycle in the resource-allocation graph 16
Banker s Algorithm q Each process must a priori claim the maximum set of resources that might be needed in its execution. q Safety check Q Repeat v pick any process that can finish with existing available resources; finish it and release all its resources v until no such process exists Q all finished safe; otherwise unsafe. 17
Data Structure for the Banker s Algorithm q n = # of processes, m = # of resources types. Q Available: Vector of length m v If available [j] = k, there are k instances of resource type R j available Q Max: n x m matrix. v If Max [i,j] = k, then process P i may request at most k instances of resource type R j Q Allocation: n x m matrix. v If Allocation[i,j] = k then P i is currently allocated k instances of R j Q Need: n x m matrix. v 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] 18
Safety Algorithm q Step 1: Let Work and Finish be vectors of length m and n, respectively. Initialize: Q Work = Available Q Finish [i] = false for i = 0, 1,, n- 1 q Step 2: Find any i such that both (If no, Step 4) Q Finish [i] = false Q Need i Work q Step 3. Work = Work + Allocation i Q Finish[i] = true Q Step 2 q Step 4. If Finish [i] == true for all i, then the system is in a safe state 19
Resource-Request Algorithm for Process P i q Process P i wants k instances of R j (Request i [j] = k) Q Step 1: If Request i Need i, go to step 2 v Otherwise, raise error condition, since process has exceeded its maximum claim Q Step 2: If Request i Available, go to step 3 Q v Otherwise P i must wait, since resources are not available Step 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 ; If safe the resources are allocated to Pi If unsafe Pi must wait, and the old resource-allocation state is restored 20
Example of Banker s Algorithm q 5 processes P 0 through P 4 ; q 3 resource types: Q A (10 instances), B (5 instances), and C (7 instances) q 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 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Question: Is this a safe state? Question: Can request for (1,0,2) by P 1 be granted? 21
Example of Banker s Algorithm q 5 processes P 0 through P 4 ; q 3 resource types: Q A (10 instances), B (5 instances), and C (7 instances) q 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 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Question: Can request for (3,3,0) by P 4 be granted? Question: Can request for (0,2,0) by P 0 be granted? 22
Methods for Handling Deadlocks q Ignore the problem and pretend that deadlocks would never occur. q Prevent the system from entering a deadlock state. q Allow the system to enter a deadlock state and then detect/recover. 23
Single Instance of Each Resource Type q Maintain wait-for graph Q Nodes are processes Q P i P j if P i is waiting for P j q Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock q 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 24
Single Instance of Each Resource Type Resource-Allocation Graph Corresponding wait-for graph 25
Additional Issues q When there are several instances of a resource type Q cycle detection in wait-for graph is not sufficient. q Deadlock detection is very similar to the safety check in the Banker s algorithm 26
Recovery from Deadlock q Recovery through preemption Q take a resource from some other process Q depends on nature of the resource q Recovery through rollback Q checkpoint a process state periodically Q rollback a process to its checkpoint state if it is found deadlocked q Recovery through killing processes Q kill one+ of the processes in the deadlock cycle Q the other processes get its resources Q In which order should we choose process to kill? 27
Next Lecture q Lab2 Session Q Please read the lab2 instructions before the next lecture 28