ch8 - Deadlocks - 2022
ch8 - Deadlocks - 2022
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 10th Edition 8.2 Silberschatz, Galvin and Gagne ©2018
Chapter Objectives
Illustrate how deadlock can occur when mutex locks are used
Define the four necessary conditions that characterize deadlock
Identify a deadlock situation in a resource allocation graph
Evaluate the four different approaches for preventing deadlocks
Evaluate approaches for recovering from deadlock
Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
The Deadlock Problem
If a resource requested by a process is unavailable, the process enters
a wait state. If the waiting processes never again change state because
the resources they request are held by other waiting processes, we
have 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 disk drives
• P1 and P2 each hold one disk drive and each needs another one
Operating System Concepts – 10th Edition 8.4 Silberschatz, Galvin and Gagne ©2018
Bridge Crossing Example
Operating System Concepts – 10th Edition 8.5 Silberschatz, Galvin and Gagne ©2018
System Model
A system consists of a finite number of resources to be distributed among a
number of competing threads
These resource can be of different types R1, R2, . . ., Rm
• e.g. CPU cycles, memory space, I/O devices
Each resource type Ri has number of identical instances Wi .
Each process/thread utilizes a resource as follows:
1. request
2. use
3. release
The request and release of resources may be system calls. For example:
• request() and release() of a device,
• open() and close() of a file,
• allocate() and free() memory system calls.
• wait() and signal() operations on semaphores,
• acquire() and release() of a mutex lock
Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018
Deadlock with Semaphores
Data:
• A semaphore S1 initialized to 1
• A semaphore S2 initialized to 1
Two processes P1 and P2
P1:
wait(s1)
wait(s2)
P2:
wait(s2)
wait(s1)
Operating System Concepts – 10th Edition 8.7 Silberschatz, Galvin and Gagne ©2018
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously in a system:
Operating System Concepts – 10th Edition 8.8 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph (Cont.)
Process/ Thread
Ti requests instance of Rj
Ti
Ti is holding an instance of Rj
Ti
Operating System Concepts – 10th Edition 8.10 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph Example
One instance of R1
Two instances of R2
One instance of R3
Three instance of R4
T1 holds one instance of R2 and is
waiting for an instance of R1
T2 holds one instance of R1, one
instance of R2, and is waiting for an
instance of R3
T3 is holds one instance of R3
Operating System Concepts – 10th Edition 8.11 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph with a Deadlock
T1 is holding an instance of
resource type R2 and is
waiting for an instance of
resource type R1.
T2 is holding an instance of
R1 and an instance of R2 and
is waiting for an instance of R3.
T3 is holding an instance of
R3 and is waiting for an
instance of R2.
2 cycles:
• T1 → R1 → T2 → R3 → T3 → R2 →
T1
• T2 → R3 → T3 → R2 → T2
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
Graph with a Cycle But no Deadlock
Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
Basic Facts
If graph contains no cycles no deadlock
If graph contains a cycle
1. if only one instance per resource type, then deadlock
2. if several instances per resource type, possibility of deadlock
Operating System Concepts – 10th Edition 8.14 Silberschatz, Galvin and Gagne ©2018
Methods for Handling Deadlocks
Generally, we can deal with the deadlock problem in one of three
ways:
3. We can allow the system to enter a deadlocked state, detect it, and
recover (Deadlock detection)
Operating System Concepts – 10th Edition 8.15 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:
Hold and Wait – To ensure that this condition never occurs in the
system, we must guarantee that whenever a process requests a resource,
it does not hold any other resources
How?
• 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 allocated to it. (the process can’t take another resource
until it leaves the current resource)
In both protocols, we may get low resource utilization and
starvation is possible
Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
No Preemption:
• To ensure that this condition does not hold, we can use the following
protocol:
1. If a process that is holding some resources requests another resource that
cannot be immediately allocated to it (i.e. it must wait), then all resources
currently being held are released (preempted)
2. Preempted resources are added to the list of resources for which the
process is waiting
3. 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
Operating System Concepts – 10th Edition 8.17 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
Circular Wait (cont.)
• We let R = {R1, R2, ..., Rm} be the set of resource
types.
• Each resource type is assigned a unique integer
number, which allows us to compare two resources and
to determine whether one precedes another in our
ordering.
• We define a one-to-one function F: R→N, where N is
the set of natural numbers
Example:
F(tape drive) = 1 , F(disk drive) = 5, F(printer) = 12
A process can initially request any number of instances of
a resource type Ri and now sending the request for Rj, the
resource will be assigned if and only if F(Rj) > F(Ri).
Side effects of deadlock prevention:
low device utilization and reduced system throughput.
Operating System Concepts – 10th Edition 8.18 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance
Look at every single resource request and decide whether to allow that
request to process.
The action will be taken before occurring deadlock.
Operating System Concepts – 10th Edition 8.19 Silberschatz, Galvin and Gagne ©2018
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Operating System Concepts – 10th Edition 8.20 Silberschatz, Galvin and Gagne ©2018
Single Instance of Each Resource Type
As we know, a deadlock exists in the system if and only if the wait-for
graph contains a cycle.
To detect deadlocks, the system needs to:
Operating System Concepts – 10th Edition 8.21 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
Detection-Algorithm Usage
When, and how often, to invoke detection algorithm depends on:
1. How often is a deadlock likely to occur?
2. How many threads will be affected by deadlock
when it happens?
(i.e. How many processes will need to be rolled back?
one for each disjoint cycle)
Operating System Concepts – 10th Edition 8.23 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Process/Thread Termination
Operating System Concepts – 10th Edition 8.24 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 10th Edition 8.25 Silberschatz, Galvin and Gagne ©2018
End of Chapter 8
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018