ch8 Deadocks-Ver1
ch8 Deadocks-Ver1
ch8 Deadocks-Ver1
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 8: Deadlocks
System Model
Deadlock in Multithreaded Applications
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
Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
Background
In a multiprogramming/multithreading environment,
Several threads may compete for a finite number of resources
A thread requests resources:
If the resources are not available at that time, the thread enters a
waiting state.
Sometimes, a waiting thread can never again change state, because the
resources it has requested are held by other waiting threads
This situation is called a deadlock.
Operating System Concepts – 10th Edition 8.4 Silberschatz, Galvin and Gagne ©2018
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Under the normal mode of operation, a process/thread utilizes a resource
as follows:
Request:
The thread requests the resource. If the request cannot be granted
immediately (i.e, if a mutex lock is currently held by another thread), then
the requesting thread must wait until it can acquire the resource.
Use:
The thread can operate on the resource ( i.e., if the resource is a mutex
lock, the thread can access its critical section)
Release:
The thread releases the resource
Operating System Concepts – 10th Edition 8.5 Silberschatz, Galvin and Gagne ©2018
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Assume Two mutex locks are created an initialized:
blocks the thread until the owner of the mutex lock invokes the
pthread_mutex_unlock()
Operating System Concepts – 10th Edition 8.7 Silberschatz, Galvin and Gagne ©2018
Circular Wait
/* 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);
}
Operating System Concepts – 10th Edition 8.8 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Deadlock is possible, if :
thread_one acquires first_mutex and
thread_two acquires second_mutex
Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Note that, even though deadlock is possible,
it will not occur if thread_one can acquire and release the mutex locks for
first mutex and second mutex before thread_two attempts to acquire the
locks.
And, of course, the order in which the threads run depends on how they are
scheduled by the CPU scheduler.
it is difficult to identify and test for deadlocks that may occur only under
certain scheduling circumstances
Operating System Concepts – 10th Edition 8.10 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph
called a system resource-allocation graph.
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
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph With A Deadlock
Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
Graph With A Cycle But No 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
Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Simply, Invalidate one of the four necessary conditions for deadlock:
1. Mutual Exclusion – We cannot prevent deadlocks by denying the mutual-
exclusion condition
not required for sharable resources (e.g., read-only files);
must hold for non-sharable resources (e.g. a mutex lock cannot be simultaneously
shared by several threads)
2. Hold and Wait – We need to: “ensure that the hold-and-wait condition never
occurs in the system”
must guarantee that whenever a process requests a resource, it does not hold any
other resources
Approach #1: require process to request and be allocated all its resources before
it begins execution,
– Cons: impractical for most applications and if applied Low resource utilization
Approach #2: allows a thread to request resources only when it has none
allocated to it.
– Cons: may lead to starvation, wait indefinitely
» If the needed R is always allocated to some other thread
Operating System Concepts – 10th Edition 8.17 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
3. No Preemption – of resources that have already been allocated, to prevent
deadlock, we need to ensure that this condition does not hold:
If a process is holding some resources and requests another that cannot be
immediately allocated to it, then all resources currently being held are released
(preempted)
Preempted resources are added to the list of resources for which the process is
waiting for
Process will be restarted only when it can regain its old resources as well as the new
ones that it is requesting
Operating System Concepts – 10th Edition 8.18 Silberschatz, Galvin and Gagne ©2018
How to Invalidate Circular Wait
Invalidating the circular wait condition is most common.
Simply, assign each resource (i.e. mutex locks) a unique number, ( this number
determines whether one precedes another)
An ordering among all synchronization objects
Then, resources must be acquired in order.
i.e. each thread can request resources only in
an increasing order of enumeration
and released in a decreasing order
Example:
first_mutex = 1
second_mutex = 5
Pros: The limits ensure that at least one of the necessary conditions for
deadlock cannot occur
Operating System Concepts – 10th Edition 8.20 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process
declare the maximum number of resources of each type
that it may need
The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition
Resource-allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes
Operating System Concepts – 10th Edition 8.21 Silberschatz, Galvin and Gagne ©2018
Safe State
Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
Basic Facts
Operating System Concepts – 10th Edition 8.23 Silberschatz, Galvin and Gagne ©2018
Safe, Unsafe, Deadlock State
Operating System Concepts – 10th Edition 8.24 Silberschatz, Galvin and Gagne ©2018
Avoidance Algorithms
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
Resource-Allocation Graph Algorithm
Operating System Concepts – 10th Edition 8.29 Silberschatz, Galvin and Gagne ©2018
Banker’s Algorithm
Multiple instances of resources
Operating System Concepts – 10th Edition 8.30 Silberschatz, Galvin and Gagne ©2018
Data Structures for the Banker’s Algorithm
Operating System Concepts – 10th Edition 8.31 Silberschatz, Galvin and Gagne ©2018
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
4. If Finish [i] == true for all i, then the system is in a safe state
Operating System Concepts – 10th Edition 8.32 Silberschatz, Galvin and Gagne ©2018
Resource-Request Algorithm for Process Pi
Operating System Concepts – 10th Edition 8.33 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Operating System Concepts – 10th Edition 8.34 Silberschatz, Galvin and Gagne ©2018
Example (Cont.)
The content of the matrix Need is defined to be Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Operating System Concepts – 10th Edition 8.35 Silberschatz, Galvin and Gagne ©2018
Example: P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) (3,3,2) true
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
Operating System Concepts – 10th Edition 8.36 Silberschatz, Galvin and Gagne ©2018
Deadlock Detection
Detection algorithm
Recovery scheme
Operating System Concepts – 10th Edition 8.37 Silberschatz, Galvin and Gagne ©2018
Single Instance of Each Resource Type
Operating System Concepts – 10th Edition 8.38 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 10th Edition 8.39 Silberschatz, Galvin and Gagne ©2018
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 [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.
Operating System Concepts – 10th Edition 8.40 Silberschatz, Galvin and Gagne ©2018
Detection Algorithm
Operating System Concepts – 10th Edition 8.41 Silberschatz, Galvin and Gagne ©2018
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
Operating System Concepts – 10th Edition 8.42 Silberschatz, Galvin and Gagne ©2018
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 – 10th Edition 8.43 Silberschatz, Galvin and Gagne ©2018
Example (Cont.)
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 – 10th Edition 8.44 Silberschatz, Galvin and Gagne ©2018
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
Operating System Concepts – 10th Edition 8.45 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Process Termination
Operating System Concepts – 10th Edition 8.46 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 10th Edition 8.47 Silberschatz, Galvin and Gagne ©2018
End of Chapter 8
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018