You are on page 1of 32

Deadlocks

By Asghar Rafique Khattak


Outline
 System Model
 Deadlock Characterization
 Methods for handling deadlocks
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
 Recovery from Deadlock
 Combined Approach to Deadlock Handling
1. What is Deadlock?

 An example from US Kansas law:


 “When two trains approach each other at a
crossing, both shall come to a full stop and
neither shall start up again until the other has
gone.”
In Picture Form: VUW CS 305

Neither truck can proceed.


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.
 An object used by a process is called
resource
 Example 1
 System has 2 tape drives. P1 and P2 each hold one tape
drive and each needs the other one.
Definitions

 A process is deadlocked if it is waiting for an


event that will never occur.
Typically, more than one process will be involved in a
deadlock .
 A process is indefinitely postponed if it is
delayed repeatedly over a long period of time
while the attention of the system is given to
other processes,
Example - Bridge Crossing

 Assume traffic in one direction.


 Each section of the bridge is viewed as a resource.
 If a deadlock occurs, it can be resolved only 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 Principles

 A deadlock is a permanent blocking of a set of process

Illustration of a deadlock
Conditions for Deadlock
(Deadlock Characterization)
 The following 4 conditions are necessary and sufficient for
deadlock (must hold simultaneously)
 Mutual Exclusion:
 Only one process at a time can use the resource.
 Hold and Wait:
 Processes hold resources already allocated to them while
waiting for other resources.
 No preemption:
 Resources are released by processes holding them only after
that process has completed its task.
 Circular wait:
 A circular chain of processes exists in which each process
waits for one or more resources held by the next process in
the chain.
Resource Allocation Graph
The resource allocation graph is used to
describe a deadlock graphically. The
graphics has two types of nodes, process
node and resource node. Process node
represented by circle and resource node
represented by rectangle.

RESOURCE
PROCESS
Claim Graph

Process claims resource

Process requests resource

Process is assigned resource

Process releases resource


Graph with cycles

R1

P1 P2

R2
• P1 is using R2
• P1 is waiting for R1
• R1 is being used by P2
• P2 is waiting for R2
Graph with cycles but not deadlock
R1 P3

P1 P2

R2
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.
Methods for handling deadlocks

 Ensure that the system will never enter a


deadlock state.
 Allow the system to potentially enter a
deadlock state, detect it and then recover
 Used by many operating systems, e.g. UNIX
 We can totally ignore the deadlock process.
Deadlock
Prevention
Management
Design the system in such a way that deadlocks can never
occur
Avoidance
Impose less stringent conditions than for prevention, allowing
the possibility of deadlock but sidestepping it as it occurs.
Detection
Allow possibility of deadlock, determine if deadlock has
occurred and which processes and resources are involved.
Recovery
After detection, clear the problem, allow processes to
complete and resources to be reused. May involve destroying
and restarting processes.
Deadlock Prevention
 Mutual Exclusion
 Non-issue for sharable resources.
 Only one process can have access to a printer
at a time, otherwise the output will be
distributed.
 Deadlock cannot only be prevented by mutual
exclusion.
Hold and Wait
 - Deadlock can be prevented by denying hold
and wait.
 Force each process to acquire all the required
resources at once. Process cannot proceed
until all resources have been acquired.
 Its Problem
 A process can also request a resource only
when it is holding no other resources. It
should release them first.
 Problem or Not?
 No Preemption
 If a process that is holding some resources requests
another resource that cannot be immediately allocated
to it, the process releases the resources currently being
held.
 Preempted resources are added to the list of resources
for which the process is waiting.
 A process is requesting for printer and is currently
holding a tap drive. As the printer is not available so tap
drive is also taken from him so that process will be in a
waiting state.
 Circular Wait
 Impose a total ordering of all resource types.

 Require that processes request resources in increasing


order
 Each resource type is given a number

 e.g. tape drive has number 1,disk drive has no. 5 and
printer has no. 12.
 A process wants to read the disk drive and printout the
result. It can not do it in reverse order.

12 Printer

05 Disk drive

1 Tape
Drive
Basic Facts

 If a system is in a safe state  no


deadlocks.
 If a system is in unsafe state  possibility of
deadlock.
 Avoidance  ensure that a system will never
reach an unsafe state.
Deadlock Avoidance
 It is a technique used to avoid deadlock. It requires
the information that how different processes would
request different resources. If we know the request
in advance and also the resources then we can
give it an order.
 SAFE STATE: system having no deadlock
 UNSAFE STATE: system having deadlock
RESOURCE ALLOCATION GRAPH
 Resource-allocation graph algorithm is used to
avoid deadlock. This algorithm uses the
resource-allocation graph by introducing a new
edge called claim edge. The claim edge is used
to indicate that a process may request a
resource in future. It is represented by a dashed
line. When the process requests that resource,
the dashed line is converted to an assignment
edge i.e. a solid line.
Graph with cycles
R1

P1 P2

R2
 It indicates that the resource has been allocated
to the process. After the process releases this
resource, the assignment edge is again
converted to claim edge. All claim edges of a
process are represented in the graph.
Banker’s Algorithm
 Set of resources, set of customers, banker
 Rules
 Each customer tells banker maximum amount of money it needs.
 Customer borrows money from banker.
 Customer eventually pays back loan.
 Banker only lends resources if the system will be in
a safe state after the loan.
Banker’s Algorithm

 Used for multiple resource type.


 Each process must a priori claim maximum
use of each resource type.
 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.
Deadlock Detection
If the system does not use any preventive or
avoidance methods, there is a possibility of
dead lock occurance.If a deadlock occurs in
the system then it should be recovered.
Deadlock Recovery
 We can recover from a deadlock by using two
approaches:
1) Kill the processes that releases all
resources for killed process
2) Take away resources.
Recovery from Deadlock:
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
 How long the process has computed, and how much longer
to completion.
 Resources the process has used.
 Resources process needs to complete.
Recovery from Deadlock:
 Selecting a victim - minimize cost.
The most important decision is the selection of the victim
process to be preempted, will be determined by considering
the number of resources held by that process, the amount of
time consumed by the process etc.
 Rollback
 return to some safe state, restart process from that
state.
 Starvation
 same process may always be picked as victim; include
number of rollback in cost factor.

You might also like