SlideShare a Scribd company logo
CSS430
Deadlocks
Textbook Chapter 7
Instructor: Stephen G. Dame
e-‐mail: sdame@uw.edu
V0.2 CSS430 Deadlocks 1
These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne),
Professor Munehiro Fukuda and the instructor’s class materials.
WKP 17
V0.2 CSS430 Deadlocks 2
“The competent programmer is fully
aware of the strictly limited size of his
own skull; therefore he approaches the
programming task in full humility, and
among other things he avoids clever
tricks like the plague.”
-
‐
-
Edsger Dijkstra
Deadlock Examples 1
Kansas Legislature: “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.”
Two processes exchange a long message with each other, but their socket buffer
is smaller than the message.
Process
B
Socket
buffer
message message
Process
A
Socket
buffer
V0.2 CSS430 Deadlocks 3
Deadlock Examples 2
Bridge crossing example: traffic only in one direction
where two cars are driving from the opposite direction.
A deadlock is not resolved unless one gets back up.
Two processes try to go into the same nested critical section in a different order.
P0
wait (A);
wait (B);
P1
wait(B)
wait(A)
V0.2 CSS430 Deadlocks 4
System Model
 Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
 Each resource type Ri has Wi instances
 Each process utilizes a resource as follows:
• request
• use
• release
V0.2 CSS430 Deadlocks 5
Deadlock Characterization
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 resource(s) is waiting to
acquire additional resources held by other processes.
③ No preemption: a resource can be released only voluntarily
by the process holding it upon its task completion.
④ Circular wait: there exists a set {P0, P1, …, P0} 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.
V0.2 CSS430 Deadlocks 6
Resource-Allocation Graph
 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 Pi  Rj
 assignment edge – directed edge Rj  Pi
A set of vertices V and a set of edges E
 V is partitioned into two types:
Pi
Rj
V0.2 CSS430 Deadlocks 7
Resource-Allocation Graph
 Process
 Resource Type with 4 instances
 Pi requests instance of Rj
 Pi is holding an instance of Rj
 Pi releases an instance of Rj
Rj
Sequence of
process resource
utilization
Request edge
Assignment edge
Pi
Pi
Pi
Pi
V0.2 CSS430 Deadlocks 8
Resource-Allocation Graph
P1 P2 P3
R1 R3
R2
R4
Is deadlock possible?
YES!
V0.2 CSS430 Deadlocks 9
CYCLE 1
Resource-Allocation Graph
P1 P2
R1 R3
R2
R4
YES!
V0.2 CSS430 Deadlocks 1
0
P3
CYCLE 2
Resource Allocation Graph With A
Cycle But No Deadlock
P1
P2
P3
 If graph contains no
cycles  no deadlock.
V0.2 CSS430 Deadlocks 1
1
 If graph contains a cycle

• if only one instance per
resource type, then
deadlock.
• if several instances per
resource type, possibility
of deadlock.
P3
Methods for Handling Deadlocks
Ensure that the system will never enter a
deadlock state. (Prevention and Avoidance)
Allow the system to enter a deadlock state
and then recover. (Detection and Recovery)
Ignore the problem and pretend that
deadlocks never occur in the system; used by
most operating systems, including UNIX.
V0.2 CSS430 Deadlocks 1
2
Deadlock Prevention
Restrain one of the following four conditions:
it does not hold any other resources.
V0.2 CSS430 Deadlocks 1
3
execution: Low resource utilization
① Mutual Exclusion – not required for sharable resources. (but not work always.)
② Hold and Wait – must guarantee that whenever a process requests a resource,
• Require a process to request and be allocated all its resources before its
• Allow process to request resources only when the process has none:
starvation possible.
③ No Preemption –
• If a process holding some resources requests another resource that cannot
be immediately allocated to it, all resources currently being held are released.
• If a process P1 requests a resource R1 that is allocated to some other
process P2 waiting for additional resource R2, R1 is allocated to P1.
④ Circular Wait – impose a total ordering of all resource types, and require that
each process requests resources in an increasing order of enumeration.
Deadlock Prevention
Circular Wait
P1 P2 P3
P4
tape disk printer
1 2 scanner 3 4
Each process can request resources only in an
increasing order of enumeration.
V0.2 CSS430 Deadlocks 1
4
Not allowed Order(tape)=1 < Order(printer)=4
 When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
 System is in safe state if there exists a sequence
<P1, P2, …, Pn>
of ALL the processes in the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i
 That is:
 If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
 When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
 When Pi terminates, Pi +1 can obtain its needed resources, and so
on.
Safe State
V0.2 CSS430 Deadlocks 1
5
A system is in a safe state only if there
exists a only if there exists a safe sequence.
 If a system is in safe state  no deadlocks
 If a system is in unsafe state  possibility of deadlock
 Avoidance  ensure that a system will never enter an
unsafe state.
Basic Facts
V0.2 CSS430 Deadlocks 1
6
Safe, Unsafe , Deadlock State
V0.2 CSS430 Deadlocks 1
7
 Single instance of a resource type
• Use a resource-allocation graph
 Multiple instances of a resource type
• Use the banker’s algorithm
Avoidance algorithms
V0.2 CSS430 Deadlocks 1
8
Claim edge Pi  Rj indicated that process Pj may request
resource Rj; represented by a dashed line
 Claim edge converts to request edge when a process
requests a resource
 Request edge 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
 Resources must be claimed a priori in the system
Resource-Allocation Graph Scheme
V0.2 CSS430 Deadlocks 1
9
Resource-Allocation Graph
P1
R1
P2
R2
V0.2 CSS430 Deadlocks 2
0
Unsafe State In Resource-
Allocation Graph
P1
R1
P2
R2
V0.2 CSS430 Deadlocks 2
1
 Suppose that process Pi requests a resource Rj
 The request can be granted only if converting the request
edge to an assignment edge does not result in the formation
of a cycle in the resource allocation graph
Resource-Allocation Graph
Algorithm
V0.2 CSS430 Deadlocks 2
2
V0.2 CSS430 Deadlocks
CSS430
Deadlocks
23
Deadlock Avoidance
Cycle possibly formed (unsafe state),
thus P2 has to wait for a safe state
Resource-Allocation Algorithm
Processes supply OS with
future resource requests
Claim edge (future request)
Works only with single
instance resource types.
wait
 Multiple resource instances
 Each process must a priori claim maximum use
 When a process requests a resource it may have to
 When a process gets all its resources it must return
them in a finite amount of time
Deadlock Avoidance
Banker’s Algorithm - Definitions
24
V0.2 CSS430 Deadlocks
Deadlock Avoidance
25
V0.2 CSS430 Deadlocks
Banker’s Algorithm - Definitions
Let n = number of processes, and m = number of resources types.
 Available – Vector of length m indicates the number of available
resources of each type. If Available[ j] = k, then k instances of
resource type Rj are available.
 Max : n × m matrix. Defines the maximum demand of each process.
If Max[i,j] = k, then process Pi may request at most k instances of
resource type Rj.
 Allocation : n × m matrix. Defines the number of resources of each
type currently allocated to each process. If Allocation[i,j] = k, then
process Pi is currently allocated k instances of resource type Rj.
 Need : n × m matrix. Indicates the remaining resource need of each
process. If Need[i,j] = k, then process Pi may need k more instances of
resource type Rj to complete its task. Note that
Need[i,j] = Max[i,j] − Allocation[i,j]
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
2. Find an i such that both:
(a) Finish[i] = false
(b) Needi  Work
If no such i exists, go to step 4
3. Work = Work + Allocation
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
26
V0.2 CSS430 Deadlocks
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
• If unsafe  Pi must wait, and the old resource-‐allocation state is
restored
Resource-Request Algorithm for Process Pi
27
V0.2 CSS430 Deadlocks
Deadlock Avoidance
Banker’s Algorithm
 5 processes P0 through P4
 Each process must claim Max use in advance.
 Resource Types: A (10 instances), B (5instances), and C (7 instances)
28
V0.2 CSS430 Deadlocks
Process
Allocation Max Need Initial Avail
A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 7 3 3 2
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
Snapshot at time T0:
Deadlock Avoidance
Banker’s Algorithm – P1 Request (1 0 2)
 Check that Request  Available (that is, (1 0 2)  (3 3 2)  true
 Execute safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
29
V0.2 CSS430 Deadlocks
Process
Allocation Max Need Initial Avail
A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 7 2 3 0
P1 3 0 2 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
Snapshot at time T1:
Deadlock Avoidance
Banker’s Algorithm – P4 Request (3 3 0)
 Check that Request  Available (that is, (3 3 0)  (3 3 2)  true
30
V0.2 CSS430 Deadlocks
Snapshot at time T1:
Process
Allocation Max Need Initial Avail
A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 7 0 0 2
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 3 3 2 4 3 3 4 3 1
Deadlock Avoidance
Banker’s Algorithm – P0 Request (4 3 0)
 Check that Request  Available (that is, (4 3 0)  (3 3 2)  false
31
V0.2 CSS430 Deadlocks
Snapshot at time T1:
Process
Allocation Max Need Initial Avail
A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 10 5 7 3 3 2
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
 Allow system to enter deadlock state
 Detection algorithm
 Recovery scheme
Deadlock Detection
32
V0.2 CSS430 Deadlocks
Single Instance of Each Resource Type
 Maintain wait-for graph
• Nodes are processes
• Pi  Pj if Pi is waiting for Pj
 Periodically invoke an algorithm that searches for
a cycle in the graph. If there is a cycle, there
exists a deadlock
 An algorithm to detect a cycle in a graph requires
33
V0.2 CSS430 Deadlocks
an order of n2 operations, where n is the number
of vertices in the graph
Resource-Allocation Graph and
Wait-for Graph
Resource-Allocation Graph
34
V0.2 CSS430 Deadlocks
Corresponding wait-for graph
Several Instances of a
Resource Type
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.
35
V0.2 CSS430 Deadlocks
V0.2 CSS430 Deadlocks
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
This algorithm requires an order of O(m x n2) operations to detect
whether the system is in deadlocked state.
Detection Algorithm Example
 Five processes P0 through P4;
 Three resources types A (7 instances), B (2 instances), and C (6
instances)
 Sequence <P0, P2, P3, P1, P4> will result in Finish[ i ] = true for all i
37
V0.2 CSS430 Deadlocks
Process
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
Snapshot at time T0
Example (Cont.)
 P2 requests an additional instance of type C
38
V0.2 CSS430 Deadlocks
 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
Process Request
A B C
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
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.
V0.2 CSS430 Deadlocks
Recovery from Deadlock:
Process Termination
 Abort all deadlocked processes
 Abort one process at a time until the deadlock cycle
is eliminated
 In which order should we choose to abort?
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?
V0.2 CSS430 Deadlocks
Recovery from Deadlock:
Resource Preemption
 Selecting a victim – minimize cost
 Rollback – return to some safe state, restart process for
that state
 Starvation – same process may always be picked as
victim, include number of rollback in cost factor
V0.2 CSS430 Deadlocks
Exercises (No turn-in)
1. Why aren’t deadlock detection and recovery so
attractive?
2. Solve Exercise 7.3, 7.6, 7.9, 7.10, 7.14, and 7.19
3. Can the Java code in the next slide cause a
deadlock? If so, write a resource allocation graph
with a deadlock.
V0.2 CSS430 Deadlocks
42
CSS430 Deadlocks
public class Deadlock {
public Deadlock( ) {
Mutex mutex[] = new Mutex[4];
for ( int i = 0; i < 4; i++ )
mutex[i] = new Mutex( );
A threadA = new A( mutex );
B threadB = new B( mutex );
C threadC = new C( mutex );
threadA.start( );
threadB.start( );
threadC.start( );
}
public static void main( String arg[] ) {
Deadlock d = new Deadlock( );
}
class Mutex{ }
private class A extends Thread
{
private Mutex[] resource;
public A( Mutex[] m ) {
resource = m;
}
public void run( ) {
System.out.println( "A started" );
synchronized ( resource[1] ) {
System.out.println( "A got rsc 1" );
synchronized ( resource[0] ) {
System.out.println( "A got rsc 0" );
}
}
System.out.println( "A finished" );
}
}
private class B extends Thread
{
private Mutex[] resource;
public B( Mutex[] m ) {
resource = m;
}
public void run( ) {
System.out.println( "B started" );
synchronized ( resource[3] ) {
System.out.println( "B got rsc 3" );
synchronized ( resource[0] ) {
System.out.println( "B got rsc 0" );
synchronized ( resource[2] ) {
System.out.println( "B got rsc 2" );
}
}
}
System.out.println( "B finished" );
}
}
V0.2 CSS430 Deadlocks
43
private class C extends Thread
{
private Mutex[] resource;
public C( Mutex[] m ) {
resource = m;
}
public void run( ) {
System.out.println( "C started" );
synchronized ( resource[2] ) {
System.out.println( "C got rsc 2" );
synchronized ( resource[1] ) {
System.out.println( "C got rsc 1" );
}
}
System.out.println( "C finished" );
}
}
}

More Related Content

PPT
Mch7 deadlock
PPTX
Deadlock.pptx
PPTX
Gp1242 007 oer ppt
PDF
deadlocks for Engenerring for he purpose
PPTX
Module 3 Deadlocks.pptx
PPT
A ppt on deadlock in operating systems for the better explanation
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PDF
7 Deadlocks
Mch7 deadlock
Deadlock.pptx
Gp1242 007 oer ppt
deadlocks for Engenerring for he purpose
Module 3 Deadlocks.pptx
A ppt on deadlock in operating systems for the better explanation
Algorithm 4Chapter Four- Deadlock (5).pptx
7 Deadlocks

Similar to CSS430_Deadlock.pptx (20)

PPTX
6. Deadlock_1640227623705.pptx
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPTX
Deadlock - An Operating System Concept.pptx
PPT
Ch07 deadlocks
PPT
Deadlock
PPTX
Deadlock
PPT
6. Deadlock.ppt
DOCX
deadlock-operating-systems (1jjhkh).docx
PPTX
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
PPT
Deadlock
PPTX
Deadlock
PPT
Deadlock.ppt
PPT
Deadlock
PPT
Deadlock Detection in Distributed Systems
PDF
CH07.pdf
PPT
Operating System
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PPT
14th November - Deadlock Prevention, Avoidance.ppt
PPT
deadlock part5 unit 2.ppt
PPT
Os module 2 d
6. Deadlock_1640227623705.pptx
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
Deadlock - An Operating System Concept.pptx
Ch07 deadlocks
Deadlock
Deadlock
6. Deadlock.ppt
deadlock-operating-systems (1jjhkh).docx
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
Deadlock
Deadlock
Deadlock.ppt
Deadlock
Deadlock Detection in Distributed Systems
CH07.pdf
Operating System
OSLec14&15(Deadlocksinopratingsystem).pptx
14th November - Deadlock Prevention, Avoidance.ppt
deadlock part5 unit 2.ppt
Os module 2 d
Ad

More from amadayshwan (10)

PPT
Page.Replacement.OS.ppt
PPT
chap.4.memory.manag.ppt
PPT
chapter10.masss storge.memory management.ppt
PPT
CPU Scheduling
PPT
ch8 (2).ppt
PPT
3 (1) [Autosaved].ppt
PDF
osvishal-160830131208 (1).pdf
PPT
3 (2).ppt
PPT
Section07-Deadlocks (1).ppt
PPT
Threads.ppt
Page.Replacement.OS.ppt
chap.4.memory.manag.ppt
chapter10.masss storge.memory management.ppt
CPU Scheduling
ch8 (2).ppt
3 (1) [Autosaved].ppt
osvishal-160830131208 (1).pdf
3 (2).ppt
Section07-Deadlocks (1).ppt
Threads.ppt
Ad

Recently uploaded (20)

PDF
bbec55_b34400a7914c42429908233dbd381773.pdf
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
DOCX
Viruses (History, structure and composition, classification, Bacteriophage Re...
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PDF
The scientific heritage No 166 (166) (2025)
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
PPTX
INTRODUCTION TO EVS | Concept of sustainability
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PDF
An interstellar mission to test astrophysical black holes
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
PPT
protein biochemistry.ppt for university classes
bbec55_b34400a7914c42429908233dbd381773.pdf
microscope-Lecturecjchchchchcuvuvhc.pptx
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
Viruses (History, structure and composition, classification, Bacteriophage Re...
Classification Systems_TAXONOMY_SCIENCE8.pptx
Comparative Structure of Integument in Vertebrates.pptx
The scientific heritage No 166 (166) (2025)
TOTAL hIP ARTHROPLASTY Presentation.pptx
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
POSITIONING IN OPERATION THEATRE ROOM.ppt
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
INTRODUCTION TO EVS | Concept of sustainability
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
AlphaEarth Foundations and the Satellite Embedding dataset
An interstellar mission to test astrophysical black holes
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
protein biochemistry.ppt for university classes

CSS430_Deadlock.pptx

  • 1. CSS430 Deadlocks Textbook Chapter 7 Instructor: Stephen G. Dame e-‐mail: sdame@uw.edu V0.2 CSS430 Deadlocks 1 These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials.
  • 2. WKP 17 V0.2 CSS430 Deadlocks 2 “The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague.” - ‐ - Edsger Dijkstra
  • 3. Deadlock Examples 1 Kansas Legislature: “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.” Two processes exchange a long message with each other, but their socket buffer is smaller than the message. Process B Socket buffer message message Process A Socket buffer V0.2 CSS430 Deadlocks 3
  • 4. Deadlock Examples 2 Bridge crossing example: traffic only in one direction where two cars are driving from the opposite direction. A deadlock is not resolved unless one gets back up. Two processes try to go into the same nested critical section in a different order. P0 wait (A); wait (B); P1 wait(B) wait(A) V0.2 CSS430 Deadlocks 4
  • 5. System Model  Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices  Each resource type Ri has Wi instances  Each process utilizes a resource as follows: • request • use • release V0.2 CSS430 Deadlocks 5
  • 6. Deadlock Characterization 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 resource(s) is waiting to acquire additional resources held by other processes. ③ No preemption: a resource can be released only voluntarily by the process holding it upon its task completion. ④ Circular wait: there exists a set {P0, P1, …, P0} 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. V0.2 CSS430 Deadlocks 6
  • 7. Resource-Allocation Graph  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 Pi  Rj  assignment edge – directed edge Rj  Pi A set of vertices V and a set of edges E  V is partitioned into two types: Pi Rj V0.2 CSS430 Deadlocks 7
  • 8. Resource-Allocation Graph  Process  Resource Type with 4 instances  Pi requests instance of Rj  Pi is holding an instance of Rj  Pi releases an instance of Rj Rj Sequence of process resource utilization Request edge Assignment edge Pi Pi Pi Pi V0.2 CSS430 Deadlocks 8
  • 9. Resource-Allocation Graph P1 P2 P3 R1 R3 R2 R4 Is deadlock possible? YES! V0.2 CSS430 Deadlocks 9 CYCLE 1
  • 10. Resource-Allocation Graph P1 P2 R1 R3 R2 R4 YES! V0.2 CSS430 Deadlocks 1 0 P3 CYCLE 2
  • 11. Resource Allocation Graph With A Cycle But No Deadlock P1 P2 P3  If graph contains no cycles  no deadlock. V0.2 CSS430 Deadlocks 1 1  If graph contains a cycle  • if only one instance per resource type, then deadlock. • if several instances per resource type, possibility of deadlock. P3
  • 12. Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state. (Prevention and Avoidance) Allow the system to enter a deadlock state and then recover. (Detection and Recovery) Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. V0.2 CSS430 Deadlocks 1 2
  • 13. Deadlock Prevention Restrain one of the following four conditions: it does not hold any other resources. V0.2 CSS430 Deadlocks 1 3 execution: Low resource utilization ① Mutual Exclusion – not required for sharable resources. (but not work always.) ② Hold and Wait – must guarantee that whenever a process requests a resource, • Require a process to request and be allocated all its resources before its • Allow process to request resources only when the process has none: starvation possible. ③ No Preemption – • If a process holding some resources requests another resource that cannot be immediately allocated to it, all resources currently being held are released. • If a process P1 requests a resource R1 that is allocated to some other process P2 waiting for additional resource R2, R1 is allocated to P1. ④ Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
  • 14. Deadlock Prevention Circular Wait P1 P2 P3 P4 tape disk printer 1 2 scanner 3 4 Each process can request resources only in an increasing order of enumeration. V0.2 CSS430 Deadlocks 1 4 Not allowed Order(tape)=1 < Order(printer)=4
  • 15.  When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.  System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes in the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i  That is:  If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished.  When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.  When Pi terminates, Pi +1 can obtain its needed resources, and so on. Safe State V0.2 CSS430 Deadlocks 1 5 A system is in a safe state only if there exists a only if there exists a safe sequence.
  • 16.  If a system is in safe state  no deadlocks  If a system is in unsafe state  possibility of deadlock  Avoidance  ensure that a system will never enter an unsafe state. Basic Facts V0.2 CSS430 Deadlocks 1 6
  • 17. Safe, Unsafe , Deadlock State V0.2 CSS430 Deadlocks 1 7
  • 18.  Single instance of a resource type • Use a resource-allocation graph  Multiple instances of a resource type • Use the banker’s algorithm Avoidance algorithms V0.2 CSS430 Deadlocks 1 8
  • 19. Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by a dashed line  Claim edge converts to request edge when a process requests a resource  Request edge 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  Resources must be claimed a priori in the system Resource-Allocation Graph Scheme V0.2 CSS430 Deadlocks 1 9
  • 21. Unsafe State In Resource- Allocation Graph P1 R1 P2 R2 V0.2 CSS430 Deadlocks 2 1
  • 22.  Suppose that process Pi requests a resource Rj  The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph Resource-Allocation Graph Algorithm V0.2 CSS430 Deadlocks 2 2
  • 23. V0.2 CSS430 Deadlocks CSS430 Deadlocks 23 Deadlock Avoidance Cycle possibly formed (unsafe state), thus P2 has to wait for a safe state Resource-Allocation Algorithm Processes supply OS with future resource requests Claim edge (future request) Works only with single instance resource types.
  • 24. wait  Multiple resource instances  Each process must a priori claim maximum use  When a process requests a resource it may have to  When a process gets all its resources it must return them in a finite amount of time Deadlock Avoidance Banker’s Algorithm - Definitions 24 V0.2 CSS430 Deadlocks
  • 25. Deadlock Avoidance 25 V0.2 CSS430 Deadlocks Banker’s Algorithm - Definitions Let n = number of processes, and m = number of resources types.  Available – Vector of length m indicates the number of available resources of each type. If Available[ j] = k, then k instances of resource type Rj are available.  Max : n × m matrix. Defines the maximum demand of each process. If Max[i,j] = k, then process Pi may request at most k instances of resource type Rj.  Allocation : n × m matrix. Defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instances of resource type Rj.  Need : n × m matrix. Indicates the remaining resource need of each process. If Need[i,j] = k, then process Pi may need k more instances of resource type Rj to complete its task. Note that Need[i,j] = Max[i,j] − Allocation[i,j]
  • 26. 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 2. Find an i such that both: (a) Finish[i] = false (b) Needi  Work If no such i exists, go to step 4 3. Work = Work + Allocation Finish[i] = true go to step 2 4. If Finish [i] == true for all i, then the system is in a safe state 26 V0.2 CSS430 Deadlocks
  • 27. 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 • If unsafe  Pi must wait, and the old resource-‐allocation state is restored Resource-Request Algorithm for Process Pi 27 V0.2 CSS430 Deadlocks
  • 28. Deadlock Avoidance Banker’s Algorithm  5 processes P0 through P4  Each process must claim Max use in advance.  Resource Types: A (10 instances), B (5instances), and C (7 instances) 28 V0.2 CSS430 Deadlocks Process Allocation Max Need Initial Avail A B C A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 10 5 7 3 3 2 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 Snapshot at time T0:
  • 29. Deadlock Avoidance Banker’s Algorithm – P1 Request (1 0 2)  Check that Request  Available (that is, (1 0 2)  (3 3 2)  true  Execute safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement 29 V0.2 CSS430 Deadlocks Process Allocation Max Need Initial Avail A B C A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 10 5 7 2 3 0 P1 3 0 2 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 Snapshot at time T1:
  • 30. Deadlock Avoidance Banker’s Algorithm – P4 Request (3 3 0)  Check that Request  Available (that is, (3 3 0)  (3 3 2)  true 30 V0.2 CSS430 Deadlocks Snapshot at time T1: Process Allocation Max Need Initial Avail A B C A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 10 5 7 0 0 2 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 3 3 2 4 3 3 4 3 1
  • 31. Deadlock Avoidance Banker’s Algorithm – P0 Request (4 3 0)  Check that Request  Available (that is, (4 3 0)  (3 3 2)  false 31 V0.2 CSS430 Deadlocks Snapshot at time T1: Process Allocation Max Need Initial Avail A B C A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 10 5 7 3 3 2 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
  • 32.  Allow system to enter deadlock state  Detection algorithm  Recovery scheme Deadlock Detection 32 V0.2 CSS430 Deadlocks
  • 33. Single Instance of Each Resource Type  Maintain wait-for graph • Nodes are processes • Pi  Pj if Pi is waiting for Pj  Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock  An algorithm to detect a cycle in a graph requires 33 V0.2 CSS430 Deadlocks an order of n2 operations, where n is the number of vertices in the graph
  • 34. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph 34 V0.2 CSS430 Deadlocks Corresponding wait-for graph
  • 35. Several Instances of a Resource Type 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. 35 V0.2 CSS430 Deadlocks
  • 36. V0.2 CSS430 Deadlocks 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 This algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state.
  • 37. Detection Algorithm Example  Five processes P0 through P4;  Three resources types A (7 instances), B (2 instances), and C (6 instances)  Sequence <P0, P2, P3, P1, P4> will result in Finish[ i ] = true for all i 37 V0.2 CSS430 Deadlocks Process 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 Snapshot at time T0
  • 38. Example (Cont.)  P2 requests an additional instance of type C 38 V0.2 CSS430 Deadlocks  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 Process Request A B C P0 0 0 0 P1 2 0 1 P2 0 0 1 P3 1 0 0 P4 0 0 2
  • 39. 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. V0.2 CSS430 Deadlocks
  • 40. Recovery from Deadlock: Process Termination  Abort all deadlocked processes  Abort one process at a time until the deadlock cycle is eliminated  In which order should we choose to abort?  Priority of the process  How long process has computed, and how much longer to completion  Resources the process has used  Resources process needs to complete  How many processes will need to be terminated  Is process interactive or batch? V0.2 CSS430 Deadlocks
  • 41. Recovery from Deadlock: Resource Preemption  Selecting a victim – minimize cost  Rollback – return to some safe state, restart process for that state  Starvation – same process may always be picked as victim, include number of rollback in cost factor V0.2 CSS430 Deadlocks
  • 42. Exercises (No turn-in) 1. Why aren’t deadlock detection and recovery so attractive? 2. Solve Exercise 7.3, 7.6, 7.9, 7.10, 7.14, and 7.19 3. Can the Java code in the next slide cause a deadlock? If so, write a resource allocation graph with a deadlock. V0.2 CSS430 Deadlocks 42
  • 43. CSS430 Deadlocks public class Deadlock { public Deadlock( ) { Mutex mutex[] = new Mutex[4]; for ( int i = 0; i < 4; i++ ) mutex[i] = new Mutex( ); A threadA = new A( mutex ); B threadB = new B( mutex ); C threadC = new C( mutex ); threadA.start( ); threadB.start( ); threadC.start( ); } public static void main( String arg[] ) { Deadlock d = new Deadlock( ); } class Mutex{ } private class A extends Thread { private Mutex[] resource; public A( Mutex[] m ) { resource = m; } public void run( ) { System.out.println( "A started" ); synchronized ( resource[1] ) { System.out.println( "A got rsc 1" ); synchronized ( resource[0] ) { System.out.println( "A got rsc 0" ); } } System.out.println( "A finished" ); } } private class B extends Thread { private Mutex[] resource; public B( Mutex[] m ) { resource = m; } public void run( ) { System.out.println( "B started" ); synchronized ( resource[3] ) { System.out.println( "B got rsc 3" ); synchronized ( resource[0] ) { System.out.println( "B got rsc 0" ); synchronized ( resource[2] ) { System.out.println( "B got rsc 2" ); } } } System.out.println( "B finished" ); } } V0.2 CSS430 Deadlocks 43 private class C extends Thread { private Mutex[] resource; public C( Mutex[] m ) { resource = m; } public void run( ) { System.out.println( "C started" ); synchronized ( resource[2] ) { System.out.println( "C got rsc 2" ); synchronized ( resource[1] ) { System.out.println( "C got rsc 1" ); } } System.out.println( "C finished" ); } } }