0% found this document useful (0 votes)
15 views52 pages

ch9 Deadlocks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views52 pages

ch9 Deadlocks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Lecture 9: Deadlocks

Hossein Asadi ([email protected])


Rasool Jalili ([email protected])

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

 Each Resource Type Ri has Wi instances


 Each Process Utilizes a Resource as follows
 Request, Use, Release
4Accomplished using system calls such as request()
and release() device or open() and close() file, OR
allocate() and free() memory
th
Operating System Concepts – 9 Edition 7.4 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
System Model (cont.)
 Physical Resources
 Printers, tape drives, memory space, or CPU cores/cycles
 Logical Resources
 Files, semaphores, and monitors
 Example: Consider a system with 3 CD RW drives
and 3 running processes
 Each process holds one CD RW drive and now requests
another drive è Deadlock
 Multithreaded Programs:
 Good candidate for deadlock
 Multiple threads compete for shared resources
Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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 at least
one resource is waiting to acquire additional
resources held by other processes
 No Preemption: a resource can be released
only voluntarily by process holding it, after
that process has completed its task
Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Characterization (cont.)
 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.

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

 Request Edge – directed edge Pi ® Rj


 Assignment Edge – directed edge Rj ® Pi

Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph (cont.)
 Process

 Resource Type with 4 instances

 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

 P = {P1, P2, P3}


 R = {R1, R2, R3, R4}
 R1: one instance, R2 two instances, R3 one instance,
and R4 three instances
 E = {P1èR1, P2èR3, …., R3èP3}
Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource Allocation Graph With a Deadlock

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

 Allow System to enter a Deadlock State and


then recover

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

 Lock Ordering does not Guarantee Deadlock Prevention


if Locks can be acquired Dynamically
 Ordering is broken with unordered arguments
void transaction(Account from, Account to, double amount)
{ mutex lock1, lock2;
lock1 = get_lock(from);
lock2 = get_lock(to);
acquire(lock1);
acquire(lock2);
withdraw(from, amount);
deposit(to, amount);
release(lock2);
release(lock1);
}
• Transactions 1 and 2 execute concurrently.
• Transaction 1 transfers $25 from account A to account B, &
Transaction 2 transfers $50 7.21
th
Operating System Concepts – 9 Edition
fromSilberschatz,
account B to account A
Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Deadlock Avoidance
Requires that system has some additional a priori
information available
 Simplest and most useful model requires that each
process declare maximum number of resources
of each type that it may need
 Deadlock-avoidance algorithm dynamically
examines resource-allocation state to ensure that
there can never be a circular-wait condition
 “Resource-allocation state” is defined by number
of available and allocated resources, and
maximum demands of processes

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

 Multiple Instances of a Resource Type


 Use banker’s algorithm
4Reading assignment

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

Why not assigning R2 to P2 after making sure that P1 doesn’t


need R2 in the near future?
Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Allocation Graph Algorithm
 Suppose that Pi requests a Rj
 Request can be Granted only if Converting
Request Edge to an Assignment Edge does
not Result in Formation of a Cycle in
Resource Allocation Graph
 Algorithm Complexity O(n2)
 Low Resource Utilization
 Resources might be available and not be
allocated to processes

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

 If Detection Algorithm is Invoked Arbitrarily,


there may be many cycles in resource graph
and so we would not be able to tell which of
many deadlocked processes “caused”
deadlock
Operating System Concepts – 9th Edition 7.35 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Recovery from Deadlock: Process Termination

 Abort all Deadlocked Processes


 Significant expense
 Most processes need to be restarted for re-
computation
 Abort one process at a time until Deadlock
Cycle is Eliminated
 After each process is aborted, a deadlock-
detection algorithm must be invoked
4To see if any process is still deadlocked

Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Recovery from Deadlock: Process Termination

 Issues with Aborting a Process


 Updating a file
 Printing data
 In which Order should we Choose to Abort?
1. Priority of process
2. How long process has computed, and how
much longer to completion
3. Resources process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
Operating System Concepts – 9th Edition 7.37 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
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

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.

 Available: Vector of length m. If available [j] = k,


there are k instances of resource type Rj available
 Max: n x m matrix. If Max [i,j] = k, then process Pi
may request at most k instances of resource type Rj
 Allocation: n x m matrix. If Allocation[i,j] = k then
Pi is currently allocated k instances of Rj
 Need: n x m matrix. If Need[i,j] = k, then Pi may
need k more instances of Rj to complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]

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

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 + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a
safe state
Operating System Concepts – 9th Edition 7.42 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Resource-Request Algorithm for Process Pi

Requesti = request vector for process Pi.


If Requesti [j] = k è Pi wants k instances of Rj
1. If Requesti £ Needi go to step 2. Otherwise, raise error
condition (as 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 – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
 If safe Þ the resources are allocated to Pi
 If unsafe Þ Pi must wait, and old resource-allocation state is
restored

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

If no such i exists, go to step 4


Operating System Concepts – 9th Edition 7.48 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2024
Detection Algorithm (cont.)
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
Algorithm requires an order of O(m x n2) operations
to detect whether the system is in deadlocked state

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)

 Snapshot at time T0:


Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002

 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

You might also like