Lecture Handling Deadlock
Lecture Handling Deadlock
Operating Systems
CS F372
by
Dr. Shubhangi
Dept. of CS and IS
27 November 2021 1
OPERATING SYSTEMS
LECTURE
HANDLING DEADLOCKS
27 November 2021 2
OS as a Resource Manager
Processes compete for finite number of
resources
Resources
Memory space
CPU Cycles
Files
I/O Devices
Instances
27 November 2021 3
Bridge Crossing Example
Example 2
System has 1 disk drive and 1 CD drive
P1 and P2 each hold one resource and each needs another one
Example 3
Multithreading
Thread1 Thread2
Lock(data item1) Lock(data item2)
Lock(data item2) Lock(data item1)
. .
. .
UnLock(data item2) UnLock(data item1)
UnLock(data item1) UnLock(data item2)
27 November 2021 6
Example of Deadlock
Space is available for allocation of
200Kbytes, and the following sequence of
events occur
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
27 November 2021 9
Resource Allocation
Request
If the request cannot be granted immediately then the
requesting process must wait until it can acquire the
resource.
Use
Process operates on resource.
Release
Process release the resource.
Resources
Physical resource -- Printer,Memory space…..
27 November 2021 10
Reusable Resources
Used by only one process at a time and not
depleted by that use
Processes obtain resources that they later
release for reuse by other processes
Examples: Processors, I/O channels, main and
secondary memory, devices, and data structures
such as files, databases, and semaphores
Deadlock occurs if each process holds one
resource and requests the other
27 November 2021 11
Consumable Resources
Created (produced) and destroyed (consumed)
27 November 2021 12
Example of Deadlock
Deadlock occurs if receive is blocking
P1 P2
... ...
Receive(P2); Receive(P1);
... ...
27 November 2021 13
Deadlock Characterization
Mutual exclusion:
At least one resource must be in a non sharable
mode; that is only one process at a time can use a
resource.
If any other process request the resource, the
requesting process must be delayed until the
resource has been released.
Hold and wait:
a process holding at least one resource is waiting to
acquire additional resources held by other processes
27 November 2021 14
Deadlock Characterization
Deadlock can arise if all four conditions hold simultaneously.
No preemption:
a resource can be released only voluntarily by the
process holding it, after that process has completed its
task.(The resource cannot be preempted).
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.
27 November 2021 15
Traffic Deadlock
27 November 2021 16
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
27 November 2021 17
Resource-Allocation Graph (Cont.)
Process
Pi
Pi requests instance of Rj
Rj
Pi is holding an instance of Rj
Pi
Rj
27 November 2021 18
Example of a Resource Allocation Graph
27 November 2021 19
Resource Allocation Graph With A Deadlock
27 November 2021 20
Graph With A Cycle But No Deadlock
27 November 2021 21
Basic Facts
If graph contains no cycles ⇒ no deadlock
27 November 2021 23
Deadlock Prevention Schemes
27 November 2021 24
Deadlock Prevention
Ensure that at least one of the 4 necessary
condition be prevented.
Mutual Exclusion
not required for sharable resources; must hold for
non-sharable resources
Only one process may use a resource at a time
27 November 2021 25
Deadlock Prevention (Cont.)
Hold and Wait
must guarantee that whenever a process requests a
resource, it does not hold any other resources.
Require process to request and be allocated all its
resources before it begins execution.
27 November 2021 26
Deadlock Prevention (Cont.)
Alternative method:
Allow a process to request resources only when
the process has no resources.
Release all resources before requesting for a new
one
27 November 2021 27
Deadlock Prevention (Cont.)
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.
Alternate Approach:
If a process request some resources,
Allot to the process if the resource is free
27 November 2021 28
Deadlock Prevention (Cont.)
Circular Wait
Impose a total ordering of all resource types, and
require that each process requests resources in
an increasing order of enumeration
27 November 2021 29
Example
Assign a unique integer number to individual resource which allows
F(tape drive)=1
F(disk drive)=5
F(printer) =12
Process can request resources only in an increasing order of
enumeration.
So R(j) is acceptable to a process if and only if F(R(i))< F(R(j)). If
27 November 2021 30
Disadvantage of Deadlock Prevention
27 November 2021 31
Deadlock Avoidance
27 November 2021 32
Two Approaches to Deadlock Avoidance
27 November 2021 33
Avoidance algorithms
Single instance of a resource type
Use a resource-allocation graph
27 November 2021 34
Resource-Allocation Graph Algorithm
3 types of edges
Request Edge, Assignment Edge, Claim Edge
27 November 2021 35
Resource-Allocation Graph
27 November 2021 36
Unsafe State In Resource-Allocation Graph
27 November 2021 37
Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj
27 November 2021 38
Banker’s Algorithm
Applicable to multiple instance of each resource
type.
This algorithm is less efficient.
Each process must a priori claim maximum use.
When a process requests a resource the system
must check whether allocation of these resources
will leave the system in safe state.
When a process gets all its resources it must
return them in a finite amount of time.
27 November 2021 39
Data Structures for the Banker’s 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
Need [i,j] = Max[i,j] – Allocation [i,j]
27 November 2021 40
Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types: A (10 instances),
27 November 2021 41
Example (Cont.)
The content of the matrix Need is defined to be
Need = Max – Allocation
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
27 November 2021 42
Example of Banker’s Algorithm
Allocation Max Need Available
ABC ABC ABC ABC
P0 0 1 0 753 743 332
P1 2 0 0 322 122
P2 3 0 2 902 600
P3 2 1 1 222 011
P4 0 0 2 433 431
27 November 2021 43
Safety Algorithm
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
27 November 2021 44
Example: P1 Request (1,0,2)
Check that Request ≤ Need (that is, (1,0,2) ≤ (1,2,2) ⇒ true
27 November 2021 45
Example: P1 Request (1,0,2)
After allocation
27 November 2021 46
Example: P1 Request (1,0,2)
After allocation
27 November 2021 47
Example: P1 Request (1,0,2)
After allocation
27 November 2021 48
Example: P1 Request (1,0,2)
After allocation
27 November 2021 49
Example: P1 Request (1,0,2)
After allocation
27 November 2021 50
Example: P1 Request (1,0,2)
After allocation
27 November 2021 52
Execute resource request algorithm for
following
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
27 November 2021 53
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
27 November 2021 54
Single Instance of Each Resource Type
Maintain wait-for graph
Nodes are processes
Pi → Pj if Pi is waiting for Pj
27 November 2021 55
Resource-Allocation Graph and Wait-for Graph
27 November 2021 56
Several Instances of a Resource Type
Available: A vector of length m indicates the
number of available resources of each type
27 November 2021 57
Example of Detection Algorithm
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 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
27 November 2021 58
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 0
P3 1 0 0
P4 0 0 2
27 November 2021 59
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
27 November 2021 60
Example
of Detection Algorithm
Five processes P through P ; three resource types
0 4
A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0:
and P4
27 November 2021 61
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
Algorithm requires an order of O(m x n2) operations to
detect whether the system is in deadlocked state
27 November 2021 62
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
Practical approach about deadlock detection algo.
Invoke the algorithm at less frequent intervals
Invoke whenever CPU utilization drops below 40%.
27 November 2021 63
Recovery from Deadlock
Automatic recovery
Process Termination
Resource Preemption
27 November 2021 64
Recovery from Deadlock: Process Termination
Abort all deadlocked processes (Very expensive)
Abort one process at a time until the deadlock cycle is
eliminated
Considerable overhead.
In which order should we choose to abort?
Priority of the process
How long process has computed, and how much longer to
completion
Types of Resources the process has used
No. of Resources process needs to complete
How many processes will need to be terminated
Is process interactive or batch?
27 November 2021 65
Recovery from Deadlock: Resource Preemption
Selecting a victim –
Which resources and from which processes are to be
preempted?
Determine the order of preemption to minimize cost.
Cost factors may include parameters as the number of
27 November 2021 66
Recovery from Deadlock: Resource
Preemption contd…
Starvation – same process may always be
picked as victim
ensure that a process can be picked as a victim
(preemption) only a (small) finite number of times.
The most common solution is to include the number of
rollbacks in the cost factor.
27 November 2021 67
Combined Approach to Deadlock Handling
Combine the three basic approaches
Prevention
Avoidance
Detection
allowing the use of the optimal approach for
each of resources in the system.
Partition resources into hierarchically ordered
classes.
Use most appropriate technique for handling
deadlocks within each class
27 November 2021 68
Strengths and Weaknesses of the Strategies
27 November 2021 69