ch7_4slides
ch7_4slides
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Chapter 7: Deadlocks Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
Deadlock Characterization Deadlock with Mutex Locks
Deadlock can arise if four conditions hold simultaneously. Deadlocks can occur via system calls, locking, etc.
Mutual exclusion: only one process at a time can use a See example box in text page 318 for mutex deadlock
resource
Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes
No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed
its task
Circular wait: there exists a set {P0, P1, …, Pn} 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.
Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
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
Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
Example of a Resource Allocation Graph Resource Allocation Graph With A Deadlock
Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks Deadlock Prevention
Ensure that the system will never enter a deadlock Restrain the ways request can be made
state:
Mutual Exclusion –
Deadlock prevention
– Some resources can be shared easily (like a read-only
Deadlock avoidence file; many can read it at the same time).
Allow the system to enter a deadlock state and then – Some resources cannot be shared (like a printer; only
recover one process can use it at a time).
Ignore the problem and pretend that deadlocks never – For non-sharable resources, we must make sure only
occur in the system; used by most operating systems, one process uses them at a time.
including UNIX
Hold and Wait –
– Normally, a process can hold one resource and wait for
another, which can cause problems (like deadlock).
– To prevent this, we say:
• A process must request all the resources it needs
before it starts running. OR
• A process can only ask for resources if it has none
already.
Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013
Safe, Unsafe, Deadlock State Avoidance Algorithms
Single instance of a resource type
Use a resource-allocation graph
Operating System Concepts – 9th Edition 7.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013
Unsafe State In Resource-Allocation Graph Resource-Allocation Graph Algorithm
Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013
Safety Algorithm Resource-Request Algorithm for Process Pi
1. Let Work and Finish be vectors of length m and n, respectively. Requesti = request vector for process Pi. If Requesti [j] = k then
Initialize: process Pi wants k instances of resource type Rj
Work = Available 1. If Requesti Needi go to step 2. Otherwise, raise error condition,
Finish [i] = false for i = 0, 1, …, n- 1 since process has exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Pi must wait,
2. Find an i such that both: since resources are not available
(a) Finish [i] = false 3. Pretend to allocate requested resources to Pi by modifying the
(b) Needi Work state as follows:
If no such i exists, go to step 4 Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
3. Work = Work + Allocationi
Finish[i] = true Needi = Needi – Requesti;
go to step 2 If safe the resources are allocated to Pi
4. If Finish [i] == true for all i, then the system is in a safe state If unsafe Pi must wait, and the old resource-allocation state
is restored
Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013
Example: P1 Request (1,0,2) Deadlock Detection
Check that Request Available (that is, (1,0,2) (3,3,2) true Allow system to enter deadlock state
Allocation Need Available
ABC ABC ABC Detection algorithm
P0 010 743 230
Recovery scheme
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
Operating System Concepts – 9th Edition 7.33 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.34 Silberschatz, Galvin and Gagne ©2013
Single Instance of Each Resource Type Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 9th Edition 7.35 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013
Several Instances of a Resource Type Detection Algorithm
Available: A vector of length m indicates the number of 1. Let Work and Finish be vectors of length m and n, respectively
available resources of each type Initialize:
Allocation: An n x m matrix defines the number of resources (a) Work = Available
of each type currently allocated to each process
(b) For i = 1,2, …, n, if Allocationi 0, then
Request: An n x m matrix indicates the current request of
Finish[i] = false; otherwise, Finish[i] = true
each process. If Request [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
Operating System Concepts – 9th Edition 7.37 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.38 Silberschatz, Galvin and Gagne ©2013
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Operating System Concepts – 9th Edition 7.39 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.40 Silberschatz, Galvin and Gagne ©2013
Example (Cont.) Detection-Algorithm Usage
When, and how often, to invoke depends on:
P2 requests an additional instance of type C
How often a deadlock is likely to occur?
Request
How many processes will need to be rolled back?
ABC
one for each disjoint cycle
P0 000
P1 202 If detection algorithm is invoked arbitrarily, there may be many
P2 001 cycles in the resource graph and so we would not be able to tell
which of the many deadlocked processes “caused” the deadlock.
P3 100
P4 002
State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
Operating System Concepts – 9th Edition 7.41 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.42 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Process Termination Recovery from Deadlock: Resource Preemption
Abort one process at a time until the deadlock cycle is eliminated Rollback – return to some safe state, restart process for that
state
In which order should we choose to abort?
1. Priority of the process Starvation – same process may always be picked as victim,
include number of rollback in cost factor
2. How long process has computed, and how much longer to
completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
Operating System Concepts – 9th Edition 7.43 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 7.44 Silberschatz, Galvin and Gagne ©2013
End of Chapter 7
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013