Download presentation
Presentation is loading. Please wait.
Published byGriffin Welch Modified over 9 years ago
1
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück
2
2 Today’s Plan Deadlocks: –System Model –Deadlock Characterization –Resource-Allocation Graph –Methods for Handling Deadlocks –Detect, Avoid, Prevent Deadlocks
3
3 What is a Deadlock? A set of processes is blocked because each is holding a resource and waiting to acquire a resource held by another process in the set.
4
4 Example: Traffic Deadlock
5
5 Deadlock Characterization Can arise if four conditions hold simultaneously: 1.Mutual exclusion: only one process at a time can use a resource; resource nonsharable. 2.No preemption: a resource can be released only voluntarily by the process holding it. 3.Hold and wait: a process holding at least one resource is waiting to acquire more resources. 4.Circular wait: a circular dependence exists: P 1 P 2 … P n
6
6 System Model Finite number of resources to be distributed among a number of competing processes. Resource types R 1, …, R m –Each resource type has one or more instances –Files, memory space, I/O devices, CPU cycles Each process utilizes a resource: Request Use Release resource
7
7 Resource-Allocation Graph deadlock request R1 R3 R2 resources P1 P2P3 processes
8
8 Graph with Cycle, but no Deadlock
9
9 Basic Facts If graph contains no cycles no deadlock If graph contains a cycle if one instance per resource type, then deadlock. if several instances per resource type, then possibility of deadlock.
10
10 Deadlock Example threadA lock(X) lock(Y) threadB lock(Y) lock(X) Deadlock possible: 1. threadA locks X 2. threadB locks Y 3. threadA waits for lock Y 4. threadB waits for lock X No deadlock will occur if threadA is able to acquire and release the locks for X and Y before threadB attempts to lock the two objects. Difficult to identify and test for deadlocks that may occur only under certain circumstances.
11
11 Methods for Handling Deadlocks Detect and recover: allow the system to enter a deadlock, detect it, and recover Avoid: ensure that system never enters a deadlock Prevent: ensure that at least one of the necessary conditions cannot hold. Ignore: pretend that deadlocks never occur; used by most OS, including UNIX.
12
12 Deadlock Detection 1.Allow system to enter deadlock state 2.Detection algorithm 3.Recovery scheme
13
13 Several Instances of a Resource Type Available: Vector of length m indicates the number of available resources of each type R j Allocation: An n x m matrix defines the number of resources of each type R j currently allocated to each process P i 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
14
14 Detection Algorithm Given: Free vector of length m (free resources) Finishvector of length n (finished processes) Initialize: (a) Free = Available (b)For i = 1,2, …, n do If Allocation i = 0 then Finish[i] = true. else Finish[i] = false. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.
15
15 Detection Algorithm (cont.) While an index i exists such that both Finish[i] == false(active process) Request i Free(satisfiable request) Do Finish[i] = true(assume termination) Free = Free + Allocation i (release resources) End If Finish[i] == false, for some i, 1 i n, then deadlock. (A process P i is deadlocked if Finish[i] == false.)
16
16 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? one for each disjoint cycle 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.
17
17 Recovery: Preemption Each section of a bridge: 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.
18
18 Recovery: Process Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor.
19
19 Recovery: Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? –Priority of the process. –How long process has computed, and how much longer to completion. –Resources the process has used (simple to preempt) –Resources process needs to complete –How many processes will need to be terminated. –Is process interactive or batch?
20
20 Deadlock Avoidance Ensure that a system will never enter an unsafe state. Requires additional a priori information. Simplest and most useful model: each process declares the maximum number of resources of each type that it may need.
21
21 Safe State System is in safe state if there exists a safe sequence of all processes. Sequence is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<i. –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.
22
22 System States unsafe state possibility of deadlock safe state no deadlocks
23
23 Banker’s Algorithm Multiple instances of some resource types. Each process must a priori claim maximum use. When a process gets all its resources it must return them in a finite amount of time. When a process requests a resource it may have to wait.
24
24 Data Structures: Banker’s Algorithm Available: Vector of length m indicates the number of available resources of each type R j Allocation: n x m matrix: number of resources of each type R j currently allocated to each process P i Max: n x m matrix: If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj. Need: n x m matrix: If Request [i][j] = k, then process P i is requesting k more instances of resource type R j Need [i,j] = Max[i,j] – Allocation [i,j]
25
25 Banker’s Algorithm Given: Free vector of length m (free resources) Finishvector of length n (finished processes) Initialize: (a) Free = Available (b)For i = 1,2, …, n do Finish[i] = false Algorithm requires an order of O(m x n 2 ) operations to detect whether the system is in deadlocked state.
26
26 Banker’s Algorithm (cont.) While an index i exists such that both Finish[i] == false(process active) Need i Free(max. request satisfiable) Do Finish[i] = true(assume termination) Free = Free + Allocation i (release all resources) End If Finish[i] == true, for all i, 1 i n, then safe state.
27
27 Resource-Request for Process Pi 1.If Request i Need i goto step 2. Else, raise error (P i exceeded max.claim) 2.If Request i Available, goto step 3. Else, 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 unsafe P i must wait, restore old resource-allocation If safe the resources are allocated to P i
28
28 Deadlock Prevention Break one of the four conditions: 1.Mutual Exclusion 2.No Preemption 3.Hold and Wait 4.Circular Wait Deadlock is impossible!
29
29 Prevention Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. –Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. –Low resource utilization; starvation possible.
30
30 Prevention (cont.) No Preemption – –If a process that is holding some resources requests another resource that cannot be 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 and 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.
31
31 Combined Approaches Combine the three basic approaches (prevention, avoidance, detection) allowing the use of the optimal approach for each of resources in the system. Partition resources into hierarchically ordered classes. Use most appropriate technique for handling deadlocks within each class.
32
32 Summary Deadlocks –Necessary conditions –Detection: algorithm for several rsc instances –Recovery: strategy –Avoidance: banker’s algorithm –Prevention: break a necessary condition –Combination: select best approach for each resource class
33
33 Source These slides are based on SGG04 and the slides provided by the authors.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.