Deadlock
Deadlock
Deadlock
Deadlock
By
Dr. Sidra Rafique
Introduction to Deadlock
A set of processes is said to be in a dead lock state if every process
is waiting for an event that can be caused only by another process
in the set.
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
A process is said to be dead locked if it is waiting for an event
which will never occur.
Example
A table with a writing pad and a pen.
Two persons sitting around the table wants to write letter.
One picks up the pad and the other grab the pen.
Dead Lock!
2
Introduction to Deadlock
Example 1
Consider a system having two tape drives.
P1 and P2 each hold one tape drive and each needs the other
one(e.g. to copy data from one tape drive to the other).
Example 2
Semaphores A and B, initialized to 1
P0 P1
signal(A);
wait (A); wait(B);
wait (B); wait(A);
--- --- P0 P1
signal(B);
3
Introduction to Deadlock
Example 3
The Dining Philosophers problem … all philosophers become
hungry at the same time, pick up the chopsticks on their right and
wait for getting the chopsticks on their left.
Example 4
• Consider a one way bridge.
• 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).
4
Introduction to Deadlock
Is deadlock the same as starvation?
A process is indefinitely postponed if it is delayed repeatedly over
a long period of time while the attention of the system is given to
other processes. i.e., logically the process may proceed but the
system never gives it the CPU.
5
Resources
Deadlocks occurs when processes have been granted exclusive access to
resources; In case of sharable access DL will not occur.
A resource can be a hardware device (e.g. tape drive, printer, memory,
CPU) or a piece of information (a variable, file, semaphore, record of a
database).
Resources comes in two flavors:
Preemptable Resources. A resource that can be taken away from the
process holding it with no ill effects,(sharable resources) e.g. CPU, memory.
Non Preemptable Resources. A resource that cannot be taken away from
the process holding it, w/o causing the computation to fail, (non sharable
resources) e.g. if a process has begun to burn a CD ROM, suddenly taking the
CD recorder away from it and giving the CD Recorder to another process will
result in a garbled CD.
In general DL involve non preemptable resources.
A process may utilize a resource in following sequence:
Request Use Release
6
Conditions for Deadlock
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 one or more allocated
resources while awaiting assignment of other.
No preemption: No resource can be forcibly removed from a
process holding it.
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.
P0 → P1 → P2 → … → Pn → P0
7
Resource Allocation Graph (RAG)
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {A set of processes} = {P1, P2, …, Pn}
R = {A set of Resources} = {R1, R2, …, Rm}
E = {Request Edges, Assignment Edges}
Request Edge: P1 Rj
Assignment Edge: Rj Pi
Process
P R
Resource Type with 4 instances
Pi requests instance of Rj Pi
8 Pi
Pi is holding an instance of Rj
RAG – Examples
9
Determining Deadlock using RAG
11
Deadlock Prevention
Restrain the ways resource allocation requests can be
made, to ensure that at least one of the four necessary
conditions is violated.
i. Mutual Exclusion.
ii. Hold and Wait.
iii. No Preemption.
iv. Circular Wait.
12
Deadlock Prevention
i. Mutual Exclusion.
Cannot be prevented for all resources. Some resources
are inherently non-sharable because their states cannot
be saved and restored without ill effects, such as a
printer.
13
Deadlock Prevention
ii. Hold and Wait
We must guarantee that whenever a process requests a
resource, it does not hold any other resources.
Option 1 - Deny wait.
Allocate the process with all the requested
resources before it starts execution. If one or more
resources are busy, nothing would be allocated and
the process would just wait. (Low resource
utilization)
Option 2 - Deny hold.
A process may request some resources and use
them. But before requesting any additional
resources, it must release all the resources that are
currently allocated. (Possibility of starvation)
14
Deadlock Prevention
iii. 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 for.
Process will be restarted only when it can regain its old
resources as well as the new ones that it has requested.
This protocol is often applied to resources whose state
can be easily saved and restored later, such as CPU
registers and memory space. It cannot generally be
applied to such resources as printers and tape drives. (Its
difficult to take a tape drive away from a process that is busy writing a tape).
15
Deadlock Prevention
iv. Circular Wait
Impose a total ordering of all resource types, and
require that each process requests resources in an
increasing order of enumeration.
We assign a unique number to each resource type by
using function
F: R → N
and make sure that processes request resources in an
increasing order of enumeration.
Example
Tape drive = 1, Disk drive = 5, and Printer = 12.
P1 holding Tape can request for both DD and Printer.
P2 holding DD can request for Printer but not for TD.
16
Deadlock Prevention (cont…)
Circular wait (Proof)
Lets assume that a cycle exist among processes:
P0 → P1 → P2 → … → Pk → P0
R0 R1 R2 Rk R0
P0 is holding R0 and waiting for P1 to release R1;
P1 is holding R1 and waiting for P2 to release R2 and so on.
As per rule of circular wait:
F(R0) < F(R1) < … F(Rk) < F(R0)
F(R0) < F(R0), which is impossible
There can be no circular wait.
17
Deadlock Avoidance
The implementation of deadlock avoidance requires that the
system has an advanced information available about the
future use of resources by processes.
Simplest way is that each process declare the maximum
number of resources of each type that it may need.
The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a
circular-wait condition.
Resource-Allocation state is defined by the total number of
resources available in the system, maximum demands of the
processes and the number of resources allocated to the
processes.
18
Deadlock Avoidance (cont…)
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
System is in a safe state if there is at least one sequence of
allocation of resources to processes that does not result in a
dead lock. (Sequence of processes does not matter).
Safe Sequence is the sequence of execution of processes in
which OS fulfills requests of all the processes and still avoids
dead lock.
Basic Facts.
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of deadlock due to
the behavior of processes.
19
Deadlock Avoidance (cont…)
DL Avoidance ensure that a system never enters in unsafe state.
A system can be in safe or unsafe state. Unsafe state does not
necessarily means that system is in DL. It may lead to a DL.
20
Example 1
System with 12 tape drives and three processes
Current system state:
Process Max Need Allocated Max. Future Need
P0 10 5 5
P1 4 2 2
P2 9 2 7
25
RAG Algorithm (cont…)
26
Banker’s Algorithm
This is a deadlock avoidance algorithm for resources with multiple
instances.
When a process initiates a request the algorithm checks whether
after the grant of this request the system will remain in safe
state. If yes the request is granted, if not the request is denied.
Algorithm is based on resource denial if there is a suspected risk of
a deadlock.
Each process must in advance inform the algo the number of
instances it may require in its life time.
When a process requests a resource it may have to wait, i.e. in
case the system is going to unsafe state by allocation of that
resource; the process must wait.
When a process gets all its resources it must return them in a finite
amount of time.
27
Data Structures for the Banker’s Algorithm
n = Number of processes.
m = Number of resource types.
28
Safety Algorithm
Used to determine whether or not a system is in safe
state.
Check the system state and sees if there is any process
whose request can be fulfilled. If yes note it down in
safe sequence and then add its allocation vector in
available vector.
So now we have got a new available vector with some
new instances of various resource types.
The above step is repeated with all the processes and
finally either we will be able to finish all the processes
with a safe sequence OR we may get stuck.
The safety algo returns safe or unsafe whatever is
29 applicable to the Bankers Algorithm.
Resource-Request Algorithm
Let Requesti be the request vector for process Pi.
If Requesti [j] = k then Pi wants k instances of resource type Rj.
1.If Requesti Needi then
go to step 2
else
report error (since process has exceeded its maximum claim).
2.If Requesti Available then
go to step 3
else
Pi must wait (since resources are not available).
3.Update system table:
Available = Available – Requestj;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
30
Resource-Request Algorithm (cont…)
Now the Banker’s Algo invokes the safety algo with this new system
state and the safety algo tells the banking algo whether the new
state will be safe or not.
If safe the resources are allocated to Pi.
restored
31
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available;
for i = 1, 2, …, n
Finish[i] = false;
2. Find an index 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.
(we are finding a process who has not finished yet and its need is
less than its allocation)
4. If Finish[i] == true, for all i, 1 i n, then the system is in a safe
state.
32
Example 1
Five processes: P0 … P4
Three resource types: A (10 instances), B (5 instances), C (7 instances)
System state is shown below:
Process
A B C A B C A B C
P0 7 5 3 0 1 0 3 3 2
P1 3 2 2 2 0 0
P2 9 0 2 3 0 2
P3 2 2 2 2 1 1
33
P4 4 3 3 0 0 2
Example 1 (cont…)
Process A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
34
Example 1 (cont…)
Need Allocation Matrix Available Vector /
Work
Process
A B C A B C A B C
P0 7 4 3 0 1 0 3 3 2
P1 1 2 2 2 0 0
P2 6 0 0 3 0 2
P3 0 1 1 2 1 1
P4 4 3 1 0 0 2
Resources cant be granted to P0 as (7,4,3)>(3,3,2)
35
However, can be granted to P1 as (1,2,2) (3,3,2)
Example 1 (cont…)
36
Safe Sequence: <P1>
Example 1 (cont…)
37
Safe Sequence: <P1, P3>
Example 1 (cont…)
38
Safe Sequence: <P1, P3, P4>
Example 1 (cont…)
39
Safe Sequence: <P1, P3, P4, P0>
Example 1 (cont…)
Process A B C A B C A B C
P0 7 4 3 0 1 0 3 3 2
P1 1 2 2 2 0 0
P2 6 0 0 3 0 2
P3 0 1 1 2 1 1
P4 4 3 1 0 0 2
1. Is Request1 Need1? (1,0,2) (1,2,2) true
41
2. Is Request1 Available? (1,0,2) (3,3,2) true
Example 2 (cont…)
42
Safe Sequence: <>
Example 2 (cont…)
43
Safe Sequence: <P1>
Example 2 (cont…)
44
Safe Sequence: <P1, P3>
Example 2 (cont…)
45
Safe Sequence: <P1, P3, P4>
Example 2 (cont…)
46
Safe Sequence: <P1, P3, P4, P0>
Example 2 (cont…)
Final safe sequence:
<P1, P3, P4, P0, P2>
Therefore, P1’s request may be granted immediately.
Example 3
Can request for (0,2,0) by P0 be granted?
Example 4
Can request for (3,3,0) by P4 be granted?
47
Sample Problem
Five processes: P0 … P3
Three resource types: A (9 instances), B (3 instances), C (6 instances)
System state is shown below:
Process
A B C A B C A B C
P0 3 2 2 1 0 0 0 1 1
P1 6 1 3 6 1 2
P2 3 1 4 2 1 1
P3 4 2 2 0 0 2
48
Deadlock Detection & Recovery
Both prevention and avoidance of deadlock lead
to conservative allocation of resources, with
corresponding inefficiencies.
Deadlock detection takes the opposite approach:
Make allocations liberally, allowing deadlock to
occur (on the assumption that it will be rare);
Apply a detection algorithm periodically to check
for deadlock;
Apply a recovery algorithm when necessary.
49
Dead Lock Detection Algorithm for Single Instance of
Each Resource Type
This algorithm use wait-for-graph that can be derived from RAG.
50
RAG and Wait-for Graph
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 2 0 0 0
P3 2 1 1 1 0 0
55
P4 0 0 2 0 0 2
Example (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 2 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
56
Finish Sequence: <P0>
Example (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 2 0 0 0 3 1 2
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
57
Finish Sequence: <P0, P2>
Example (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 2 0 0 0 3 1 2
P3 2 1 1 1 0 0 5 2 3
P4 0 0 2 0 0 2
58
Finish Sequence: <P0, P2, P3>
Example (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 2 0 0 0 3 1 2
P3 2 1 1 1 0 0 5 2 3
P4 0 0 2 0 0 2 5 2 5
59
Finish Sequence: <P0, P2, P3, P4>
Example (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 2 0 0 1
P3 2 1 1 1 0 0
61
P4 0 0 2 0 0 2
Example2 (cont…)
Process
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2 0 1 0
P2 3 0 2 0 0 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
62
Finish Sequence: <P0>
Example2 (cont…)
P0’s request can be satisfied with currently available
resources, but request for no other process can be
satisfied after that.
ÞDeadlock exists, consisting of processes
P1, P2, P3, and P4.
63
Deadlock Detection
How often should the detection algorithm be invoked?
1. Every time a request for allocation cannot be granted
immediately—expensive but process causing the deadlock
is identified, along with processes involved in deadlock.
2. Keep monitoring CPU usage, and when it goes below a
certain level, invoke the algorithm.
3. Run it periodically after a specified time interval.
4. Run the algo arbitrarily/randomly—In this case, we may
find a number of cycles in the system but may not be able
to find out which process has created these cycles.
64
Deadlock Recovery
Recovery algorithms vary a lot in their severity:
a) Process Termination:
Abort all deadlocked processes. Though drastic, this is probably
the most common approach!
Abort deadlocked processes one at a time until the deadlock no
longer exists. The aborted processes are called victims.
b) Resource Preemption :
Preempt resources until the deadlock no longer exists.
65
Deadlock Recovery
a) Process Termination:
In which order should we choose to abort processes?
1. Priority of a process.
2. How long the process has run.
3. Resources already used by a process.
4. Further resources the process needs to complete.
5. How many child processes will be needed to terminate.
6. Is the process interactive or batch?
66
Recovery from Deadlock
b) Resource Preemption :
In resource preemption, select a process and take back some
resources from that process and allocate those resources to other
requesting processes and bring the system out of deadlock.
Three important issues to be considered are:
Selecting a victim – Which resources and which processes are to be
preempted?
Rollback – If we preempt a resource from a process, what should be done
with that process? We need to return the victim to some safe state from
where it can be restarted later on.
Starvation – There is a possibility that same process may always be picked
as victim, so to avoid this include number of rollbacks in cost factor of
victim selection.
67