Professional Documents
Culture Documents
07 MH Deadlock
07 MH Deadlock
Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018
Current Standing
Lab 10/12
Assignment 2/3
Quiz 3/5
OHT 1 /2
ESE 0/1
Operating System Concepts – 10th Edition 1.2 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 1.3 Silberschatz, Galvin and Gagne ©2018
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 1.4 Silberschatz, Galvin and Gagne ©2018
Deadlock
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 1.5 Silberschatz, Galvin and Gagne ©2018
System Model
Operating System Concepts – 10th Edition 1.6 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 1.7 Silberschatz, Galvin and Gagne ©2018
Deadlock Characterization
Operating System Concepts – 10th Edition 1.9 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
Operating System Concepts – 10th Edition 1.10 Silberschatz, Galvin and Gagne ©2018
Resource Allocation Graph with a Deadlock
Operating System Concepts – 10th Edition 1.11 Silberschatz, Galvin and Gagne ©2018
Graph with a Cycle But no Deadlock
Operating System Concepts – 10th Edition 1.12 Silberschatz, Galvin and Gagne ©2018
Basic Facts
If graph contains no cycles no deadlock
If graph contains a cycle
• if only one instance per resource type, then deadlock
• if several instances per resource type, possibility of deadlock
Operating System Concepts – 10th Edition 1.13 Silberschatz, Galvin and Gagne ©2018
Methods for Handling Deadlocks
1. Ignore the problem and pretend that deadlocks never occur in the
system.
2. Ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
3. Allow the system to enter a deadlock state and then recover
Operating System Concepts – 10th Edition 1.14 Silberschatz, Galvin and Gagne ©2018
Deadlocks Prevention vs Avoidance
Deadlock prevention provides a set of methods to ensure that at least
one of the necessary conditions (4x) cannot hold.
• These methods prevent deadlocks by constraining how requests
for resources can be made
Operating System Concepts – 10th Edition 1.15 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:
Operating System Concepts – 10th Edition 1.16 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:
Hold and Wait –
• Must guarantee that whenever a process requests a resource,
it does not hold any other resources
• Require process to request and be allocated all its resources
before it begins execution, or
• Allow process to request resources only when the process
has none allocated to it. Before it can request any additional
resources, it must release all the resources that it is currently
allocated
• Both these protocols have two main disadvantages.
Low resource utilization: since resources may be allocated
but unused for a long period
Starvation possible: A thread that needs several popular
resources may have to wait indefinitely
Operating System Concepts – 10th Edition 1.17 Silberschatz, Galvin and Gagne ©2018
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 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
The three options presented thus far for deadlock prevention are
generally impractical in most situations, Circular-wait condition
— presents an opportunity for a practical solution by invalidating
one of the necessary conditions.
Operating System Concepts – 10th Edition 1.18 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
Circular Wait:
• One way to ensure that this condition never holds is to impose a
total ordering of all resource types and to require that each thread
requests resources in an increasing order of enumeration
To illustrate, we let R = {R1, R2, ..., Rm} be the set of resource types.
We assign to each resource type a unique integer number, which
allows us to compare two resources and to determine whether one
precedes another in our ordering. Formally, we define a one-to-one
function F: R → N, where N is the set of natural numbers.
Operating System Concepts – 10th Edition 1.19 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Invalidating the circular wait condition is most common.
Simply assign each resource (i.e., mutex locks) a unique number.
Resources must be acquired in order.
If:
first_mutex = 1
second_mutex = 5
Operating System Concepts – 10th Edition 1.20 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Keep in mind that developing an ordering, or hierarchy, does not in itself prevent
deadlock. It is up to application developers to write programs that follow the ordering.
However, establishing a lock ordering can be difficult, especially on a system with
hundreds—or thousands—of locks.
Imposing a lock ordering does not guarantee deadlock prevention if locks can be
acquired dynamically
• Deadlock is possible if two threads simultaneously invoke the transaction() function,
transposing different accounts.
Operating System Concepts – 10th Edition 1.21 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance
Deadlock Prevention: prevent deadlocks by limiting how requests can be
made. however, are low device utilization and reduced system throughput
Deadlock Avoidance: Requires that the system has some additional a
priori information available
Operating System Concepts – 10th Edition 1.22 Silberschatz, Galvin and Gagne ©2018
Safe State
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
Operating System Concepts – 10th Edition 1.23 Silberschatz, Galvin and Gagne ©2018
Basic Facts
If a system is in safe state no deadlocks
Operating System Concepts – 10th Edition 1.24 Silberschatz, Galvin and Gagne ©2018
Safe, Unsafe, Deadlock State
Operating System Concepts – 10th Edition 1.25 Silberschatz, Galvin and Gagne ©2018
Avoidance Algorithms
Single instance of a resource type
• Use a resource-allocation graph
Operating System Concepts – 10th Edition 1.27 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 1.28 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
Operating System Concepts – 10th Edition 1.29 Silberschatz, Galvin and Gagne ©2018
Unsafe State In Resource-Allocation Graph
Operating System Concepts – 10th Edition 1.30 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph Algorithm
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
Operating System Concepts – 10th Edition 1.31 Silberschatz, Galvin and Gagne ©2018
Banker’s Algorithm
Multiple instances of resources
When a process gets all its resources it must return them in a finite
amount of time
Operating System Concepts – 10th Edition 1.32 Silberschatz, Galvin and Gagne ©2018
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
Operating System Concepts – 10th Edition 1.33 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
Operating System Concepts – 10th Edition 1.34 Silberschatz, Galvin and Gagne ©2018
Resource-Request Algorithm for Process Pi
Operating System Concepts – 10th Edition 1.35 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Operating System Concepts – 10th Edition 1.36 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Operating System Concepts – 10th Edition 1.37 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Operating System Concepts – 10th Edition 1.38 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Operating System Concepts – 10th Edition 1.39 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
P2 3 0 2 902 600
P3 2 1 1 422 211
P4 0 0 2 533 531
--------------
725
Operating System Concepts – 10th Edition 1.40 Silberschatz, Galvin and Gagne ©2018
Example of Banker’s Algorithm
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
Operating System Concepts – 10th Edition 1.45 Silberschatz, Galvin and Gagne ©2018
Deadlock Detection
Detection algorithm
Recovery scheme
Operating System Concepts – 10th Edition 1.46 Silberschatz, Galvin and Gagne ©2018
Single Instance of Each Resource Type
Maintain wait-for graph
• Nodes are processes
• Pi Pj if Pi is waiting for Pj
Operating System Concepts – 10th Edition 1.47 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 10th Edition 1.48 Silberschatz, Galvin and Gagne ©2018
Several Instances of a Resource Type
Operating System Concepts – 10th Edition 1.49 Silberschatz, Galvin and Gagne ©2018
Detection Algorithm
Operating System Concepts – 10th Edition 1.50 Silberschatz, Galvin and Gagne ©2018
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
Operating System Concepts – 10th Edition 1.51 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 1.52 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 1.53 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 1.54 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Process Termination
Operating System Concepts – 10th Edition 1.55 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 10th Edition 1.56 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention vs Deadlock Avoidance
Operating System Concepts – 10th Edition 1.57 Silberschatz, Galvin and Gagne ©2018
End
Reference Ch. 8
Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018