Chapter 3
Chapter 3
Chapter 3
Lecture 03:Deadlock
Objectives
– describe deadlock, and forms of prevention,
avoidance, detection, and recovery
Deadlock 1
Contents
1. What is Deadlock?
2. Dealing with Deadlock
3. Deadlock Prevention
4. Deadlock Avoidance
5. Deadlock Detection
6. Deadlock Recovery
Deadlock 2
1. What is Deadlock?
Deadlocks are a set
of blocked
processes each
holding a resource
and waiting to
acquire a resource
held by another
process.
Deadlock 3
1. What is Deadlock?
Deadlock 4
1. What is Deadlock?
Example
Program 1 requests resource A and receives it.
Program 2 requests resource B and receives it.
Program 1 requests resource B and is queued up, pending the release of B.
Program 2 requests resource A and is queued up, pending the release of A.
Now neither program can proceed until the
other program releases a resource hence
deadlock
Deadlock 5
1. What is Deadlock?
An example:
– “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.”
Deadlock 6
In Picture Form:
Deadlock 7
1.1. System Deadlock
Mutual Exclusion
– at least one resource must be held in
non-shareable mode
Deadlock
continued 9
No Preemption
– only the process can release its held resource
Circular Wait
– {P0, P1, …, Pn}
– Pi is waiting for the resource held by Pi+1;
Pn is waiting for the resource held by P0
Deadlock 10
1.3. Resource Allocation Graph
Rk
Deadlock 11
Edge Notation
Pi Rj
– process i has requested an instance of resource j
– called a request edge
Rj Pi
– an instance of resource j has been assigned to
process i
– called an assignment edge
Deadlock 12
Example Graph
R1 R3
P1 P2 P3
R4
R2
Deadlock 13
Finding a Deadlock
Deadlock 14
The end
Deadlock 15
Graph with a Deadlock
R1 R3
P1 P2 P3
R4
R2
Deadlock 16
Graph without a Deadlock
R1
P2
P1 P3
R2
P4
Deadlock 17
2. Deadling with Deadlocks
– deadlock avoidance
see a deadlock coming and alter the
process/resource allocation strategy
Deadlock
continued 18
Deadlock detection and recovery
Deadlock 19
3. Deadlock Prevention
Deadlock 20
3.1. Eliminate Mutual Exclusion
Deadlock 21
3.2. Eliminate Hold & Wait
Deadlock 22
3.3. Eliminate “No Preemption”
Deadlock 23
3.4. Eliminate Circular Wait
Deadlock 24
4. Deadlock Avoidance
Deadlock 25
4.1. Safe States
An OS is in a safe state if there is a
safe sequence of process executions
<P1, P2, …, Pn>.
Deadlock 27
Example 1
Max no. of resources: 12 tape drives
Max needs Current Allocation
P0 10 5
P1 4 2
P2 9 2
Deadlock
continued 30
When the resource is actually requested,
the claim edge is changed to a request
edge.
Deadlock
continued 31
All resources must be claimed before
system start-up.
Deadlock 32
Example
R1 R1
P1 P2 P1 P2
R2 R2
R2 allocation to P2
creates an unsafe state
Deadlock 33
4.3. Banker’s Algorithm
Assume that:
– a resource can have multiple instances
– the OS has N processes, M resource types
Max[N][M]
– max demand of each process
– e.g. max[i][j] == k means Pi wants k Rj’s
Deadlock
continued 35
Work[M]
– no. of resource instances available for work
(by all processes)
– e.g. Work[j] == k means K Rj’s are
available
Finish[N]
– record of finished processes
– e.g. Pi is finished if Finish[i] == true
Deadlock
continued 36
Allocation[N][M]
– no. of resource instances allocated to each process
– e.g. Allocation[i][j] == k
means Pi currently has k Rj’s
Need[N][M]
– no. of resource instances still needed by each process
– e.g. Need[i][j] == k
means Pi still needs k Rj’s
– Need[i][j] == Max[i][j] - Allocation[i][j]
Deadlock
continued 37
Request[N][M]
– no. of resource instances currently requested by
each process
– e.g. Request[i][j] == k
means Pi has requested k Rj’s
Deadlock 38
Vectors
Allocation[i]
– resources currently allocated to Pi
Need[i]
– resources still needed by Pi
Request[i]
– resources currently requested by Pi
Deadlock 39
The Safety Algorithm
1 Vector Copy: Work := Available; Finish := false
2 Find i such that Pi hasn’t finished but could:
Finish[i] == false
Need[i] <= Work
If no suitable i, go to step 4.
3 Assume Pi completes:
Work := Work + Allocation[i]
Finish[i] := true
go to step 2
4 If for all i Finish[i] == true then Safe-State
Deadlock 40
Safety Example
Deadlock
continued 41
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Deadlock 42
Request Resource Algorithm
Deadlock 44
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 3
Deadlock 45
Further Request Examples
Deadlock 46
5. Deadlock Detection
R1 R3 R4 P5
P1 P2 P3 P1 P2 P3
R2 P4 R5 P4
Deadlock 49
5.2. Banker’s Algorithm Variation
Deadlock 50
Detection Algorithm
1 Vector Copy: Work := Available; Finish := false
2 Find i such that Pi hasn’t finished but could:
Finish[i] == false
Request[i] <= Work
If no suitable i, go to step 4.
3 Assume Pi completes:
Work := Work + Allocation[i]
Finish[i] := true
go to step 2
4 If Finish[i] == false then Pi is deadlocked
Deadlock 51
Example 1
Deadlock
continued 52
Allocation Request Available
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 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Deadlock 53
Example 2
Change P2 to request 1 C instance
Allocation Request Available
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 3 0 0 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
The OS is deadlocked.
Deadlock 54
Deadlock Recovery
System-based recovery:
– abort one or more processes in the circular wait
Deadlock 55
1. Process Termination
Deadlock 56
2. Resource Preemption
Issues:
– how to select a resource
(e.g. by using minimum cost)
Deadlock 57