You are on page 1of 9

Deadlock


Permanent blocking of a set of processes
that either compete for system resources or
Operating Systems I communicate with each other


No efficient solution
Unit 8 – Deadlocks

Involve conflicting needs for resources by
Dr. Alejandro Zunino - ISISTAN two or more processes

Examples


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)

Bridge Crossing Example System Model

 Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices
 Each resource type Ri has Wi instances.

Traffic only in one direction.

Each process utilizes a resource as follows:

Each section of a bridge can be viewed as a resource.

request

If a deadlock occurs, it can be resolved if one car 
use
backs up (preempt resources and rollback). 
release

Several cars may have to be backed up if a deadlock
occurs.

Starvation is possible.
Deadlock Characterization Resource-Allocation Graph

Deadlock can arise if four conditions hold 
A set of vertices V and a set of edges E.
simultaneously:

Mutual exclusion: only one process at a time can use a 
V is partitioned into two types:
resource.

Hold and wait: a process holding at least one resource is
 P = {P1, P2, …, Pn}, the set consisting of all the
waiting to acquire additional resources held by other processes in the system.
processes. R = {R1, R2, …, Rm}, the set consisting of all

No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed its resource types in the system.
task.  request edge – directed edge P1  Rj
 Circular wait: there exists a set { P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held by  assignment edge – directed edge Rj  Pi
P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by Pn

Reusable Resources Example of Deadlock


Used by one process at a time and not
depleted by that use

Processes obtain resources that they later
release for reuse by other processes

Processors, I/O channels, main and
secondary memory, files, databases, and
semaphores

Deadlock occurs if each process holds one
resource and requests the other

Another Example of Deadlock Consumable Resources


Space is available for allocation of 200K 
Created (produced) and destroyed
bytes, and the following sequence of events (consumed) by a process
occur 
Interrupts, signals, messages, and
information in I/O buffers
P1 P2

... ...
1 2
Deadlock may occur if a Receive message is
Request 80K bytes; Request 70K bytes;
... ... 
50K
Request 60K bytes; Request 80K bytes;
blocking

May take a rare combination of events to
cause deadlock

Deadlock occurs if both processes progress
to their second request
Example of Deadlock Resource-Allocation Graph


Deadlock occurs if receive is blocking 
Process

...
P1

...
P2

Resource Type with 4 instances
Receive(P2); Receive(P1);
... ...
Send(P2, M1); Send(P1, M2);
 Pi requests instance of Rj
Pi
Rj
 Pi is holding an instance of Rj
Pi
Rj

Example of a Resource Allocation Resource Allocation Graph With A


Graph Deadlock

Resource Allocation Graph With A


Some Facts
Cycle But No Deadlock

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...
Methods for Handling Deadlocks Deadlock Prevention

Restrain the ways request can be made.

Ensure that the system will never enter a 
Mutual Exclusion – not required for
deadlock state. sharable resources; must hold for
nonsharable resources.

Allow the system to enter a deadlock state 
Hold and Wait – must guarantee that
and then recover. whenever a process requests a resource, it
does not hold any other resources.

Ignore the problem and pretend that 
Require process to request and be allocated all
deadlocks never occur in the system; used its resources before it begins execution, or allow
by most operating systems, including UNIX. process to request resources only when the
process has none.

Low resource utilization; starvation possible.

Deadlock Prevention (Cont.) Deadlock Avoidance



No Preemption –

If a process that is holding some resources requests 
Requires that the system has some
another resource that cannot be immediately additional a priori information available
allocated to it, then all resources currently being 
Simplest and most useful model requires that each
held are released. process declare the maximum number of

Preempted resources are added to the list of resources of each type that it may need.
resources for which the process is waiting. 
The deadlock-avoidance algorithm dynamically

Process will be restarted only when it can regain its examines the resource-allocation state to ensure
old resources, as well as the new ones that it is
that there can never be a circular-wait condition.
requesting. 
Resource-allocation state is defined by the number

Circular Wait – impose a total ordering of of available and allocated resources, and the
all resource types, and require that each maximum demands of the processes.
process requests resources in an increasing
order of enumeration.

Safe State Basic Facts


When a process requests an available resource, system 
If a system is in safe state  no
must decide if immediate allocation leaves the system in a
safe state. deadlocks.

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

If a system is in unsafe state 
resources that Pi can still request can be satisfied by possibility of deadlock.
currently available resources + resources held by all the Pj,
with j<I.
 If Pi resource needs are not immediately available, then Pi can wait

Avoidance  ensure that a system will never
until all Pj have finished. enter an unsafe state.

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.
Resource-Allocation Graph
Safe, Unsafe , Deadlock State
Algorithm
 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.

When a resource is released by a process,
assignment edge reconverts to a claim
edge.

Resources must be claimed a priori in the
system.

Resource-Allocation Graph For Unsafe State In Resource-


Deadlock Avoidance Allocation Graph

Data Structures for the Banker’s


Banker’s Algorithm
Algorithm

Multiple instances. 
Let n = number of processes, and m =
number of resources types.

Each process must a priori claim maximum 
Available: Vector of length m. If available
use. [j] = k, there are k instances of resource
type Rj available.

When a process requests a resource it may 
Max: n x m matrix. If Max [i,j] = k, then
have to wait. process Pi may request at most k instances
of resource type Rj.

When a process gets all its resources it must
return them in a finite amount of time.
Data Structures for the Banker’s
Safety Algorithm
Algorithm
1.Let Work and Finish be vectors of length m

Allocation: n x m matrix. If Allocation[ i,j] = and n, respectively. Initialize:
k then Pi is currently allocated k instances of Work = Available
Finish [i] = false for i - 1,3, …, n.
Rj. 2.Find and i such that both:
 Need: n x m matrix. If Need[i,j] = k, then Pi (a) Finish [i] = false
may need k more instances of Rj to complete (b) Needi  Work
its task. If no such i exists, go to step 4.
3.Work = Work + Allocationi
Need [i,j] = Max[i,j] – Allocation [i,j]. Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the
system is in a safe state.

Resource-Request Algorithm for Resource Allocation Graph With A


Process Pi Cycle But No Deadlock
Request = request vector for process Pi. If
Requesti [j] = k then process Pi wants k
instances of resource type Rj.
1. If Requesti  Needi go to step 2. Otherwise,
raise error condition, since process has
exceeded its maximum claim.
2. If Requesti  Available, go to step 3.
Otherwise Pi must wait, since resources are not
available.

Resource-Request Algorithm for


Example of Banker’s Algorithm
Process Pi (Cont.)
 5 processes P0 through P4; 3 resource types A
(10 instances), B (5 instances, and C (7
3. Pretend to allocate requested resources to Pi
instances).
by modifying the state as follows:
Available = Available = Requesti;  Snapshot at time T0:
Allocationi = Allocationi + Requesti; Allocation Max Available
Needi = Needi – Requesti;;
ABC ABC ABC
• If safe  the resources are allocated to P i. P0 0 1 0 7 5 3 332
• If unsafe  Pi must wait, and the old resource-
P1 2 0 0 322
allocation state is restored
P2 3 0 2 902
P3 2 1 1 222
P4 002 433
Example P1 Request (1,0,2)
Example (Cont.)
(Cont.)

Check that Request  Available (that is,

The content of the matrix. Need is defined (1,0,2)  (3,3,2)  true.
to be Max – Allocation. Allocation Need Available
Need ABC ABC ABC
ABC P0 0 1 0 743 230
P0 743 P1 302 020
P1 122 P2 301 600
P2 600
P3 211 011
P3 011
P4 002 431
P4 431
 Executing safety algorithm shows that sequence < P1, P3,

The system is in a safe state since the P4, P0, P2> satisfies safety requirement.
sequence < P1, P3, P4, P2, P0> satisfies  Can request for (3,3,0) by P4 be granted?
safety criteria.  Can request for (0,2,0) by P0 be granted?

Single Instance of Each Resource


Deadlock Detection
Type

Allow system to enter deadlock state 
Maintain wait-for graph

Nodes are processes.

Detection algorithm  Pi  Pj if Pi is waiting for Pj.


Recovery scheme 
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.

Resource-Allocation Graph and Several Instances of a Resource


Wait-for Graph 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 [ij] = k, then process Pi is
requesting k more instances of resource
Resource-Allocation Graph Corresponding wait-for graph
type. Rj.
Detection Algorithm Detection Algorithm (Cont.)

1.Let Work and Finish be vectors of length m 3.Work = Work + Allocationi


and n, respectively Initialize: Finish[i] = true
(a) Work = Available go to step 2.
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false;otherwise, Finish[i] = true. 4. If Finish[i] == false, for some i, 1  i 
2.Find an index i such that both: n, then the system is in deadlock state.
(a) Finish[i] == false Moreover, if Finish[i] == false, then Pi is
(b) Requesti  Work deadlocked.
Algorithm requires an order of O( m x n2) operations
If no such i exists, go to step 4. to detect whether the system is in deadlocked state.

Example of Detection Algorithm Example (Cont.)


 Five processes P0 through P4; three resource  P2 requests an additional instance of type C.
types Request
ABC
A (7 instances), B (2 instances), and C (6 P0 000
instances). P1 201
 Snapshot at time T0: P2 001
Allocation Request Available P3 100
ABC ABC ABC P4 002
P0 010 000 000
P1 200 202

State of system?
P2 303 000
 Can reclaim resources held by process P0, but
P3 211 100 insufficient resources to fulfill other processes;
P4 002 002
requests.
 Sequence < P0, P2, P3, P1, P4> will result in  Deadlock exists, consisting of processes P1, P2,
Finish[i] = true for all i. P3, and P4.

Recovery from Deadlock:


Detection-Algorithm Usage
Process Termination

When, and how often, to invoke depends 
Abort all deadlocked processes.
on: 
Abort one process at a time until the

How often a deadlock is likely to occur? deadlock cycle is eliminated.

How many processes will need to be rolled 
In which order should we choose to abort?
back? 
Priority of the process.

one for each disjoint cycle 
How long process has computed, and how much
longer to completion.

If detection algorithm is invoked arbitrarily, 
Resources the process has used.
there may be many cycles in the resource 
Resources process needs to complete.
graph and so we would not be able to tell 
How many processes will need to be
which of the many deadlocked processes terminated.
“caused” the deadlock. 
Is process interactive or batch?
Recovery from Deadlock: Combined Approach to Deadlock
Resource Preemption Handling

Combine the three basic approaches

Selecting a victim – minimize cost. 
prevention

avoidance

Rollback – return to some safe state, restart 
detection
process for that state. allowing the use of the optimal approach
for each of resources in the system.

Starvation – same process may always be
picked as victim, include number of rollback

Partition resources into hierarchically
in cost factor. ordered classes.


Use most appropriate technique for handling
deadlocks within each class.

You might also like