Module 3
Chapter 2: Deadlock
Contents
• The Deadlock Problem
• System model
• deadlock characterization
• Methods for Handling Deadlocks
• deadlock prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
Objectives
• To develop a description of deadlocks, which prevent sets of
concurrent processes from completing their tasks.
• To present a number of different methods for preventing or avoiding
deadlocks in a computer system
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
• Example
• semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Bridge Crossing Example
• Traffic only in one direction
• Each section of a bridge can be viewed as 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
Note : Most OS do not prevent or deal with deadlocks
Deadlock Principles
ᾆ A deadlock is a permanent blocking of a set of threads
ᾆ a deadlock can happen while threads/processes are competing for
system resources or communicating with each other
Illustration of a deadlock
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
• use
• release
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.
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.
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
Resource-Allocation Graph (Cont.)
• Process
• Resource Type with 4 instances
• Pi requests instance of Rj P
i
Rj
• Pi is holding an instance of Rj P
i
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A
Deadlock
P1ᾆ R1 ᾆ P2 ᾆ R3 ᾆ P3 ᾆ R2 ᾆ P1
P2 ᾆ R3 ᾆ P3 ᾆ R2 ᾆ P2
Graph With A Cycle But No Deadlock
P1ᾆ R1 ᾆ P3 ᾆ R2 ᾆ P1
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
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 : Allow all deadlock conditions, but calculate cycles about to happen and
stop dangerous operations.
• Allow the system to enter a deadlock state and then
• detect and
• recover
• Ignore the problem and pretend that deadlocks never occur in the system;
used by most OS, including UNIX.
Deadlock Prevention
1. Mutual exclusion:
• Hold for non-sharable resources ex: printer
• 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
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 an increasing order
Deadlock Avoidance
Safe State
Basic facts
Avoidance algorithms
• Single instance of a resource type ᾆ resource allocation graph
algorithm
• Multiple instances of a resource type ᾆ banker’s algorithm
Resource-Allocation Graph Scheme
• Claim edge Pi ᾆ Rj ==>process Pj may request resource Rj;(--
>).
• 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.
Resource-Allocation Graph
Unsafe state in resource-allocation graph
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
Banker’s Algorithm
• Multiple instances.
• 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.
Data Structures for the Banker’s Algorithm
Safety Algorithm
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:
Resource-Request Algorithm for Process
Pi
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi = Needi – Requesti;
ᾆIf safe ==> resources are allocated to Pi.
ᾆIf unsafe==> Pi must wait, and the old resource-allocation
state is restored
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,
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.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection
oAllow system to enter deadlock state
oDetection algorithm
oRecovery scheme
ᾆSingle instance
ᾆSeveral instances
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.
Resource allocation Graph and Wait-for
Graph
Several Instances of a Resource Type
Detection Algorithm
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>
Example (Cont.)
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
Recovery from Deadlock
1. Process Termination
2. Resource Preemption
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?
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.
Thank You