Deadlocks
The Deadlock Problem
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
2
System Model
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
Request- Requesting process must wait until it
acquire the resource
use- Process can operate on the resource
Release- process releases the resource.
3
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a 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, …, 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 P0 is waiting for a resource that is
held by P0.
4
NECESSARY CONDITIONS
ALL of these four must happen simultaneously for a deadlock to occur:
Mutual exclusion
One or more than one resource must be held by a process in a non-
sharable (exclusive) mode.
Hold and Wait
A process holds a resource while waiting for another resource.
No Preemption
There is only voluntary release of a resource - nobody else can make a
process give up a resource.
Circular Wait
Process A waits for Process B waits for Process C .... waits for Process
A.
5
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
R = {R1, R2, …, Rm}, the set consisting of all resource
types in the system
request edge :directed edge P1 → Rj
assignment edge : directed edge Rj → Pi
6
Resource-Allocation Graph (Cont.)
Process
Resource Type with 4 instances
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
7
Example of a Resource Allocation Graph
8
Resource Allocation Graph With A Deadlock
P1→ R1 → P2 → R3 → P3 → R2 → P1
P2 → R3 → P3 → R2 → P2
9
Graph With A Cycle But No Deadlock
P1→ R1 → P3 → R2 → P1
10
Basic Facts
If graph contains no cycles no deadlock
If graph contains a cycle
if only one instance per resource type, then
deadlock
if several instances per resource type,
possibility of deadlock
11
Methods for handling deadlocks
Ensure that the system will never enter a deadlock state.
Prevention: Prevent any one of the 4 conditions from
happening.
Avoidance : Examines resource allocation state to ensure that
circular wait condition can never exist.
Resource allocation state is determined by number of available &
allocated resources and the maximum demand of processes.
Allow the system to enter a deadlock state and then
detect and
recover
12
Deadlock prevention
1. Mutual exclusion:
• Hold for non-sharable resources ex: printer can’t be shared
simultaneously by several processes.
• not required for sharable resources
• Can’t deny mutual exclusion condition
2. Hold and wait:
a. Collect all resources before execution→ resource utilization is
low
b. Allow a process to request resources only when it has none →
starvation possible
• A process may request some resources & use them. Before
it can request additional resource, it must release all the
resources that is currently allocated.
13
Deadlock prevention
3. No preemption:
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.
4. Circular wait:
impose a total ordering of all resource types,
each process requests resources in a strictly increasing order or
strictly decreasing order
14
15
16
17
Avoidance algorithms
Single instance of a resource type → resource
allocation graph algorithm
Multiple instances of a resource type → banker’s
algorithm
18
Resource-Allocation Graph Scheme
Claim edge Pj --> Rj ==>process Pj may request resource
Rj;(-->) in future .
Claim edge →request edge , when a process requests a
resource.
Request edge →assignment edge , when the resource is
allocated to the process.
When a resource is released by a process, assignment
edge reconverts to a claim edge.
Resources must be claimed a priori in the system.
19
Resource-Allocation Graph
Unsafe state in resource-
allocation graph
20
Resource-Allocation Graph Algorithm
Pi → Rj
The request can be granted only if
converting the request edge to an
assignment edge does not result in the
formation of a cycle in the resource
allocation graph
21
Banker’s Algorithm
• Handles Multiple instances of some resources.
• Each process must a priori claim maximum use.
• When a process requests a resource it may
have to wait.
• When a process gets all its resources it must
return them in a finite amount of time.
22
How many instances of each resource
each process can max request [max]
How many instances of each resource
each process currently holds[allocation]
How many instances of each resource is
available in the system [available]
23
24
25
Resource-Request Algorithm for Process Pi
If Requesti [j] = k then process Pi wants k instances of
resource type Rj.
1. If Requesti ≤ Needi go to step 2. else, raise error
condition, since process has exceeded its maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must
wait, since resources are not available.
3. Pretend to allocate requested resources to Pi by
modifying the state as follows:
26
Resource-Request Algorithm for Process Pi
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi = Needi – Requesti;
➢ If resulting resource allocation state is safe ==>
resources are allocated to Pi.
➢ If resulting resource allocation state is unsafe==>
Pi must wait, and the old resource-allocation
state is restored
27
Example of Banker’s Algorithm
5 processes P0 … P4;
3 resource types: A (10 instances), B (5), and C (7)
Snapshot at time T0:
System is in safe state -- sequence < P1, P3, P4, P2, P0>
28
Example: P1 Request (1,0,2)
Check that Request ≤ Available (that is, (1,0,2) ≤
(3,3,2) ==> true.
< P1, P3, P4, P0, P2> satisfies safety requirement.
Hence request to P1 can be immediately granted.
29
Deadlock Detection
o Allow system to enter deadlock state
o Detection algorithm
o Recovery scheme
❑Single instance
❑Several instances
30
Single Instance of Each Resource Type
Maintain wait-for graph
Nodes are processes.
Pi → Pj if Pi is waiting for Pj.
Periodically invoke an algorithm that
searches for a cycle in the graph. If there
is a cycle, there exists a deadlock.
31
32
33
34
Example of Detection Algorithm
• 5 processes P0 … P4;
• 3 resource types: A (7 instances), B (2), and C (6)
• Snapshot at time T0:
Sequence <P0, P2, P3, P1, P4>
35
36
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.
37
Recovery from Deadlock
1. Process Termination (Pessimistic Approach)
2. Resource Preemption(Optimistic Approach)
38
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?
what the priority of the process is
How long process has computed, and how much longer to
completion.
how many & what type of resources the process has used
How many more resources process needs to complete
How many processes will need to be terminated
Is process interactive or batch?
39
Resource 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.
40