Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Deadlocks System Model Deadlock Characterization

Similar presentations


Presentation on theme: "Chapter 7: Deadlocks System Model Deadlock Characterization"— Presentation transcript:

1 Chapter 7: Deadlocks System Model Deadlock Characterization
Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock

2 The Deadlock Problem P0 P1
In a multiprogramming environment, several processes may compete for a number of resources. A process requests resources; if the resurces are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a deadlock. 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 CD RW drives. P1 and P2 each holds one CD RW drive and each needs another one. semaphores A and B representing CD RW drives are initialized to 1 P0 P1 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 System consists of a finite number of resources.
Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices (physical resources) files, semaphores (logical resources) Each resource type Ri has Wi instances. If a system has two CPU’s, then the resource type CPU has two 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
Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: At least one of the resources is in a non-sharable mode (two processes cannot use a resource at the same time), where only one process at a time can use that resource(e.g. printer). If another process request that resource, the requesting process must be delayed untill the resource has been released. 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 {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

6 Resource-Allocation Graph
Consists of a set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system. request edge – directed edge Pi  Rj assignment edge – directed edge Rj  Pi

7 Resource-Allocation Graph (Cont.)
Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Rj Pi Rj

8 Example of a Resource Allocation Graph
R={R1,R2,R3,R4} P={P1,P2,P3} E={P1R1,R1P2,P2R3,R3P3,R2P1,R2P2} Resource instances=[ ] Available=[ ]

9 Finding Deadlocks A cycle in a resource-allocation graph is a necessary condition for a deadlock: 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.

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

11 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

12 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.

13 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.

14 Methods for Handling Deadlocks
Use a protocol to ensure that the system will never enter a deadlock state: Deadlock prevention Deadlock avoidance Allow the system to enter a deadlock state, detect it, and then recover. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX, Linux and Windows.

15 Deadlock Prevention The aim is to ensure that at least one of the four conditions we discussed before, cannot hold. Mutual Exclusion – not required for sharable resources (e.g., read-only files); must hold for non-sharable resources In general, we don’t have systems with all resources being sharable. Some resources such as printers are non-sharable(cannot be simultaneously shared by several processes). Hence, it is not possible to prevent deadlocks by denying ME for such resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources (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.

16 Deadlock Prevention (Cont.)
No Preemption There is no preemption for already allocated resources. To ensure that this condition does not hold, following protocols can be used: 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 – to ensure that this condition never holds, impose a total ordering of all resource types, and to require that each process requests resources in an increasing order of enumeration.

17 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 (a unique integer number, that is assigned to each resource type). For example: Resources = {tape drive, disk drive, printer} Function F defined as: F (tape drive) = 1 F (disk drive) = 5 F (printer) = 12 Each process can request resources only in an increasing order of enumaration. If a process wants to use the tape drive and the printer at the same time, it should first request the tape drive, then the printer (since, 1<12). 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.

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

19 Deadlock Avoidance Requires that the system has some additional a priori information available. 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.

20 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 .

21 Safe, Unsafe, Deadlocked State Spaces
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. Safe, Unsafe, Deadlocked State Spaces

22 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. Available tape drive = 3 Process Allocation Maximum Need (more) P P P At T0, there are 12-(5+2+2)=3 free tape drives. P1 can allocate all its tape drives (4) and then return them. 2+3 = 5 free tape drives. P0 can allocate all its tape drives (10) and then return them. 5+5 = 10 free tape drives. P2 can allocate all its tape drives (9) and then return them = 12 free tape drives. The sequence <P1, P0, P2> is a safe sequence.

23 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 P P P

24 Avoidance Algorithms Single instance of a resource type
Use a resource-allocation graph Multiple instances of a resource type Use the Banker’s algorithm

25 Banker’s Algorithm Multiple instances of a resource type
Each process must declare the maximum number of instances of each resource type that it may need. When a process requests a resource it may have to wait, if enough resources are not available. When a process gets all its resources it must return them in a finite amount of time.

26 Banker’s Algorithm – A deadlock avoidance algorithm
Data structures of the algorithm: Let n = number of processes, and m = number of resources types. Available[m]: Vector of length m indicates number of available resources of each type. If available [ j ] = k, there are k instances of resource type Rj available. Max[n, m]: n x m matrix defines maximum demand of each process. If Max [i, j] = k, then process Pi may request at most k instances of resource type Rj. Allocation[n, m]: n x m matrix defines the number of resources of each type currently allocated to each process. If Allocation[i, j ] = k then Pi is currently allocated k instances of Rj. Need[n, m]: n x m matrix indicates the remaining resource need of each process. If Need [ i, j ] = k, then Pi may need k more instances of Rj to complete its task. Need [i, j] = Max[i, j] – Allocation [i, j].

27 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 : if and only if

28 The Algorithm for Process Pi
Process Pi makes a request. Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj. If Request(i) > Need(i), then there is an error. Otherwise, go to step 3. If Request(i) >Available, then Pi must wait, since resources are not available. Otherwise, go to step 4 Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available - Request(i); Allocation(i) = Allocation(i)+ Request(i); Need(i) = Need(i) – Request(i) Check whether the resulting state is safe or not using safety algorithm. If safe  the resources are allocated to Pi. If unsafe  Pi must wait, and the old resource-allocation state is restored

29 Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 1, …, n 2. Find an i such that both: (a) Finish [i] = false (b) Needi  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

30 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 R R1 R2 R3 R1 R2 R3 P P 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

31 Example of Banker’s Algorithm (continue)
Work ═ Available = [ 0 2 1] Finish ═ [false, false] i═1, P1 : Need (1) ═ [ ] ≤ Work? Yes Work ═ Work + Allocation (1) ═ [ 1 4 1] Finish [1] ═ true i═2, P2 : Need (2) ═ [ ] ≤ Work? Yes Work ═ Work + Allocation (2) ═ [ 1 4 1] Finish [2] ═ true ═> System is in safe state, so do the allocation. Exercise: Repeat this example for Request(2) and check if the system will end up in an safe state or not?

32 Example of Banker’s Algorithm
5 processes P0 through P4; 3 resource types: A (10 instances), B (5 instances), and C (7 instances). Snapshot at time T0: Allocation Max Available A B C A B C A B C P P P P P

33 Example (Cont.) The content of the matrix Need is defined to be Max – Allocation. Need Available A B C A B C P P P P P The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. (apply safety algorithm to verify)

34 Example: P1 Request (1,0,2) Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. Allocation Need Available A B C A B C A B C P P P P P Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement. Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?

35 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.

36 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.

37 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 index i such that both 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) 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.

38 Example of Detection Algorithm
Five processes P1 through P5; three resource types A (7 instances), B (2 instances), and C (8 instances). Snapshot at time T0: Allocation Request Available A B C A B C A B C P P P P P Sequence <P1, P3, P4, P2, P5> will result in Finish[i] = true for all i. (Apply the detection algorithm to verify this!)

39 Apply the detection algorithm:
A1. Work = Available = [0 0 0] Finish [i] = False, for i = 1, 2, 3, 4, 5 A2. P1 : Finish [1] = False, Request [1] ≤ Work (i.e [0 0 0] ≤ [0 0 0]) yes A3. Work = Work + Alloaction(1) = [0 0 0] + [0 1 0] = [0 1 0] Finish[1] = True A2. P2 : Finish[2] = False Request[2] ≤ Work (i.e [2 0 2] ≤ [0 1 0]) No A2. P3 : Finish[3] = False Request[3] ≤ Work (i.e [0 0 0] ≤ [0 1 0]) yes A3. Work = [0 1 0] + [3 0 3] = [3 1 3] Finish[3] = True

40 Apply the detection algorithm(cont)
A2. P4 : Finish[4] = False Request[4] ≤ Work (i.e [1 0 0] ≤ [3 1 3]) yes A3. Work = [3 1 3] + [2 1 1] = [5 2 4] Finish[4] = True A2. P2 : Finish[2] = False Request[2] ≤ Work (i.e [2 0 2] ≤ [5 2 4]) yes A3. Work = [5 2 4] + [2 0 2] = [7 2 6] Finish[2] = True A2. P5: Finish[5] = False Request[5] ≤ Work (i.e [0 0 2] ≤ [7 2 6]) yes A3. Work = [7 2 6] + [0 0 2] = [7 2 8] Finish[5] = True A4. Finish[i] = True for all processes, there is no deadlock.

41 Example (Cont.) P3 requests an additional instance of type C. Request
A B C P P P P P State of system? Can reclaim resources held by process P1, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P2, P3, P4, and P5.

42 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: Process Termination: Abort one or more processes in order to break the deadlock (mostly this is done) Resource Preemption: Preempt some resources from some of the deadlocked processes.

43 Recovery from Deadlock: Process Termination
Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose processes to abort? 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 Is process interactive or batch?

44 Recovery from Deadlock: Resource Preemption
Preempt some resources from processes and give these resources to other processes untill the deadlock cycle is broken. Three issues need to be addressed: selecting a victim – Which processes and which resources are to be preempted? Determine order of preeemption to minimize cost. Rollback – If a resources is preempted from a process, what should be done with that process? return to some safe state and restart process from that state Starvation – same process may always be picked as victim, most common solution is to include number of rollbacks in cost factor

45 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 7: Deadlocks System Model Deadlock Characterization"

Similar presentations


Ads by Google