Documente Academic
Documente Profesional
Documente Cultură
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
System Model
(The Deadlock Problem)
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
Resource types R1, R2, . . ., Rm
Physical resources: CPU cycles, memory space, I/O devices etc.
Logical resources: Files, semaphores, monitors etc.
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request (open file, allocate memory, allocate printer etc.)
use
release (close file, free memory, release printer etc.)
Example - system has 2 tape drives, P1 and P2 each hold one tape
drive and each needs another one.
Example - semaphores A and B, initialized to 1:
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Example – bridge crossing
System Model
(The Deadlock Problem)
TAPE DRIVES R : Request
H : Holding
R R
H H H
R
P1 P2 P3
R H H R
P2 P1
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
request edge : directed edge P1 Rj
assignment edge : directed edge Rj Pi
Process :
Pi requests instance of Rj : Pi
Rj
Pi is holding an instance of Rj : Pi
Rj
Example of a Resource Allocation Graph
R1 R3
Request Edge
Assignment Edge
W H W H
P1 P2 P3
H
H
R2
R4
P = {P1, P2, P3}
R ={R1, R2, R3, R4}
E ={P1R1, R1P2, P2R3, R3P3, R2 P1,
R2P2}
P1 is holding R2 and is waiting for R1
P2 is holding R1&R2 and is waiting for R3
P3 is holding R3
R1 R3
P1 P2 P3
R2
R4
Resource-allocation graph with a deadlock
P2
R1
If graph contains no cycle, there is
no deadlock.
P3 If graph contains a cycle:
P1 - If only one instance per resource
type, then there is a deadlock.
R2 - If several instances per resource
type, then there is a possibility of
P4 deadlock.
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria.
Example of Banker’s Algorithm
[P1 Requests (1,0,2)]
Check that Request need (i.e. 1, 0, 2, 1, 2, 2) true.
Check that Request Available (i.e. (1,0,2) (3,3,2) true.
(Work)
Allocation Max Available Need
ABC A B C A B C ABC
P0 0 1 0 7 5 3 2 3 0 (755) 743
P1 3 0 2 3 2 2 5 3 2 020
P2 3 0 2 9 0 2 10 5 7 600
P3 2 1 1 2 2 2 7 4 3 011
P4 0 0 2 4 3 3 7 4 5 431
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement. Hence request of P1 can be granted
immediately.
Deadlock Detection
A deadlock situation may occur in the absence of a
deadlock-prevention or a deadlock avoidance
algorithm.
In such a situation, the OS must provide:
An algorithm that examines the state of the system
to determine whether a deadlock has occurred.
An algorithm to recover from the deadlock.
Disadvantages of this scheme are:
The run-time costs of maintaining the necessary
information and executing the detection algorithm.
The potential losses inherent in recovering from a
deadlock.
Deadlock Detection
(Single Instance of Each Resource Type)
Maintains a wait-for graph obtained from the
resource-allocation graph:
Nodes are processes.
Pi Pj implies that Pi is waiting for Pj to
release a resource that Pi needs.
Pi Pj exists if Pi RQ and RQ Pj edges
are available on the resource-allocation
graph.
A deadlock exists in the system if and only if Resource-Allocation Graph
the wait-for graph contains a cycle.
To detect deadlocks, the system needs to:
maintain the wait-for graph.
periodically invoke an algorithm that
searches for a cycle in the graph.
An algorithm to detect a cycle in a graph
requires an order of n2 operations, where n
is the number of vertices in the graph.
Corresponding wait-for graph
Deadlock Detection
(Several Instances of a Resource Type)
This algorithm also employs following time-varying data
structures (like Banker’s Algorithm):
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.
The detection algorithm simply investigates every
possible sequence for the processes that remain to be
completed.
Deadlock 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.
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 a
deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked.
The system is not in a deadlocked state. The safe sequence <P0, P2,
P3, P4, P1>.
Example of Detection Algorithm
Processes: P0, P1, P2, P3, P4; A =7, B= 2 , and C= 6.
State of system
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes’ requests.
Deadlock exists which consists of processes P1, P2, P3, and P4.
Detection-Algorithm Usage
When, and how often, to invoke depends upon the following factors:
How often a deadlock is likely to occur?
How many processes will be affected by deadlock when it
happens?
It should be invoked frequently if deadlocks occur frequently to release
resources and to reduce number of processes involved in the
deadlock.
An extreme option is that the detection algorithm be invoked every
time a request for allocation cannot be granted immediately. It causes
a considerable overhead in computation time.
Another option is to invoke it at less frequent intervals (i.e. CPU
utilization < 40% or once per hour). Here it is difficult to tell which of
the many deadlocked processes caused the deadlock.
If detection algorithm is invoked at arbitrary in time, there may be
many cycles in the resource graph and so it will be difficult to know
which of the many deadlocked processes “caused” the deadlock.
Recovery from Deadlock
The system can recover from the deadlock automatically using the following
options:
Process termination
Resource preemption
Process Termination
Abort all deadlocked processes: This method will break the deadlock cycle
but at a great expense as the results of partial computations of processes
must be discarded and probably recomputed later.
Abort one process at a time until the deadlock cycle is eliminated: It also
incurs considerable overhead as the detection algorithm has to be invoked
after termination of each process to determine whether any processes are still
deadlocked.
Aborting a process may not be easy (midst of updating a file or midst of
printing data on the printer).
Only those processes should be selected for termination which incur the
minimum cost considering the following factors:
Priority
Completed and remaining computations
Number and types of resources held
More resources needed
Number of processes needed for termination
Interactive or batch process
Recovery from Deadlock: Resource Preemption
In this scheme, some resources from processes are successively
preempted and given to other processes until the deadlock cycle is
broken.
Three issues are needed to be addressed to recover from deadlocks
using resource preemption method:
Selecting a victim: Factors to be considered to minimize cost are:
Number of resources a deadlock process is holding.
The amount of time a deadlocked process has consumed
during its execution.
Rollback: Two methods can be used:
If a resource is preempted from a process, it must be rolled
back to some safe state and restart it from that state.
A total rollback (abort the process and then restart it) is more
effective as it is difficult to determine a safe state of a process.
Starvation: Starvation can occur if resources are always
preempted from the same process primarily on cost factors.
System must ensure that a process can be picked as a victim only
a finite number of times (to include number of rollbacks in the cost
factor).
Combined Approach to Deadlock Handling
Three basic approaches can be combined for using an optimal
algorithm:-
Prevention
Avoidance
Detection
Suppose a system consists of the following four classes of
resources:
Internal resources: used by the system such as PCB.
Prevention through resource ordering
Central memory: used by a user job.
Prevention through preemption
Job resources: assigned devices (such as tape drives) and
files.
Avoidance can be used
Swappable space: space for each user job on the backing
store.
Pre-allocation can be used