Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock.

Similar presentations


Presentation on theme: "Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock."— Presentation transcript:

1 Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock

2 The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example  System has 2 tape drives.  P 1 and P 2 each hold one tape drive and each needs another one. Example  semaphores A and B representing tape drives are initialized to 1 P 0 P 1 wait (A);wait(B) wait (B);wait(A)

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 System Model Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows:  request  use  Release A process cannot request more than the total number of resources available in the system

5 Deadlock Characterization Mutual exclusion: At least one of the resources is non-sharable where only one process at a time can use that resource. Hold and wait: There must be at least one process holding at least one resource and waiting to acquire additional resources currently being hold by other processes. No preemption: No resource can be preempted before the holding process completes its task with that resource. 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. Deadlock can arise if four conditions hold simultaneously.

6 Resource-Allocation Graph V is partitioned into two types:  P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system.  R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system. request edge – directed edge P i  R j assignment edge – directed edge R j  P i A set of vertices V and a set of edges E.

7 Resource-Allocation Graph (Cont.) Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j PiPi PiPi RjRj RjRj

8 Finding Deadlocks If the resource allocation graph contains no cycles then there is no deadlock in the system at that instant. If the resource allocation graph contains a cycle then a deadlock may exist. If there is a cycle and each resource has exactly one instance then a deadlock has occurred. A cycle in a resource-allocation graph is a necessary condition for a deadlock :

9 Example of a Resource Allocation Graph No cycle  No deadlock

10 Resource Allocation Graph - Example Here are 2 cycles: P1  R1  P2  R3  P3  R2  P1 P2  R3  P3  R2  P2 hence, P1,P2,P3 are deadlocked

11 Resource Allocation Graph - Example Here is a cycle: P1  R1  P3  R2  P1 But, there is no deadlock. P4 can finish after a while and R2 will then be assigned to P3. Hence, the cycle will be broken.

12 Basic Facts If graph contains no cycles  no deadlock. If graph contains a cycle   if only one instance per resource type exists, then deadlock.  if several instances per resource type exist, deadlock may or may not exist.

13 Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX.

14 Deadlock Prevention Mutual Exclusion  In general, we don’t have systems with all resources being sharable. Some resources such as printers are non-sharable. Hence, it is not possible to prevent deadlocks by denying ME for such resources. Hold and Wait (possible solutions are:)  Each process must request and get all of its resources before it begins execution.  Each process can request resources only when it has no resources allocated. Both protocols cause low resource utilization; starvation possible. A process that requests several commonly used resources may have to wait indefinitely. Ensure that at least one of the four conditions we discussed before cannot hold.

15 Deadlock Prevention (Cont.) No Preemption  If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.  Preempted resources are added to the list of resources for which the process is waiting.  Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

16 Deadlock Prevention (Cont.) Formally define a function, F: R→N where R= {R1, R2, …., RN} is a set of resource types, and N is the set of natural numbers. For example: F (tape driver) = 1 F (disk driver) = 5 F (printer) = 12 If a process wants to use the tape drive and the printer, it should first request the tape drive, then the printer. Whenever a process requests a resource Ri, it must have released all resources Rj with F (Rj) ≥ F (Ri). With this protocol, a circular wait is impossible.

17 Deadlock Prevention (Cont.) Proof by contradiction: Assume a set of processes is in a circular wait: {P1, P2, …., PN} Every P i+1 is holding R i and requesting R i+1. So, we must have F (R i ) < F (R i+1 ), i = 1,2,..... This means that, F (R1) < F (R2) < … < F (Rn) < F (R1)  F (R1) < F (R1)??? P1 R1 P2 R2 P3 R3 Example: N=3 contradiction

18 Deadlock Avoidance Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Requires that the system has some additional a priori information available.

19 Safe State Given some additional information on how each process will request resources, it is possible to construct an algorithm that will avoid deadlock states. The algorithm will dynamically examine the resource allocation operations to ensure that there won’t be a circular wait on resources. When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. A state is safe if the system can allocate resources up to its maximum to each process in some order (safe sequence of processes) avoiding a deadlock. A deadlock state is an unsafe state.

20 Basic Facts If a system is in safe state  no deadlocks. If a system is in unsafe state  possibility of deadlock. Avoidance  ensure that a system will never enter an unsafe state.

21 Example Consider a system with 12 tape drives and 3 processes P0, P1 and P2. Assume that we know the maximum number of tape drives each processes may request as: P0 requires 10 tape drives, P1 requires 4, and P2 requires as many as 9. Suppose at time T0, 9 tape drives are allocated as follows: P0←5, P1←2, and P2←2. Process Allocation Maximum Need P0 5 10 5 P1 2 4 2 P2 2 9 7 At T0, there are 12-(5+2+2)=3 free tape drives. The sequence is a safe sequence.

22 Example (continue) It is possible to go from a safe to an unsafe state: Consider the above example. Assume at time T1, P2 requests one tape drive and gets it. Now we have 2 free tape drives. The system is in an unsafe state, because only P1 can be allocated all its tape drives. After it finishes and returns all 4 tape drives, P0 is allocated 5, may request 5 more → has to wait P2 is allocated 3, may request 6 more → has to wait P0 and P2 may be deadlocked! Process Allocation Maximum Need P0 5 10 5 P1 2 4 2 P2 3 9 6

23 Banker’s Algorithm – A deadlock avoidance algorithm Available[m]: Vector of length m. If available[ j ] = k, there are k instances of resource type R j available. Max[n,m]: 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,m]: n x m matrix. If Allocation[ i,j ] = k then P i is currently allocated k instances of R j. Need[n,m]: 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]. Let n = number of processes, and m = number of resources types.

24 REMARKS Each row vector in Allocation and Need are denoted as Allocation (i) and Need (i). So, Allocation (i) specifies the resources currently allocated to process Pi. Let X and Y be vectors of length n. We say, X ≤ Y X[i] ≤ Y[i], i=1, 2, …, n X < Y X ≤ Y and X ≠ Y

25 The Algorithm for Process P i 1. Process P i makes a request. If Request i [j] = k then process P i wants k instances of resource type R j. 2. If Request(i) > Need(i), then there is an error. 3. If Request(i) >Available, then P i must wait, since resources are not available. 4. 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) 5. Check whether the resulting state is safe or not using safety algorithm. If safe  the resources are allocated to P i. If unsafe  P i must wait, and the old resource- allocation state is restored

26 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: Finish [i] = false Need(i)  Work If no such i exists, go to step 4. 3. If an i is found, 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.

27 Example of Banker’s Algorithm Suppose a system with three resources types is supporting two processes. The state of the system is as follows. Available ═ [1 4 1] (i.e. not in use at the time being) MAX ALLOCATION REQUEST NEED R1 R2 R3 R1 R2 R3 P1 1 3 10 0 0 1 2 0 1 3 1 P2 1 4 1 0 0 0 0 2 1 1 4 1 Suppose Request (1) is to be processed: Available ═ [ 1 4 1] - [ 1 2 0] ═ [ 0 2 1]. Allocation ═ [ 1 2 0 0 0 0] Need ═ [ 0 1 1 1 4 1] Then, apply the safety algorithm

28 Example of Banker’s Algorithm (continue) Work ═ [ 0 2 1] Finish ═ [false, false] i═1 : Need (1) ═ [ 0 1 1 ] ≤ Work? Yes Work ═ Work + Allocation (1) ═ [ 1 4 1] Finish [1] ═ true i═2 : Need (2) ═ [ 1 4 1 ] ≤ Work? Yes Work ═ Work + Allocation (2) ═ [ 1 4 1] Finish [2] ═ true ═> System is in safe state, so do the allocation.

29 Example of Safety Algorithm 5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5 instances), and C (7 instances). Snapshot at time T 0 : AllocationMaxAvailable A B CA B C A B C P 0 0 1 07 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 24 3 3

30 Is the system Safe? The content of the matrix. Need is defined to be Max – Allocation. NeedAvailable A B C P 0 7 4 3 3 3 2 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 The system is in a safe state since the sequence satisfies safety criteria. (apply safety algorithm to verify)

31 Deadlock Detection If a system has no deadlock prevention and no deadlock avoidance scheme, then it needs a deadlock detection scheme with recovery from deadlock capability. For this, information should be kept on the allocation of resources to processes, and on outstanding allocation request. Then, an algorithm is needed which will determine whether the system has entered a deadlock state. This algorithm must be invoked periodically.

32 A Deadlock Detection Algorithm Data structures used: Available[m]: as in Banker’s Algorithm Allocation [n, m]: as in Banker’s Algorithm Request [n,m]: A two dimensional array of size n×m which indicates the current request of each process. If Request [ i,j ] ═ k, then Pi, is requesting k more instances of resource type Rj, Allocation(i) and Request(i) are the i’th rows of Allocation and Request matrices. Work and Finish are vectors of length m and n, as in the safety algorithm.

33 The Algorithm A1: Initialize Work ═ Available For i ═ 1 to n do if Allocation (i ) ═ 0 then Finish[i] ═ true else Finish[i] ═ false A2: Find an i such that Finish[i] ═ false Request (i) ≤ Work If no such i can be found, go to A4 A3: For that i found in A2 do Work ═ Work + Allocation (i) Finish[i] ═ true go to A2 A4: If Finish [i] ≠ true for all i, then the system is in a deadlock state and for those i’s for which Finish[i] ═ false, Pi’s are deadlocked.

34 Example of Detection Algorithm Five processes P 1 through P 5 ; three resource types A (7 instances), B (2 instances), and C (8 instances). Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 1 0 1 0 0 0 0 0 0 0 P 2 2 0 2 2 0 2 P 3 3 0 30 0 0 P 4 2 1 1 1 0 0 P 5 0 0 2 0 0 2 Sequence will result in Finish[i] = true for all i. (Apply the detection algorithm to verify this!)

35 Example (Cont.) P 3 requests an additional instance of type C. Request A B C P 1 0 0 0 P 2 2 0 2 P 3 0 0 1 P 4 1 0 0 P 5 0 0 2 State of system?  Can reclaim resources held by process P 1, but insufficient resources to fulfill other processes; requests.  Deadlock exists, consisting of processes P 2, P 3, P 4, and P 5.

36 Recovery from Deadlock If the system is in a deadlock state, some method for recovering it from the deadlock must be applied. There are various ways of recovery from deadlock:  Allocate one resource to several processes, by violating mutual exclusion.  Preempt some resources from some of the deadlocked processes.  Abort one or more processes in order to break the deadlock (mostly this is done)

37 Recovery from Deadlock: Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. problem: Starvation – same process may always be picked as victim. In which order should we select victim processes?  Priority of the process.  How long process has computed, and how much longer to completion.  Resources the process has used.  Resources process needs to complete.  How many processes will need to be terminated.

38 Ignore Deadlocks - UNIX UNIX does not provide any method to handle deadlocks. So, the UNIX approach is just to ignore the problem on the assumption that most users would prefer an occasional deadlock to a rule restricting all users to one process, one open file, and one of everything.


Download ppt "Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock."

Similar presentations


Ads by Google