Deadlocks - CH 8
Deadlocks - CH 8
Deadlocks - CH 8
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ System Model
▪ Deadlock Characterization
▪ Methods for Handling Deadlocks
▪ Deadlock Prevention
▪ Deadlock Avoidance
▪ Deadlock Detection
▪ Recovery from Deadlock
Operating System Concepts – 10th Edition 8.2 Silberschatz, Galvin and Gagne ©2018
Chapter Objectives
▪ Illustrate how deadlock can occur when mutex locks are used
▪ Define the four necessary conditions that characterize deadlock
▪ Identify a deadlock situation in a resource allocation graph
▪ Evaluate the four different approaches for preventing deadlocks
▪ Apply the banker’s algorithm for deadlock avoidance
▪ Apply the deadlock detection algorithm
▪ Evaluate approaches for recovering from deadlock
Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
Deadlock
□ Permanent blocking of a set of processes that either compete for
system resources or communicate with each other
□ A set of blocked processes each holding a resource and waiting
to acquire a resource held by another process in the set
□ No efficient solution
□ Most OSes do not prevent or deal with deadlocks
□ But such features will probably be added soon
Deadlock
Deadlock
Example:
P0 P1
… … P0 P1
Request(Disk) Request(Printer)
… …
Request(Printer) Request(Disk)
… …
Release(Disk) Release(Printer) R0
… …
Release(Printer) Release(Disk) P0 P1
… …
R1
System Model
Operating System Concepts – 10th Edition 8.7 Silberschatz, Galvin and Gagne ©2018
Deadlock with Semaphores
▪ Data:
• A semaphore S1 initialized to 1
• A semaphore S2 initialized to 1
▪ Two processes P1 and P2
▪ P1:
wait(s1)
wait(s2)
▪ P2:
wait(s2)
wait(s1)
Operating System Concepts – 10th Edition 8.8 Silberschatz, Galvin and Gagne ©2018
Necessary Conditions for Deadlock
Deadlock can arise if four conditions hold simultaneously (Coffman conditions):
▪ The first three conditions are necessary but not sufficient for a deadlock
to exist
Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
Necessary Conditions for Deadlock
Deadlock can arise if four conditions hold simultaneously (Coffman conditions ):
4. Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such
that
Operating System Concepts – 10th Edition 8.10 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
A directed graph consists of: A set of vertices V and a set of edges E.
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
Operating System Concepts – 10th Edition 8.11 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph Example
▪ One instance of R1
▪ Two instances of R2
▪ One instance of R3
▪ Three instance of R4
▪ T1 holds one instance of R2 and is waiting for an
instance of R1
▪ T2 holds one instance of R1, one instance of R2,
and is waiting for an instance of R3
▪ T3 is holds one instance of R3
Deadlock?
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph
Deadlock?
Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph
R0
P0 P1
R1
Deadlock?
Operating System Concepts – 10th Edition 8.14 Silberschatz, Galvin and Gagne ©2018
Basic Facts
Operating System Concepts – 10th Edition 8.15 Silberschatz, Galvin and Gagne ©2018
Methods for Handling Deadlocks
▪ Ostrich
• Ignore the problem and pretend that deadlocks never
occur in the system
• Used by most operating systems, including UNIX, Windows
▪ Allow the system to enter a deadlock state and then detect and recover
Attempt to detect the presence of deadlock and take action to recover.
Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:
Operating System Concepts – 10th Edition 8.17 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
□ 2. Allow process to request resources only when the process has none
■ i.e., before a process can request any additional resources, it must release
all the resources that it is currently allocated
□ Disadvantages
■ Low resource utilization
□ 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 implicitly 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
Deadlock Prevention
□ No Preemption (Cont.)
□ Disadvantage
■ It is often applied to resources whose state can be easily saved and restored
later, such as CPU registers
■ It cannot generally be applied to such resources as printers and tape drives
□ 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
3
R0
P0 P1
R1
5
□ 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 plus resources held
by all the Pj, with j < i
Safe, Unsafe, Deadlock State
Operating System Concepts – 10th Edition 8.25 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph Scheme
▪ 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
Operating System Concepts – 10th Edition 8.26 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
Operating System Concepts – 10th Edition 8.27 Silberschatz, Galvin and Gagne ©2018
Unsafe State In Resource-Allocation Graph
Operating System Concepts – 10th Edition 8.28 Silberschatz, Galvin and Gagne ©2018
Banker’s Algorithm
R0 R1 R2 R3 R0 R1 R2 R3 R0 R1 R2 R3
P0 1 0 2 3 P0 1 0 0 2 P0 0 0 2 1
P1 2 3 1 0 P1 1 2 0 0 P1 1 1 1 0
P2 0 2 1 1 P2 0 1 1 0 P2 0 1 0 1
P3 4 3 1 1 P3 2 3 1 0 P3 2 0 0 1
P4 2 2 2 1 P4 0 0 0 0 P4 2 2 2 1
Max Allocation Need
P2 P1 P3 P0 P4
(1,1,0,1) (1,2,1,1) (2,4,1,1) (4,7,2,1) (5,7,2,3) (5,7,2,3)
Banker’s Algorithm: an Example
R0 R1 R2 R3 R0 R1 R2 R3 R0 R1 R2 R3
P0 1 0 2 3 P0 1 0 0 2 P0 0 0 2 1
P1 2 3 1 0 P1 1 2 0 0 P1 1 1 1 0
P2 0 2 1 1 P2 0 1 1 0 P2 0 1 0 1
P3 4 3 1 1 P3 2 3 1 0 P3 2 0 0 1
P4 2 2 2 1 P4 1 0 0 0 P4 1 2 2 1
Max Allocation Need
P2
(0,1,0,1) (0,2,1,1)
Banker’s Algorithm: an Example
P1 asks for an instance of R0, is it allocated?
R0 R1 R2 R3 R0 R1 R2 R3
Resource = 5 7 2 3 Avail= 0 1 0 1
R0 R1 R2 R3 R0 R1 R2 R3 R0 R1 R2 R3
P0 1 0 2 3 P0 1 0 0 2 P0 0 0 2 1
P1 2 3 1 0 P1 2 2 0 0 P1 0 1 1 0
P2 0 2 1 1 P2 0 1 1 0 P2 0 1 0 1
P3 4 3 1 1 P3 2 3 1 0 P3 2 0 0 1
P4 2 2 2 1 P4 0 0 0 0 P4 2 2 2 1
Max Allocation Need
P2 P1 P3 P0 P4
(0,1,0,1) (0,2,1,1) (2,4,1,1) (4,7,2,1) (5,7,2,3) (5,7,2,3)
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
Note: Request = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj
Banker’s Algorithm Disadvantages
□ Maximum resource requirement must be stated in advance
□ There must be a fixed number of resources to allocate
□ The algorithm is very conservative
□ It limits access to resources
Deadlock Detection and Recovery
□ If a system does not employ either a deadlock prevention or a deadlock
avoidance algorithm, then a deadlock situation may occur
Operating System Concepts – 10th Edition 8.39 Silberschatz, Galvin and Gagne ©2018
Deadlock Detection
■ If a process has a row in the Allocation matrix of all zeros, then it is marked as
finished.
R0 R1 R2 R3 R4 R0 R1 R2 R3 R4
P0 1 0 1 1 0 P0 0 1 0 0 1
P1 1 1 0 0 0 P1 0 0 1 0 1
P2 0 0 0 1 0 P2 0 0 0 0 1
P3 0 0 0 0 0 P3 1 0 1 0 1
Allocation Request
P2
(0,0,0,0,1) (0,0,0,1,1) Yes, P0 and P1 are deadlocked
Detection Algorithm
Operating System Concepts – 10th Edition 8.42 Silberschatz, Galvin and Gagne ©2018
Detection Algorithm (Cont.)
Operating System Concepts – 10th Edition 8.43 Silberschatz, Galvin and Gagne ©2018
Deadlock Recovery
□ Abort all deadlocked processes
□ One of the most common solution adopted in operating systems
□ Expensive solution
Operating System Concepts – 10th Edition 8.46 Silberschatz, Galvin and Gagne ©2018
Deadlock Recovery
□ Successively abort deadlocked processes until deadlock no
longer exists
□ Incurs considerable overhead, since after each process is aborted, a
deadlock-detection algorithm must be invoked
▪ Rollback – return to some safe state, restart process for that state
Operating System Concepts – 10th Edition 8.49 Silberschatz, Galvin and Gagne ©2018
End of Chapter 8
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018