Sunteți pe pagina 1din 32

Deadlocks

 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

Deadlock Involving Same Resource Type


Printer Tape Drive

R H H R

P2 P1

Deadlock Involving Different Resource Types


Bridge Crossing Example

 Traffic only in one direction.


 Each section of a bridge can be viewed as a resource.
 If a deadlock occurs, it can be resolved if one car backs
up (preempt resources and rollback).
 Several cars may have to be backed up if a deadlock
occurs.
 Starvation is possible.
Deadlock Characterization
 Deadlock can arise if the following 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 the process holding it, after that process has
completed its task.
 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.
Resource-Allocation Graph
A set of vertices V and a set of edges E.
 V is partitioned into two types:
 P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.

 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 :

 Resource Type with 4 instances :

 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 ={P1R1, R1P2, P2R3, R3P3, R2 P1,
R2P2}
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

Cycle 1 : P1 R1 P2R3—P3R2P1


Cycle 2 : P2R3P3R2P2

Process : P1,P2 & P3 are deadlocked

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.

Resource allocation graph with a cycle but no deadlock


Methods for Handling Deadlocks
 Ensure that the system will never enter a deadlock state:
 Deadlock prevention
 Mutual exclusion
 Hold and wait
 No preemption
 Circular wait
 Deadlock avoidance
 Banker’s Algorithm and Safety Algorithm
 Allow the system to enter a deadlock state and then recover:
 Deadlock detection
 Single instance of each resource type
 Several instances of each resource type (Banker’s algorithm)
 Recovery from deadlock
 Process termination
 Resource Preemption
 Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.
Deadlock Prevention
Restrain the ways request can be made.
 Mutual Exclusion – not required for sharable resources (read-only files)
must hold for nonsharable resources (i.e. printers, plotters).
 Hold and Wait – can use one of the following two protocols:
 All resources are allocated before a process starts its execution.
 The process must release all the currently allocated resources before
requesting any additional resources.
Disadvantages:
 Low resource utilization
 Starvation possible
 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.
 Circular Wait – impose a total ordering of all resource types, and require
that each process requests resources in an increasing order of
enumeration. No circular wait condition is held if total ordering protocol is
used, F(R0)<F(R1)< F(R2)<F(R3)<, …. , <F(Rn-1)<F(Rn)< F(R0).
Deadlock Avoidance
 Requires that the system has some additional a priori
information available (the resources currently available, the
resources currently allocated to each process, and the
future requests and releases of each process to decide
whether the current request can be satisfied or must wait to
avoid a possible future deadlock).
 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.
Safe State
 A state is safe if the system can allocate resources to each process
(up to its maximum) in some order and still avoid a deadlock.
 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 safe sequence of all
processes.
 Sequence <P1, P2, …, Pn> is safe if 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.
 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.
 If no such sequence exists, then the system state is said to be unsafe.
Basic Facts
 If a system is in safe state  there is no possibility of deadlock.
 If a system is in unsafe state  there is a possibility of deadlock.
 Avoidance  ensure that a system will never enter an unsafe state.
Example
Tape Drives = 12
At t0 time :
Process Max Needs Current Needs
P0 10 5
P1 4 2 Free = 3
P2 9 2
• Safe state: sequence <P1, P0, P2> satisfies the safety
condition.
At t1 time :
Process Max Needs Current Needs
P0 10 5
P1 4 2 Free = 2
P2 9 3
• Unsafe state: System goes into a deadlock state as P0 and
P1 cannot be allocated the additional resources.
Resource-Allocation Graph Algorithm
 Claim edge Pi  Rj indicated that process Pj may request resource Rj;
represented by a dashed line.
 Claim edge is converted to request edge when a process requests a
resource.
 The request can be granted only if converting the request edge Pi 
Rj to an assignment edge Rj  Pi does not result in the formation of a
cycle in the resource-allocation graph.
 When a resource is released by a process, assignment edge is
reconverted to a claim edge.
 Resources must be claimed a priori in the system.
 An algorithm for detecting a cycle in this graph requires an order of n2
operations, where n is the number of processes in the system.
 If no cycle exists, then the allocation of the resource will leave the
system in a safe state. If a cycle is found, then the allocation will put
the system in an unsafe state, Therefore, process Pi will have to wait
for its requests to be satisfied.
Unsafe State in Resource-Allocation
Resource-Allocation Graph for Deadlock
Graph Avoidance
Banker’s Algorithm
 Multiple instances of each resource type.

 Each process must have a priori claim,


maximum use of resources of each type.
 When a process requests resources it may
have to wait if the allocation of resources
will leave the system in unsafe state.
 When a process gets all its resources it
must return them in a finite amount of time.
Data Structures for the 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]


 The vector Allocationi specifies the resources currently
allocated to process Pi; the vector Needi specifies the
additional resources that process Pi may still request to
complete its task.
Safety Algorithm
 The algorithm for finding out whether or not a system is in a safe
state can be described as follows:
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 1, 2, 3, …, n.
2. Find an i such that both:
 Finish [i] = false
 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.
 This algorithm may require an order of m x n2 operations to
decide whether a state is safe.
Resource-Request Algorithm for Process Pi
 Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj.
 When a request for resources is made by process Pi , the following
actions are taken:
1. If Requesti  Needi go to step 2. Otherwise, raise an error
condition, since process has exceeded its maximum claim.
2. If Requesti  Available, go to step 3. Otherwise, Pi must wait,
since the 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 the old resource-allocation state
is restored
Example of Banker’s Algorithm
 Processes: P0 , P1 , P2 , P3, P4 ; Resources: A=10, B=5, C=7.
 Snapshot at time T0:
(Work)
Allocation Max Available Need
ABC ABC A B C A B C
P0 010 753 3 3 2 7 4 3
P1 200 322 5 3 2 1 2 2
P2 302 902 10 4 7 6 0 0
P3 211 222 7 4 3 0 1 1
P4 002 433 7 4 5 4 3 1

 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.

 Algorithm requires an order of (m x n2) operations to detect whether


the system is in a deadlocked state.
Example of Detection Algorithm
 Processes: P0, P1, P2, P3, P4; A =7, B= 2 , and C= 6.
 Snapshot at time T0:

Processes Allocation Request Available


ABC ABC ABC
P0 010 000 0 0 0 (0 1 0)
P1 200 202 726
P2 303 000 313
P3 211 100 524
P4 002 002 526

 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.

 P2 requests an additional instance of type C.

Processes Allocation Request Available


ABC ABC ABC
P0 010 000 0 0 0 (0 1 0)
P1 200 202
P2 303 001
P3 211 100
P4 002 002

 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

S-ar putea să vă placă și