Lecture 6- Deadlocks (1) (4)
Lecture 6- Deadlocks (1) (4)
Lecture 6: Deadlocks
Operating Systems
System Model
• A system contains a finite number of resource types (R1, R2, . . .,
Rm) to be distributed among competing processes
• The resource types are partitioned in to several types (e.g files,
I/O devices, CPU cycles, memory), each having a number of
identical instances
• A process must request a resource before using it and release it
after making use of it. Each process utilizes a resource as follows:
• Request
• A process requests for an instance of a resource type. If
the resource is free, the request will be granted. Otherwise
the process should wait until it acquires the resource
• Use
• The process uses the resource for its operations
• Release
• The process releases the resource
May 11, 2025 3
Operating Systems
Deadlock
• Deadlock can be defined as a permanent blocking of
processes that either compete for system resources or
communicate with each other
• The set of blocked processes each hold a resource and wait
to acquire a resource held by another process in the set
• All deadlocks involve conflicting needs for resources by two
or more processes
• A set of processes or threads is deadlocked when each
process or thread is waiting for a resource to be freed which
is controlled by another process
• Example 1
• Suppose a system has 2 disk drives
• If P1 is holding disk 2 and P2 is holding disk 1 and if P1
requests for disk 1 and P2 requests for disk 2, then
deadlock
May 11, 2025 occurs 4
Operating Systems
Traffic gridlock is an everyday
example of a deadlock situation.
When two trains approach each other at a crossing,
both shall come to a full stop and neither shall start up
again until the other has gone
Operating Systems
Deadlock characterization
Deadlock can arise if four conditions hold
simultaneously in a system:
1. Mutual exclusion: only one process at a time can use a
resource. No process can access a resource unit that has been
allocated to another process
2. Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other processes.
3. No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed its
task.
4. 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
May 11, 2025 6
Operating Systems
Resource Allocation Graph
• Deadlock can be better described by using a directed
graph called resource allocation graph
• The graph consists of 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
• If a Resource Allocation Graph contains a cycle, then a
deadlock may exist
May 11, 2025 7
Operating Systems
Resource Allocation Graph /RAG
• (cont.)
Diagrammatically, processes and resources in RAG are
represented as follow:
• Process p
• Pi requests instance of Rj Pi Rj
• Pi is holding an instance of RP
j i
Rj
Operating Systems
Example of Resource Allocation
Graph
Operating Systems
Graph With A Cycle But No
Deadlock
Basic Facts
• If graph contains no cycles then
there’s no deadlock
Operating Systems
Example of Resource Allocation
Graph • The RAG shown here tells us about the
following situation in a system:
• P= {P1, P2, P3}
• R= {R1,R2, R3, R4}
• E ={P1R1,P2R3, R1P2, R2P1,R3P3}
• P3 is holding an instance of R3
May 11, 2025 11
Operating Systems
Example of Resource Allocation
Graph
• Resource Allocation Graph With a Deadlock
Operating Systems
Methods for handling
Deadlocks
• Deadlock problems can be handled in one of the following 3
ways:
Operating Systems
Deadlock
Prevention
• By ensuring at least one of the necessary conditions for deadlock
will not hold, deadlock can be prevented.
This is mainly done by restraining how requests for
resources can be made
Operating Systems
Deadlock Prevention (contd.)
1. Mutual Exclusion – This is not required for sharable resources;
however to prevent a system from deadlock, the mutual exclusion
condition must hold for non-sharable resources
Operating Systems
Deadlock Prevention (contd.)
3. No Preemption
• If a process holding certain resources is denied further request,
that process must release its original resources allocated to it
• If a process requests a resource allocated to another process
waiting for some additional resources, and the requested
resource is not being used, then the resource will be
preempted from the waiting process and allocated to
the requesting process
• 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
• This approach is practical to resources whose state can easily
saved and retrieved easily
May 11, 2025 16
Operating Systems
Deadlock Prevention
(contd.)
4. Circular Wait
• A linear ordering of all resource types is defined and each
process requests resources in an increasing order of
enumeration
Operating Systems
Deadlock Avoidance
• Deadlock avoidance scheme requires each process to declare
the maximum number of resources of each type that it
may need in advance
Operating Systems
Deadlock avoidance: Safe State
Basic Facts
• If a system is in a safe
state, then there are no
deadlocks.
• If a system is in unsafe
state, then there is a
possibility of deadlock
• Deadlock avoidance
method ensures that a
system will never enter an
unsafe
May 11, 2025
state 19
Operating Systems
Deadlock Avoidance Algorithms
• Based on the concept of safe state, we can define
algorithms that ensures the system will never
deadlock.
Operating Systems
Deadlock Avoidance Algorithms (contd.)
Resource-Allocation Graph Scheme
• A new type of edge (Claim edge), in addition to the request and
assignment edge is introduced.
• Claim edge Pi Rj indicates that process Pi may request resource Rj at
some point in the future. The edge resembles a request edge but is
represented by a dashed line in the graph
• Claim edge is converted to request edge when a process requests a
resource
• Request edge is converted to an 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
• If no cycle exists in the allocation, then system is in safe state
otherwise the system is in unsafe state
• Resources must be claimed a priori in the system
• i.e, before a process starts executing, all its claim edge must show up
in the allocation graph
May 11, 2025 21
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Resource-Allocation
• Suppose Graph
that process Pi requests Algorithm
a resource Rj
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Resource-Allocation Graph algorithm
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
• This algorithmBanker’s Algorithm
is used when there are multiple instances of
resources
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
• The following data structures
Banker’s are used in the algorithm:
Algorithm
Let n = number of processes, and
m = number of resources types.
• Available: Vector of length m.
• If available [j] = k, there are k instances of resource type Rj
available.
• Max: n x m matrix.
• If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj.
• Allocation: n x m matrix.
• If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
• Need: n x m matrix.
• If Need[i,j] = k, then Pi may need k more instances of Rj to complete
its task
May 11, 2025 25
Need [i,j] = Max[i,j] – Allocation [i,j]
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
The algorithm is asBanker’s
follows: Algorithm
1.Process pi makes requests for resources. Let Request(i) be the
corresponding request vector. So, if pi wants k instances of
resource type rj, then Request(i)[j] = k
2.If Request(i) !≤ Need(i), there is an error.
3.Otherwise, if Request(i) !≤ Available, then pi must wait
4.Otherwise, Modify the data structures as follows :
• Available = Available - Request(i)
• Allocation(i) = Allocation(i) + Request(i)
• Need(i) = Need(i) - Request(i)
5.Check whether the resulting state is safe. (Use the safety
algorithm presented in the next slide)
6.If the state is safe, do the allocation. Otherwise, pi must wait for
Request(i)
Operating Systems
Deadlock Avoidance Algorithms (contd.)
Safety
•Algorithm
It is used to identify whether or not a system is in a safe state.
The algorithm can be described as follow:
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish [i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
May 11, 2025 27
Operating Systems
Deadlock Avoidance Algorithms (contd.)
Resource-Request Algorithm
• This algorithm determines if a request can be safely granted. The
algorithm is described below
• If Request = request vector 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. Otherwise, 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:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• If safe the resources are allocated to Pi.
May 11,
If unsafe Pi must wait, and the old resource-allocation state
• 2025 28
is
restored
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Examples of Banker’s algorithm:
Banker’s Algorithm
• Assume a system has
• 5 processes P0 through P4;
• 3 resource types:
A (10 instances), B (5instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
May 11, 2025 29
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Example (contd.)Banker’s Algorithm
• The content of the matrix Need is defined to be Max –
Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3,
P4May
, P11,2,2025
P0> satisfies safety criteria 30
Operating Systems
Deadlock Avoidance Algorithms
(contd.)
Example: P1 Request (1,0,2)
Banker’s Algorithm
• Check that Request Available (that is, (1,0,2) (3,3,2) true
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 301 600
P3 211 011
P4 002 431
• Executing safety algorithm shows that sequence < 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?
Operating Systems
May 11, 2025 32
Operating Systems
Deadlock
Detection
• If a system does not implement either deadlock
prevention or avoidance, deadlock may occur. Hence
the system must provide
Operating Systems
Deadlock Detection:
1. Single Instance of Each Resource Type
• If there are single instances of each resources in a system, then
an algorithm that uses a type of resource allocation graph called
wait-for graph will be used
• The wait-for graph is obtained from the resource allocation graph
by removing the resource nodes and collapsing the
corresponding edges
• If a process Pi points to Pj I in a wait-for graph, it indicates that
A Pi is waiting for Pj to release a resource Pj needs
Operating Systems
Deadlock Detection:
1. Single Instance of Each Resource Type
(contd.)
Operating Systems
Deadlock Detection:
2. Several Instances of a Resource Type
• When there are multiple instances of a resource type in a
resource allocation system, the wait-for graph is not applicable.
Hence, a deadlock detection algorithm is used
• The algorithm uses several data structures similar to the ones in
banker’s algorithm
• Available: A vector of length m indicates the number of
available resources of each type.
• Allocation: An n x m matrix defines the number of resources of
each type currently allocated to each process.
• Request: An n x m matrix indicates the current request of each
process. If Request [ij] = k, then process Pi is requesting k more
instances of resource type. Rj.
Operating Systems
Deadlock Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi 0, then
Finish[i] = false;otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 i n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked.
May 11, 2025 37
Operating Systems
Deadlock Detection Algorithm
(contd.)
•Example
Five processes P0 through P4; three resource types
• A (7 instances), B (2 instances), and C (6 instances)
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all i
May 11, 2025 38
Operating Systems
Deadlock Detection Algorithm
(cont.)
•Example
P2 requests an additional instance of type C.
Request
ABC
P0 000
P1 201
P2 001
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.
May 11, 2025 39
Operating Systems
Deadlock Detection Algorithm
•Usage
When, and how often, to invoke the detection algorithm depends on:
1.How often a deadlock is likely to occur?
2.How many processes will be affected (need to be rolled back)?
Operating Systems
Recovery from
•Deadlock
Once a deadlock has been detected, recovery strategy is needed. There
are two possible recovery approaches:
• Process termination
• Resource preemption
Process Termination
• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is eliminated
Operating Systems
Recovery from
Deadlock
Resource Preemption
• In this recovery strategy, we successively preempt resources and allocate
them to another process until the deadlock is broken
• While implementing this strategy, there are three issues to be considered
• Selecting a victim – which resources and process should be selected
to minimize cost just like in process termination. The cost factors may
include parameters like the number of resources a deadlocked
process is holding, number of resources it used so far
• Rollback – if a resource is preempted from a process, then it can not
continue its normal execution
• The process must be rolled back to some safe state and started
• Starvation – same process may always be picked as victim several
times. As a result, starvation may occur. The best solution to this
problem is to only allow a process to be picked as a vicitim for a
limited finite number of times. This can be done by including the
number of rollback in the cost factor
Operating Systems
Exercises
1. For each of the resource allocation graphs below, determine whether
there is a deadlock or not. Give explanations for each of your answers.
a. b
.
c d.
.
Operating Systems
Exercises(cont…)
2. There are four processes in a system and they are going to share nine
tape drives. Their current and maximum number of allocation numbers
are as follows :
Proces Allocatio Maximu a. Is the system in a safe state?
s n m
Why or why not?
P1 3 6
b. Is the system deadlocked? Why
P2 1 2 or why not?
P3 4 9
P4 0 2
Operating Systems
Exercises(cont…)
4. Given the resource allocation graph below:
a. Apply the deadlock detection
algorithm and either indicate why
the system is deadlocked, or specify
a safe allocation sequence
Operating Systems