Unit 3 Chapter2 Deadlock Notes
Unit 3 Chapter2 Deadlock Notes
Deadlocks
Deadlocks
• Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and
waiting for another resource held by some other process.
Similar situation occurs in operating systems when there are two or more processes hold some
resources and wait for resources held by other(s).
Here is an example of a situation where deadlock can occur
Deadlock Situation
System Model
• A system consist of finite number of resources. (For ex: memory, printers, CPUs).
• These resources are distributed among number of processes.
• A process must
request a resource before using it and
release the resource after using it.
• The process can request any number of resources to carry out a given task.
• The total number of resource requested must not exceed the total number of resources available.
• In normal operation, a process must perform following tasks in sequence:
1) Request
If the request cannot be granted immediately (for ex: the resource is being used by
another process), then the requesting-process must wait for acquiring the resource.
For example: open( ), malloc( ), new( ), and request( )
2) Use
3) Release
Deadlock Characterization
In a deadlock, processes never finish executing, and system resources are tied up, preventing other
jobs from starting.
Necessary Conditions
1) Mutual Exclusion
If any other process requests this resource, then the requesting-process must wait for the
resource to be released.
4) Circular Wait
Resource-Allocation-Graph
• The resource-allocation-graph (RAG) is a directed graph that can be used to describe the deadlock
situation.
• RAG consists of a
→ set of vertices (V) and
Here, the request for Rj from Pi can be granted only if the converting request-edge to
assignment-edge do not form a cycle in the resource-allocation graph.
1) Pictorially,
→ We represent each process Pi as a circle.
→ We represent each resource-type Rj as a rectangle.
2) As shown in below figures, the RAG illustrates the following 3 situation:
3) 1) RAG with a deadlock
2) RAG with a cycle and deadlock
(a) Resource allocation Graph (b) With a deadlock (c) with cycle but no deadlock
Conclusion:
1) Deadlock prevention or avoidance - Do not allow the system to get into a deadlocked state.
2) Deadlock detection and recovery - Abort a process or preempt some resources when
deadlocks are detected.
3) Ignore the problem all together - If deadlocks only occur once a year or so, it may be better
to simply let them happen and reboot the system.
• In order to avoid deadlocks, the system must have additional information about all processes.
• In particular, the system must know what resources a process will or may request in the future.
• Deadlock detection is fairly straightforward, but deadlock recovery requires either aborting processes
or preempting resources.
• If deadlocks are neither prevented nor detected, then when a deadlock occurs the system will
gradually slow down.
Deadlock-Prevention
Deadlocks can be eliminated by preventing at least one of the four required conditions:
a. Mutual exclusion
b. Hold-and-wait
c. No preemption
d. Circular-wait.
Mutual Exclusion
The processes must be prevented from holding one or more resources while simultaneously
waiting for one or more other resources.
Protocol-1
o Each process must be allocated with all of its resources before it begins execution.
o All the resources (tape drive, disk files and printer) are allocated to the process at the
beginning.
Protocol-2
o A process must request a resource only when the process has none.
o Initially, the process is allocated with tape drive and disk file.
o The process performs the required operation and releases both tape drive and disk file.
o Then, the process is again allocated with disk file and the printer
o Again, the process performs the required operation & releases both disk file and the
printer.
No Preemption
• If a process is holding some resources and requests another resource that cannot be immediately
allocated to it, then all resources currently being held are preempted.
• The preempted resources are added to the list of resources for which the process is waiting.
• The process will be restarted only when it regains the old resources and the new resources that it is
requesting.
Protocol-2
• When a process request resources, we check whether they are available or not.
• These 2 protocols may be applicable for resources whose states are easily saved and restored, such
as registers and memory.
• But, these 2 protocols are generally not applicable to other devices such as printers and tape drives.
Circular-Wait
Protocol-1
Protocol-2
Require that whenever a process requests a resource, it has released resources with a lower
number.
One big challenge in this scheme is determining the relative ordering of the different resources.
Deadlock Avoidance
• The general idea behind deadlock avoidance is to prevent deadlocks from ever happening.
• Deadlock-avoidance algorithm
→ requires more information about each process, and
→ tends to lead to low device utilization.
• For example:
→ In simple algorithms, the scheduler only needs to know the maximum number of each
resource that a process might potentially use.
→ In complex algorithms, the scheduler can also take advantage of the schedule of exactly what
resources may be needed in what order.
A deadlock-avoidance algorithm dynamically examines the resources allocation state to ensure
that a circular-wait condition never exists.
Safe State
• A state is safe if the system can allocate all resources requested by all processes without entering a
deadlock state.
• A state is safe if there exists a safe sequence of processes {P0, P1, P2, ..., PN} such that
the requests of each process(Pi) can be satisfied by the currently available resources.
• If a safe sequence does not exist, then the system is in an unsafe state, which may lead to deadlock.
• All safe states are deadlock free, but not all unsafe states lead to deadlocks.
Resource-Allocation-Graph Algorithm
• If resource categories have only single instances of their resources, then deadlock states can be
detected by cycles in the resource-allocation graphs.
• In this case, unsafe states can be recognized and avoided by augmenting the resource-allocation
graph with claim edges (denoted by a dashed line).
• Claim edge Pi → Rj indicated that process Pi may request resource Rj at some time in future.
3) The request for Rj from Pi can be granted only if the converting request edge to assignment
edge do not form a cycle in the resource allocation graph.
• To apply this algorithm, each process Pi must know all its claims before it starts executing.
• Conclusion:
If no cycle exists, then the allocation of the resource will leave the system in a safe state.
If cycle is found, system is put into unsafe state and may cause a deadlock.
Though R2 is currently free, we cannot allocate it to P2 as this action will create a cycle in the
graph as shown in Figure.
This cycle will indicate that the system is in unsafe state: because, if P1 requests R2 and P2
requests R1 later, a deadlock will occur.
(a) For deadlock avoidance (b) an unsafe state
• Problem:
The resource-allocation graph algorithm is not applicable when there are multiple instances
for each resource.
• Solution:
Banker's Algorithm
• This algorithm is applicable to the system with multiple instances of each resource types.
else
the process must wait until some other process releases enough resources.
• Assumptions:
1) Available [m]
2) Max [n][m]
This matrix indicates the maximum demand of each process of each resource.
If Max[i,j]=k, then process Pi may request at most k instances of resource type Rj.
3) Allocation [n][m]
4) Need [n][m]
If Need[i,j]=k, then Pi may need k more instances of resource Rj to complete its task.
So, Need[i,j] = Max[i,j] - Allocation[i]
Safety Algorithm
• This algorithm is used for finding out whether a system is in safe state or not.
• Assumptions:
Work is a working copy of the available resources, which will be modified during the analysis.
Finish is a vector of boolean values indicating whether a particular process can finish.
Step 1:
Let Work and Finish be two vectors of length m and n respectively.
Initialize:
Work = Available
Finish[i] = false for i=1,2,3,…….n
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Need i <= Work.
If no such i exist, then go to step 4
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2
Step 4:
If Finish[i] = true for all i, then the system is in safe state.
Resource-Request Algorithm
• This algorithm determines if a new request is safe, and grants it only if it is safe to do so.
• When a request is made ( that does not exceed currently available resources ), pretend it has been
granted, and then see if the resulting state is a safe one. If so, grant the request, and if not, deny the
request.
Step 1:
If Request(i) <= Need(i)
then
go to step 2
else
raise an error condition, since the process has exceeded its maximum claim.
Step 2:
If Request(i) <= Available
then
go to step 3
else
Pi must wait, since the resources are not available.
Step 3:
If the system want to allocate the requested resources to process Pi then modify the
state as follows:
Available = Available – Request(i)
Allocation(i) = Allocation(i) + Request(i)
Need(i) = Need(i) – Request(i)
Step 4:
If the resulting resource-allocation state is safe,
then i) transaction is complete and
ii) Pi is allocated its resources.
Step 5:
If the new state is unsafe,
then i) Pi must wait for Request(i) and
ii) old resource-allocation state is restored.
An Illustrative Example
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 3 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Solution (i):
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Solution (ii):
……P0………P1……..P2……..P3……P4…..
…….P0………P1…….P2…….P3……..P4……
……P0………P1……P2………..P3…….P4….
……P0………P1……P2………P3…….P4…..
…..P0…….P1………P2…….P3…….P4….
Finish= | true | true | false | true | true |
…..P0…….P1………P2…….P3…….P4….
A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0
P1 3 0 2 3 2 2
Need
A B C
P0 7 4 3
P1 0 2 0
P2 6 0 0
P3 0 1 1
P4 4 3 1
• To determine whether this new system state is safe, we again execute Safety
algorithm. Step 1: Initialization
Here, m=3, n=5
…..P0………P1………P2……….P3…….P4….
So P0 must wait.
……P0………P1…….P2………P3…….P4……
…..P0……….P1……P2……..P3…….P4…...
……P0………P1……P2………P3…….P4….
…..P0…….P1………P2…….P3…….P4….
Conclusion: Since the system is in safe sate, the request can be granted.
Deadlock Detection
• If all the resources have only a single instance, then deadlock detection-algorithm can be defined
using a wait-for-graph.
2) Rq → Pj.
• For example:
• A deadlock exists in the system if and only if the wait-for-graph contains a cycle.
• To detect deadlocks, the system needs to
→ maintain the wait-for-graph and
• Problem: However, the wait-for-graph is not applicable to a multiple instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple instance of a resource type.
• Assumptions:
Let ‘n’ be the number of processes in the system
1) Available [m]
This vector indicates the no. of available resources of each type.
2) Allocation [n][m]
3) Request [n][m]
If Request [i, j] = k, then process Pi is requesting k more instances of resource type Rj.
Step 1:
Let Work and Finish be vectors of length m and n respectively.
a) Initialize Work = Available
b) For i=0,1,2……….n
if Allocation(i) != 0
then
Finish[i] = false;
else
Finish[i] = true;
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Request(i) <= Work.
If no such i exist, goto step 4.
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2.
Step 4:
If Finish[i] = false for some i where 0 < i < n, then the system is in a deadlock state.
Detection-Algorithm Usage
• Solution 1:
he deadlock-algorithm must be executed whenever a request for allocation cannot be
granted immediately.
In this case, we can identify
→ set of deadlocked-processes and
→ specific process causing the deadlock.
• Solution 2:
The deadlock-algorithm must be executed in periodic intervals.
For example:
→ once in an hour
→ whenever CPU utilization drops below certain threshold
Process Termination
• These resources are given to other processes until the deadlock-cycle is broken.
• Three issues need to be considered:
1) Selecting a victim
Which resources/processes are to be pre-empted (or blocked)?
2) Rollback
If a resource is taken from a process, the process cannot continue its normal execution.
3) Starvation
Problem: In a system where victim-selection is based on cost-factors, the same process may
be always picked as a victim.
As a result, this process never completes its designated task.
Solution: Ensure a process is picked as a victim only a (small) finite number of times.
Exercise Problems
A B C A B C A B C
P0 0 0 2 0 0 4 1 0 2
P1 1 0 0 2 0 1
P2 1 3 5 1 3 7
P3 6 3 2 8 4 2
P4 1 4 3 1 5 7
Solution (i):
• The content of the matrix Need is given
by Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 0 0 2
P1 1 0 1
P2 0 0 2
P3 2 1 0
Solution (ii): P4 0 1 4
……P0………P1……..P2……..P3……P4…..
……P0………P1…….P2………..P3……P4….
…….P0…….P1……P2………P3……...P4…
……P0…….P1…….P2……….P3……P4….
…..P0……..P1……P2……..P3…….P4….
……P0…….P1……..P2……P3…….P4….
A B C A B C A B C
P0 0 0 2 0 0 4 1 0 0
P1 1 0 0 2 0 1
P2 1 3 7 1 3 7
P3 6 3 2 8 4 2
P4 1 4 3 1 5 7
Need
A B C
P0 0 0 2
P1 1 0 1
P2 0 0 0
P3 2 1 0
P4 0 1 4
• To determine whether this new system state is safe, we again execute Safety
algorithm. Step 1: Initialization
....P0………P1…….P2……….P3…..P4…..
So P1 must wait.
.....P0…….P1……..P2…..P3.……..P4….
....P0……….P1…….P2…….P3……..P4…
....P0……….P1…….P2…….P3…….P4…
....P0……….P1…….P2……P3…….P4…
Conclusion: Since the system is in safe sate, the request can be granted.
2) For the following snapshot, find the safe sequence using Banker's algorithm: The number of
resource units is (A, B, C) which are (7, 7, 10) respectively.
A B C A B C A B C
P1 2 2 3 3 6 8 7 7 10
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4
Solution:
Need
A B C
P1 1 4 5
P2 2 3 0
P3 2 2 0
….P1………..P2……..P3…
……P1……P2……….P3….
…..P1……P2…….P3……
……P1……P2…….P3….
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
i) What is the content of the matrix need?
ii) Show that the system is not deadlock by generating one safe sequence
iii) At instance t, P2 makes one additional for instance of type C. Show that the system is
deadlocked if the request is granted. Write down deadlocked-processes.
Solution (i):
Need
A B C
P0 0 0 0
P1 0 0 2
P2 0 0 0
P3 0 0 0
P4 0 0 0
Solution (ii):
……P0………P1…….P2……….P3……P4…
…..P0………P1…….P2…….P3……..P4…
....P0……P1…….P2……….P3…….P4…
…P0……P1………P2…….P3…….P4…
....P0……P1…….P2…….P3…….P4….
...P0…….P1……..P2…….P3…….P4…
Conclusion: Yes, the system is currently in a safe state. Hence there is no deadlock in the system.
A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 1 5 2 0
P2 1 0 0 0 1 7 5 0
P3 1 3 5 4 2 3 5 6
P4 0 6 3 2 0 6 5 2
P5 0 0 1 4 0 6 5 6
Solution (i):
Need
A B C D
P1 0 0 0 0
P2 0 7 5 2
P3 1 0 0 2
P4 0 0 2 0
P5 0 6 4 2
Solution (ii):
....P1………P2…….P3……….P4…..P5…..
Finish = | false | false | false | false | false
| Step 2: For i=1
....P1………P2…….P3…….P4……..P5…
....P1………P2…….P3…….P4……..P5…
....P1………P2…….P3…….P4…….P5…
....P1………P2…….P3…….P4……P5…
....P1………P2…….P3…….P4……P5…
Finish = | true | true | true | true | true |
A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 1 1 0 0
P2 1 4 2 0 1 7 5 0
P3 1 3 5 4 2 3 5 6
P4 0 6 3 2 0 6 5 2
P5 0 0 1 4 0 6 5 6
Need
A B C D
P1 0 0 0 0
P2 0 3 3 2
P3 1 0 0 2
P4 0 0 2 0
P5 0 6 4 2
....P1………P2…….P3……...P4……..P5…
....P1……P2……….P3…….P4……..P5….
....P1………P2…….P3…….P4…….P5….
....P1………P2…….P3…….P4……P5….
....P1………P2……P3…….P4……P5….
Conclusion: Since the system is in safe sate, the request can be granted.
5) Consider a system containing ‘m’ resources of the same type being shared by ‘n’
processes. Resources can be requested and released by processes only one at a time. Show
that the system is deadlock free if the following two conditions hold:
• If there exists a deadlock state, then A=m because there's only one kind of resource and resources
can be requested and released only one at a time.
• From condition (ii), N+A = M<m+n
6) Consider the traffic deadlock depicted in the figure given below, explain that the four
necessary conditions for dead lock indeed hold in this examples.
Ans:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption and
4) Circular-wait.
• The mutual exclusion condition holds since only one car can occupy a space in the roadway.
• Hold-and-wait occurs where a car holds onto its place in the roadway while it waits to advance in the
roadway.
• A car cannot be removed (i.e. preempted) from its position in the roadway.
• Lastly, there is indeed a circular-wait as each car is waiting for a subsequent car to advance.
• The circular-wait condition is also easily observed from the graphic.