You are on page 1of 22

Chapter 7 Deadlock

Resources
Examples of computer resources
Printers
Tape drives
Tables
Preemptable resources
Can be taken away from a process with no ill
effects
Nonpreemptable resources
Will cause the process to fail if taken away
Reusable resources
Used by one process at a time and not
depleted by that use
Examples: Processors, I/O channels, main and
secondary memory, files, databases, and
semaphores
Shared and exclusive resources
Example of shared resource: FILE
Example of exclusive resource: PRINTER
Consumable resources
Created (produced) and destroyed (consumed)
by a process
Examples: Interrupts, signals, messages, and
information in I/O buffers

Prepared by Dr. Amjad Mahmood

7.1

System Model
A system consists of a number of resources to be
distributed among a number of competing
processes.
There are different types of resources R1, R2,...,
Rm.
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances. For
example, if two CPUs then resource type CPU has
two instances.
Sequence of Events Required to Use a Resource
Each process utilizes a resource as follows:
Request a resource:
Request is made through a system call
Process must wait if request is denied
Requesting process may be blocked
may fail with error code
Use the resource:
The process can operate on the resource.
Release the resource:
The process releases the resource. A
resource is released through a system call.

Prepared by Dr. Amjad Mahmood

7.2

Deadlock
Formal Definition
A set of processes is deadlocked if each
process in the set is waiting for an event that
only another process in the set can cause
Usually the event is release of a currently held
resource
None of the processes can
Run
Release resources
Be awakened
Involve conflicting needs for resources by two or
more processes
Examples of Deadlock
Example 1

System has 2 tape drives. P1 and P2 each


hold one tape drive and each needs another
one.
Example 2
Semaphores A and B, initialized to 1
P0
P1
wait (A);
wait(B)
wait (B);
wait(A)

Prepared by Dr. Amjad Mahmood

7.3

Example 3
Space is available for allocation of 200K bytes,
and the following sequence of events occur
P0

P1

.
Request 80KB;

Request 60KB;

Request 70KB;

Request 80KB;

Deadlock occurs if both processes progress to


their second request
Four Conditions for Deadlock
Deadlock can
simultaneously

arise

if

four

conditions

hold

Mutual exclusion condition:


Only one process at a time can use a
resource (non-shareable resource).
Each resource is assigned to a process or
is available
Hold and wait condition:
A process holding at least one resource
can request for additional resources
No preemption condition:
A resource can be released only voluntarily
by the process holding it. That is previously
granted resources cannot be forcibly taken
away.

Prepared by Dr. Amjad Mahmood

7.4

Circular wait condition:


there exists a set {P0,P1,,P0} 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,,Pn1 is
waiting for a resource that is held by Pn,
and P0 is waiting for a resource that is held
by P0.

Resource-Allocation Graph
Deadlocks can be described more precisely in terms
of a directed graph, called a system resourceallocation graph.
This graph consists of 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.

E is partitioned into two


types as well:

Prepared by Dr. Amjad Mahmood

7.5

Request
edge

directed edge P1 Rj

Assignment
edge
directed edge Rj Pi
Different symbols are used to represent processes
and resources as given below:
Process:
Resource type of 4 instances:
Pi requests instance of Rj:

Pi
Rj

Pi is holding an instance of Rj
:

Prepared by Dr. Amjad Mahmood

Pi
Rj

7.6

Prepared by Dr. Amjad Mahmood

7.7

Method of Handling Deadlocks


Just ignore the problem altogether
Prevention
Ensure that the system will never enter a
deadlock state
Requires negating one of the four necessary
conditions
Dynamic avoidance
Require careful resource allocation

Prepared by Dr. Amjad Mahmood

7.8

Detection and recovery


Allow the system to enter a deadlock state and
then recover
We need some methods to determine whether
or not the system has entered into deadlock.
We also need algorithms to recover from the
deadlock.
The Ostrich Algorithm
Pretend there is no problem
The system will eventually stop functioning
Reasonable if
Deadlocks occur very rarely
Cost of prevention is high
UNIX and Windows takes this approach
It is a trade off between
Convenience
Correctness
Deadlock Prevention
Prevent/deny Mutual Exclusion condition
Use shareable resource.
Impossible for practical system.
Prevent/Deny
Hold and Wait condition
(a) Pre-allocation - Require processes to request
resources before starting
A process never has to wait for what it
needs
Prepared by Dr. Amjad Mahmood

7.9

(b) Process must give up all resources and then


request all immediately needed
Problems
May not know required resources at start
of run
Low resource utilization many resources
may be allocated but not used for long time
Starvation possible a process may have
to wait indefinitely for popular resources.
Prevent/deny No Preemption condition
(a) 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.
(b) The required resource(s) is/are taken back
from the process(s) holding it/them and given
to the requesting process
Problems
Some resources (e.g. printer, tap drives)
cannot be preempted without detrimental
implications.
May require the job to restart
Prevent/Deny
Prepared by Dr. Amjad Mahmood

7.10

Circular Wait
Order resources (each resource type is
assigned a unique integer) and allow process to
request for them only in increasing order
If a process needs several instances of the
same resource, it should issue a single request
for all of them.
Alternatively, we can require that whenever a
process requests an instance of a resource
type it has released all the resources which are
assigned a smaller inter value.
Problem:
Adding a new resource that upsets
ordering requires all code ever written to
be modified
Resource numbering affects efficiency
A process may have to request a
resource well before it needs it, just
because of the requirement that it
must request resources in ascending
order
An example:

Prepared by Dr. Amjad Mahmood

7.11

Deadlock Avoidance
OS never allocates resources in a way that could
lead to a deadlock
Processes must tell OS in advance how many
resources they will request
Some Definitions
State of a system
An enumeration of which processes hold, are
waiting for or might request which resource
Safe state
1. No process is deadlocked, and there exits no
possible sequence of future request in which
deadlock could occur
2. No process is deadlocked and the current state
will not lead to a dead lock state
3. Safe state is where there is at least one
sequence that does not result in deadlock
Prepared by Dr. Amjad Mahmood

7.12

Unsafe state
Is a state that is not safe
Basic Facts
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of
deadlock.
Avoidance ensure that a system will never enter
an unsafe state

Prepared by Dr. Amjad Mahmood

7.13

Deadlock Avoidance with Resource-Allocation


Graph
This algorithm can be used if we have only one
instance of each resource type.
In addition to the request and assignment edges, a
claim edge is also introduced.
Claim edge Pi Rj indicated that process Pj may
request resource Rj in future; 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.
That is, before a process starts executing, all of its
claim edges must already appear in the resourceallocation graph.
Suppose that process Pi requests resource Rj. The
request can be granted only if converting the
request edge if converting the request edge PiRj
to an assignment edge does not result in a cycle in
the resource-allocation graph. That is we use a
cycle detection algorithm is used. If no cycle exits,
the process Pi will have to wait.

Prepared by Dr. Amjad Mahmood

7.14

Resource-allocation graph for deadlock avoidance

An unsafe state in the resource-allocation graph

Prepared by Dr. Amjad Mahmood

7.15

Bankers Algorithm
Applicable to system with multiple instances of
resource types.
Each process must a priori claim maximum use.
When a process requests a resource it may have to
wait.
When a process gets all its resources it must return
them in a finite amount of time.
Bankers algorithm runs each time:
A process requests resource Is it sage?
A process terminates Can I allocate released
resources to a suspended process waiting for
them?
A new state is safe if and only if every process
can complete after allocation is made
Make allocation and then check system
state and deallocate if unsafe

Data Structures for Bankers 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. Max [i,j] = k mean that process
Pi may request at most k instances of 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].
Prepared by Dr. Amjad Mahmood

7.16

Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively.
Initialize: Work = Available
Finish [i]=false for i=1,3, , n.
2. Find and i such that both:
(a) Finish [i] = false
(b) 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.

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

Prepared by Dr. Amjad Mahmood

7.17

Example of Bankers Algorithm


5 processes P0 through P4; 3 resource types
A (10 instances),
B (5 instances), and
C (7 instances).
Snapshot at time T0:

P0
P1
P2
P3
P4

Allocation
ABC
010
200
302
211
002

Max
ABC
753
322
902
222
433

Available
ABC
332

The content of the matrix. Need is defined to be Max


Allocation.
Process
P0
P1
P2
P3
P4

Need
ABC
743
122
600
011
431

The system is in a safe state since the sequence


<P1,P3,P4,P2,P0> satisfies safety criteria.

Prepared by Dr. Amjad Mahmood

7.18

Example P1 Request (1,0,2)


Check that Request Available
that is, (1,0,2) (3,3,2) true.
Process
P0
P1
P2
P3
P4

Allocatio
n
ABC
010
302
301
211
002

Need

Available

ABC
743
020
600
011
431

ABC
230

Executing safety algorithm shows that sequence


<P1, P3, P4, P0, P2> satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection Recovery
Allow system to enter deadlock state
Need a detection algorithm
Need a recovery algorithm
How to Detect a Deadlock Using a Resource-Graph?
If each resource type has exactly one instance and
the graph has a cycle then a deadlock has occurred.
Or if the cycle involves only a set of resource types,
each of which has only a single instance, then the
deadlock has occurred.

Prepared by Dr. Amjad Mahmood

7.19

Therefore, a cycle in the graph is both a


necessary and sufficient condition for the
existence of a deadlock.

Prepared by Dr. Amjad Mahmood

7.20

Examples:

Resource-allocation graph with a deadlock

Recovery from Deadlocks Process Termination


Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle
is eliminated.
In which order should we choose to abort?
Priority of the process.
Prepared by Dr. Amjad Mahmood

7.21

How long process has computed, and how


much longer to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be
terminated?
Is process interactive or batch?
Recovery from Deadlocks Resource Preemption
Selecting a victim minimize cost.
Rollback return to some safe state, restart process
for that state.
Starvation same process may always be picked
as victim, include number of rollback in cost factor.
Combined Approach to Deadlock Handling
Combine the three basic approaches
prevention
avoidance
detection
allowing the use of the optimal approach for each of
resources in the system.
Partition resources into hierarchically ordered
classes.
Use most appropriate technique for handling
deadlocks within each class.

Prepared by Dr. Amjad Mahmood

7.22

You might also like