
97101134f5717c1ebe502289790f9b2d.ppt
- Количество слайдов: 53
Operating Systems Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003 6. Deadlock (Ch. 7, S&G) ch 8 in the 6 th ed. v Objectives – describe deadlock, and forms of prevention, avoidance, detection, and recovery OSes: 6. Deadlock 1
Contents 1. What is Deadlock? 2. Dealing with Deadlock 3. Deadlock Prevention 4. Deadlock Avoidance 5. Deadlock Detection 6. Deadlock Recovery OSes: 6. Deadlock 2
1. What is Deadlock? v An example from US Kansas law: – “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone. ” OSes: 6. Deadlock 3
In Picture Form: VUW CS 305 Neither truck can proceed. OSes: 6. Deadlock 4
1. 1. System Deadlock v A process must request a resource before using it, and must release the resource after finishing with it. v A set of processes is in a deadlock state when every process in the set is waiting for a resource that can only be released by another process in the set. OSes: 6. Deadlock 5
1. 2. Necessary Conditions for Deadlock v Mutual Exclusion – at least one resource must be held in non-shareable mode v Hold and Wait – a process is holding a resource and waiting for others OSes: 6. Deadlock continued 6
v No Preemption – only the process can release its held resource v Circular Wait – {P 0, P 1, …, Pn} – Pi is waiting for the resource held by Pi+1; Pn is waiting for the resource held by P 0 OSes: 6. Deadlock 7
1. 3. Resource Allocation Graph v A set of processes {P 0, P 1, …} Pj v A set of resource types {R 1, R 2, …}, together with instances of those types. Rk OSes: 6. Deadlock 8
Edge Notation v Pi R j – process i has requested an instance of resource j – called a request edge v R j Pi – an instance of resource j has been assigned to process i – called an assignment edge OSes: 6. Deadlock 9
Example Graph R 1 P 1 R 2 OSes: 6. Deadlock Fig. 7. 1, p. 211 R 3 P 2 P 3 R 4 10
Finding a Deadlock v If the graph has no cycles then there are no deadlock processes. v If there is a cycle, then there may be a deadlock. OSes: 6. Deadlock 11
Graph with a Deadlock R 1 P 1 R 2 OSes: 6. Deadlock Fig. 7. 2, p. 212 R 3 P 2 P 3 R 4 12
Graph without a Deadlock Fig. 7. 3, p. 213 R 1 P 2 P 3 R 2 P 4 OSes: 6. Deadlock 13
2. Deadling with Deadlocks v Stop a deadlock ever occuring – deadlock prevention u disallow at least one of the necessary conditions – deadlock avoidance u see a deadlock coming and alter the process/resource allocation strategy OSes: 6. Deadlock continued 14
v Deadlock detection and recovery v Ignore the problem – done by most OSes, including UNIX – cheap solution – infrequent, manual reboots may be acceptable OSes: 6. Deadlock 15
3. Deadlock Prevention v Eliminate one (or more) of: – mutual exclusion – hold and wait – no preemption (i. e. have preemption) – circular wait OSes: 6. Deadlock 16
3. 1. Eliminate Mutual Exclusion v Shared resources do not require mutual exclusion – e. g. read-only files v But some resources cannot be shared (at the same time) – e. g. printers OSes: 6. Deadlock 17
3. 2. Eliminate Hold & Wait v One approach requires that each process be allocated all of its resources before it begins executing – eliminates the wait possibility v Alternatively, only allow a process to request resources when it currently has none – eliminates the hold possibility OSes: 6. Deadlock 18
3. 3. Eliminate “No Preemption” v Make a process automatically release its current resources if it cannot obtain all the ones it wants – restart the process when it can obtain everything v Alternatively, the desired resources can be preempted from other waiting processes OSes: 6. Deadlock 19
3. 4. Eliminate Circular Wait v Impose a total ordering on all the resource types, and force each process to request resources in increasing order. v Another approach: require a process to release larger numbered resources when it obtains a smaller numbered resource. OSes: 6. Deadlock 20
4. Deadlock Avoidance v In deadlock avoidance, the necessary conditions are untouched. v Instead, extra information about resources is used by the OS to do better forward planning of process/resource allocation – indirectly avoids circular wait OSes: 6. Deadlock 21
4. 1. Safe States v An OS is in a safe state if there is a safe sequence of process executions
. v In a safe sequence, each Pi can satisfy its resource requests by using the currently available resources and (if necessary) the resources held by Pj (j < i) – only when Pj has finished OSes: 6. Deadlock 22
Safe State Implications Fig. 7. 4, p. 218 v A safe state cannot lead to deadlock. Deadlock Unsafe v An unsafe state may lead to deadlock. v Deadlock is avoided by always Safe keeping the system in a safe state – this may reduce resource utilization OSes: 6. Deadlock 23
Example 1 p. 218 v Max no. of resources: 12 tape drives Max needs v P 0 P 1 P 2 10 4 9 Current Allocation 5 2 2 v Currently, there are 3 free tape drives v The OS is in a safe state, since
is a safe sequence. OSes: 6. Deadlock 24
Example 2 v Same as last slide, but P 2 now has 3 tape drives allocated currently. Max needs v P 0 P 1 P 2 10 4 9 Current Allocation 5 2 3 v The OS is in an unsafe state. OSes: 6. Deadlock 25
4. 2. Using Resource Allocation Graphs v Assume a resource type only has one instance. v Add a claim edge: – Pi R j – process Pi may request a resource Rj in the future – drawn as a dashed line OSes: 6. Deadlock continued 26
v When the resource is actually requested, the claim edge is changed to a request edge. v When an assignment is released, the assignment edge is changed back to a claim edge. OSes: 6. Deadlock continued 27
v All resources must be claimed before system start-up. v An unsafe state is caused by a cycle in the resource allocation graph. OSes: 6. Deadlock 28
Example Figs 7. 5, 7. 6, p. 220 -221 R 1 P 2 P 1 R 2 P 2 R 2 allocation to P 2 creates an unsafe state OSes: 6. Deadlock 29
4. 3. Banker’s Algorithm v Assume that: – a resource can have multiple instances – the OS has N processes, M resource types v Initially, each process must declare the maximum no. of resources it will need. v Calculate a safe sequence if possible. OSes: 6. Deadlock 30
Banker Data Structures v Available[M] – no. of available resource instances for each resource type – e. g. Available[j] == k means K Rj’s v Max[N][M] – max demand of each process – e. g. max[i][j] == k means Pi wants k Rj’s OSes: 6. Deadlock continued 31
v Work[M] – no. of resource instances available for work (by all processes) – e. g. Work[j] == k means K Rj’s are available v Finish[N] – record of finished processes – e. g. Pi is finished if Finish[i] == true OSes: 6. Deadlock continued 32
v Allocation[N][M] – no. of resource instances allocated to each process – e. g. Allocation[i][j] == k means Pi currently has k Rj’s v Need[N][M] – no. of resource instances still needed by each process – e. g. Need[i][j] == k means Pi still needs k Rj’s – Need[i][j] == Max[i][j] - Allocation[i][j] continued 33 OSes: 6. Deadlock
v Request[N][M] – no. of resource instances currently requested by each process – e. g. Request[i][j] == k means Pi has requested k Rj’s OSes: 6. Deadlock 34
Vectors v Allocation[i] shorthand for referring to the 2 D data structures – resources currently allocated to Pi v Need[i] – resources still needed by Pi v Request[i] – resources currently requested by Pi OSes: 6. Deadlock 35
The Safety Algorithm 1 2 3 4 VUW CS 305; p 221 -222 Vector Copy: Work : = Available; Finish : = false Find i such that Pi hasn’t finished but could: Finish[i] == false Need[i] <= Work If no suitable i, go to step 4. Assume Pi completes: Work : = Work + Allocation[i] Finish[i] : = true go to step 2 If for all i Finish[i] == true then Safe-State OSes: 6. Deadlock 36
Safety Example v Resource Type A B C OSes: 6. Deadlock p. 222 Instances 10 5 7 continued 37
Allocation Max Available P 0 P 1 P 2 P 3 P 4 v A 0 2 3 2 0 B 1 0 0 1 0 C 0 0 2 1 2 A 7 3 9 2 4 B 5 2 0 2 3 C 3 2 2 2 3 Need A B C 3 3 2 1 6 0 4 2 0 1 3 A B C 7 4 3 2 0 1 1 The OS is in a safe state since
is a safe sequence. OSes: 6. Deadlock 38
Request Resource Algorithm 1 2 3 VUW CS 305; p. 222 If (Need[i] < Request[i]) then max-error While (Available < Request[i]) do wait Construct a new state by: Available = Available - Request[i] Allocation[i] = Allocation[i] + Request [i] Need[i] = Need[i] - Request [i] 4 If (new state is not safe) then restore and wait OSes: 6. Deadlock 39
Request Example 1 v At some time, P 1 requests an additional 1 A instance and 2 C instances – i. e. Request[1] == (1, 0, 2) v Does this lead to a safe state? – Available >= Request[1] so continue – generate new state and test for safety OSes: 6. Deadlock 40
Allocation Max Available P 0 P 1 P 2 P 3 P 4 v A 0 3 3 2 0 B 1 0 0 1 0 C 0 2 2 1 2 A 7 3 9 2 4 B 5 2 0 2 3 C 3 2 2 2 3 Need A B C 2 3 0 0 6 0 4 2 0 1 3 A B C 7 4 3 0 0 1 3 The OS is in a safe state since
is a safe sequence. OSes: 6. Deadlock 41
Further Request Examples v From this state, P 4 requests a further (3, 3, 0) – cannot be granted, since insufficient resources v Alternatively, P 0 requests a further (0, 2, 0) – should not be granted since the resulting state is unsafe OSes: 6. Deadlock 42
5. Deadlock Detection v If there are no prevention or avoidance mechanisms in place, then deadlock may occur. v Deadlock detection should return enough information so the OS can recover. v How often should the detection algorithm be executed? OSes: 6. Deadlock 43
5. 1. Wait-for Graph v Assume that each resource has only one instance. v Create a wait-for graph by removing the resource types nodes from a resource allocation graph. v Deadlock exists if and only if the wait-for graph contains a cycle. OSes: 6. Deadlock 44
Example Fig. 7. 7, p. 225 P 5 R 1 R 3 P 1 P 2 P 3 R 2 P 4 P 5 R 4 OSes: 6. Deadlock R 5 P 1 P 2 P 3 P 4 45
5. 2. Banker’s Algorithm Variation v If a resource type can have multiple instances, then an algorithm very similar to the banker’s algorithm can be used. v The algorithm investigates every possible allocation sequence for the processes that remain to be completed. OSes: 6. Deadlock 46
Detection Algorithm 1 2 3 4 VUW CS 305; p. 225 Vector Copy: Work : = Available; Finish : = false Find i such that Pi hasn’t finished but could: Finish[i] == false Request[i] <= Work If no suitable i, go to step 4. Assume Pi completes: Work : = Work + Allocation[i] Finish[i] : = true go to step 2 If Finish[i] == false then Pi is deadlocked OSes: 6. Deadlock 47
Example 1 v Resource Type A B C OSes: 6. Deadlock p. 226 Instances 7 2 6 continued 48
Allocation Request P 0 P 1 P 2 P 3 P 4 v Available A 0 2 3 2 0 A B C 0 0 0 B 1 0 0 1 0 C 0 0 3 1 2 A 0 2 0 1 0 B 0 0 0 C 0 2 0 0 2 The OS is not in a deadlocked state since
is a safe sequence. OSes: 6. Deadlock 49
Example 2 v Change P 2 to request 1 C instance Allocation P 0 P 1 P 2 P 3 P 4 v OSes: 6. Deadlock Request Available A 0 2 3 2 0 A 0 2 0 1 0 A B C 0 0 0 B 1 0 0 1 0 C 0 0 3 1 2 B 0 0 0 C 0 2 1 0 2 The OS is deadlocked. 50
6. Deadlock Recovery v Tell the operator v System-based recovery: – abort one or more processes in the circular wait – preempt resources in one or more deadlocked processes OSes: 6. Deadlock 51
6. 1. Process Termination v Abort all deadlocked processes, or v Abort one process at a time until the deadlocked cycle disappears – not always easy to abort a process – choice should be based on minimum cost OSes: 6. Deadlock 52
6. 2. Resource Preemption v Issues: – how to select a resource (e. g. by using minimum cost) – how to rollback the process which has just lost its resources – avoiding process starvation OSes: 6. Deadlock 53