ch9 Deadlocks
ch9 Deadlocks
Fall 2024
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Lecture 9: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Lecture Objectives
To Develop a Description of Deadlocks,
which Prevent sets of Concurrent Processes
from Completing their Tasks
To Present a number of Different Methods
for Preventing or Avoiding Deadlocks in a
Computer System
Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
System Model
System Consists of Resources
To be distributed among a number of competing
processes
Resource Types R1, R2, . . ., Rm
E.g,: CPU cycles, memory space, I/O devices
Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, …, Pn}, set consisting of all
processes in the system
R = {R1, R2, …, Rm}, set consisting of all
resource types in the system
Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph (cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example of a Resource Allocation Graph
Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Graph With A Cycle But No Deadlock
Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Basic Facts
If Graph Contains no Cycles Þ No Deadlock
If Graph Contains a Cycle Þ
Only one instance per resource type è
deadlock
Cycle involves only a set of resource types, each
of which has only a single instance è deadlock
4In the above two cases, cycle is necessary & sufficient
condition for existence of deadlock
Several instances per resource type è
possibility of deadlock
4In this case, cycle is necessary but not sufficient
Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Methods for Handling Deadlocks
Ensure that System will Never Enter a
Deadlock State
Deadlock prevention: try to violate one of
necessary conditions for deadlock
Deadlock avoidance: try to regulate how/when
requests can be made to acquire resources
4More conservative approach than deadlock prevention
Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Methods for Handling Deadlocks (cont.)
Ignore Problem and Pretend that Deadlocks
Never occur in system
Used by most OSes, including UNIX
Up to application developer to detect and handle
deadlocks
What if Deadlocks are not Resolved?
Deterioration of system performance
4Eventually need a manual restart
Deadlock occur very infrequent è cheaper
approach in mainstream applications
4Instead of employing prevention, avoidance, or
detection and recovery methods
Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Prevention
Mutual Exclusion
Not required for sharable resources
A process never needs to wait for a sharable
resources
Must hold for non-sharable resources
Example
4Read-only files
Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Prevention (cont.)
Hold and Wait – must guarantee that
whenever a process requests a resource, it
does not hold any other resources
Solution 1: Require process to request and be
allocated all its resources before it begins
execution
Solution 2: Or allow process to request
resources only when process has none
allocated to it
Cons
4Low resource utilization L
4Starvation possible L
Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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 list of
resources for which the process is waiting
Process will be restarted only when it can regain
its old resources, as well as new ones
This protocol applicable only to resources whose
state can be easily saved and restored later
4 CPU registers and memory space: applicable J
4 Printers and tape drives: not (easily) applicable L
Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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
A process which holds R(i), can request
instance of R(j) if F(Rj) > F(Ri)
Ensuring order by application developer
4Can use lock-order verifier (e.g., witness in FreeBSD)
Example
F(tape)=1, F(disk drive)=5, and F(printer)=12
Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Example
/* thread one runs in this function */
void *do_work_one(void *param)
{
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
/** * Do some work */
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
pthread_exit(0);
}
/*************************************/
/* thread two runs in this function */
void *do_work_two(void *param)
{
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/** * Do some work */
pthread_mutex_unlock(&first_mutex);
pthread_mutex_unlock(&second_mutex);
pthread_exit(0);
}
Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Example with Lock Ordering
Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Safe State
When a process requests an available
resource, system must decide if immediate
allocation leaves system in a safe state
System is in safe state if there exists a
sequence <P1, P2, …, Pn> (aka, safe sequence)
of ALL processes in systems such that for
each Pi, resources that Pi can still request
can be satisfied by currently available
resources + resources held by all Pj, with j<i
Operating System Concepts – 9th Edition 7.23 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Safe State (cont.)
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
Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Basic Facts
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
Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Safe, Unsafe, Deadlock State
Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Avoidance Algorithms
Single Instance of a Resource Type
Use a resource-allocation graph
Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph Scheme
Claim edge
Pi ® Rj indicates that process Pi 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 resource is allocated to process
Resource is Released by a process è
Assignment Edge reconverts to a claim edge
Resources must be claimed a priori in
system
Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph
Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Unsafe State In Resource-Allocation Graph
Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Detection
Allow System to Enter Deadlock State
Detection Algorithm
Single instance of each resource type
Multiple instances of a resource type
4Reading assignment
Recovery Scheme
Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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 graph
If there is a cycle è there exists a deadlock
An Algorithm to Detect a Cycle in a Graph
Requires an Order of n2 operations
Where n is number of vertices in graph
Operating System Concepts – 9th Edition 7.33 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Corresponding
Graph wait-for Graph
Operating System Concepts – 9th Edition 7.34 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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?
4One for each disjoint cycle
Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Recovery from Deadlock: Process Termination
Operating System Concepts – 9th Edition 7.38 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Reading Assignment
Banker’s Algorithm
Deadlock Detection
Multiple instances of a resource type
Operating System Concepts – 9th Edition 7.39 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Banker’s Algorithm
Multiple Instances
Each Process must a Priori Claim Max Use
When a Process Requests a Resource it
may have to Wait
When a Process Gets all its Resources it
must Return them in a Finite Amount of Time
Operating System Concepts – 9th Edition 7.40 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Data Structures for Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
Operating System Concepts – 9th Edition 7.41 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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
Operating System Concepts – 9th Edition 7.43 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Operating System Concepts – 9th Edition 7.44 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example (cont.)
Content of Need is defined to be Max – Allocation
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
System is in a Safe State
Since sequence <P1,P3,P4,P2,P0> satisfies safety criteria
Operating System Concepts – 9th Edition 7.45 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example: P1 Request (1,0,2)
Check that Request £ Available (i.e., (1,0,2) £ (3,3,2) Þ true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
Executing safety algorithm shows that sequence < P1, P3,
P4, P0, P2> satisfies safety requirement
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Operating System Concepts – 9th Edition 7.46 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Several Instances of a Resource Type
Available: A vector of length m indicates
number of available resources of each type
Allocation: An n x m matrix defines
number of resources of each type currently
allocated to each process
Request: An n x m matrix indicates current
request of each process
If Request [i][j] = k, è process Pi is requesting
k more instances of resource type Rj
Operating System Concepts – 9th Edition 7.47 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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
Operating System Concepts – 9th Edition 7.49 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example of Detection Algorithm
Five Processes P0 through P4; three
Resource 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
Operating System Concepts – 9th Edition 7.50 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Example (cont.)
P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
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
Operating System Concepts – 9th Edition 7.51 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
End of Lecture 9
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024