DEADLOCK PREVENTION &
RECOVERY
I am S.RAJAPRIYA
I am here because I love to give a fright.
You can find me at rajapriya@vvvcollege.org
DEADLOCK
2
✘ The Deadlock Problem
✘ System Model
✘ Deadlock Characterization
✘ Methods for Handling
Deadlocks
✘ Deadlock Prevention
✘ Deadlock Avoidance
✘ Deadlock Detection
✘ Recovery from Deadlock
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
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs
another one
3
“A Deadlock is a permanent blocking of a
set of threads”
4
SYSTEM MODEL
✘ Resources typesR1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
✘ Each resource type Ri has Wi instances.
✘ Each process utilizes a resource as follows:
✗ request
✗ use
✗ release
5
Mutual exclusion :
only one process at a time can
use a resource. If another process request
that resource, the requesting process must
be delayed until the resource has been
released
Hold and wait :
a process holding at least one
resource is waiting to acquire additional
resources held by other processes
Deadlock Characterization
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, …, 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, …, Pn–1 is waiting for a resource that is
held by
Pn, and P0 is waiting for a resource that is
held by P0.
6
RESOURCE ALLOCATION GRAPH
Deadlock can be described more precisely in terms of a directed
graph called a system 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
Example
7
Resource-Allocation Graph
✘ Pictorially, we represent
✘ Process
✘ Resource Type with 4 instances
✘ Pi requests instance of Rj
✘ Pi is holding an instance of Rj
8
Pi
Pi
Example of a Resource Allocation Graph
• The sets P,R,E:
o P={p1,p2,p3}
o R={R1,R2,R3,R4}
o E={P1->R1,P2->R3,R1->P2,
R2->P2,R2->P1,R3->P3}
9
Resource Allocation Graph with Deadlock
• Suppose that process P3 request an
instance of resource type R3.
• Since no resource is currently
available, a request edge P3->R2 is
added to the graph.
• At this point two cycles exist in the
system:
✘ P1->R1->P2->R3->P3->R2->P1
✘ P2->R3->P3->R2->P2
• Processes P1,P2,P3 are deadlocked.
10
Graph with a Cycle but No Deadlock
• In this example we also have a
cycle
✘ P1->R1->P3->R2->P1
• However, there is no cycle.
• Observe that process P4 may
release its resource R2.That
resource can then be allocated to
P3,breaking the cycle.
11
✘ 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
12
DEADLOCK PREVENTION
✘ By ensuring that at least one of these conditions cannot hold,
We can prevent the occurrence of a deadlock
13
Eliminate Mutual Exclusion –
not required for sharable
resources, must hold for non-sharable
resources. For example, a printer cannot
be shared by several processes. Read
only files are good example of sharable
resource
Eliminate Hold and Wait –
must guarantee that
whenever a process requests a
resource, it does not hold any other
resources.
Deadlock Prevention
✗ Allocate all required resources to the
process before the start of its
execution, this way hold and wait
condition is eliminated but it will
lead to low device utilization.
✗ For example, if a process requires
printer at a later time and we have
allocated printer before the start of
its execution printer will remain
blocked till it has completed its
execution.
✗ The process will make a new request
for resources after releasing the
current set of resources. This
solution may lead to starvation.
14
Eliminate 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.
Deadlock Prevention
✘ Process will be restarted
only when it can regain its
old resources, as well as the
new ones that it is
requesting.
15
Deadlock Prevention
Eliminate Circular Wait –
• Each resource will be assigned
with a numerical number. A
process can request the
resources increasing/decreasing
order of numbering.
•For Example, if P1 process is allocated
R5 resources, now next time if P1 ask
for R4, R3 lesser than R5 such request
will not be granted, only request for
resources more than R5 will be granted.
16
A picture is worth a thousand words
17
Safe State!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.
18
19
Safe, Unsafe and Deadlock State
Place your screenshot here
Recovery from Deadlock
20
Process Termination
Resource Preemption
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?
21
Resource Preemption
Selecting a victim
Starvation –
same process
may always be
picked as victim,
include number of
rollback in cost
factor
Rollback – return
to some safe
state, restart
process for that
state.
22
Thankyou
23

Deadlock

  • 1.
    DEADLOCK PREVENTION & RECOVERY Iam S.RAJAPRIYA I am here because I love to give a fright. You can find me at rajapriya@vvvcollege.org
  • 2.
    DEADLOCK 2 ✘ The DeadlockProblem ✘ System Model ✘ Deadlock Characterization ✘ Methods for Handling Deadlocks ✘ Deadlock Prevention ✘ Deadlock Avoidance ✘ Deadlock Detection ✘ Recovery from Deadlock
  • 3.
    The Deadlock Problem Aset of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Example System has 2 disk drives P1 and P2 each hold one disk drive and each needs another one 3
  • 4.
    “A Deadlock isa permanent blocking of a set of threads” 4
  • 5.
    SYSTEM MODEL ✘ ResourcestypesR1, R2, . . ., Rm CPU cycles, memory space, I/O devices ✘ Each resource type Ri has Wi instances. ✘ Each process utilizes a resource as follows: ✗ request ✗ use ✗ release 5
  • 6.
    Mutual exclusion : onlyone process at a time can use a resource. If another process request that resource, the requesting process must be delayed until the resource has been released Hold and wait : a process holding at least one resource is waiting to acquire additional resources held by other processes Deadlock Characterization 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, …, 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, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. 6
  • 7.
    RESOURCE ALLOCATION GRAPH Deadlockcan be described more precisely in terms of a directed graph called a system 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 Example 7
  • 8.
    Resource-Allocation Graph ✘ Pictorially,we represent ✘ Process ✘ Resource Type with 4 instances ✘ Pi requests instance of Rj ✘ Pi is holding an instance of Rj 8 Pi Pi
  • 9.
    Example of aResource Allocation Graph • The sets P,R,E: o P={p1,p2,p3} o R={R1,R2,R3,R4} o E={P1->R1,P2->R3,R1->P2, R2->P2,R2->P1,R3->P3} 9
  • 10.
    Resource Allocation Graphwith Deadlock • Suppose that process P3 request an instance of resource type R3. • Since no resource is currently available, a request edge P3->R2 is added to the graph. • At this point two cycles exist in the system: ✘ P1->R1->P2->R3->P3->R2->P1 ✘ P2->R3->P3->R2->P2 • Processes P1,P2,P3 are deadlocked. 10
  • 11.
    Graph with aCycle but No Deadlock • In this example we also have a cycle ✘ P1->R1->P3->R2->P1 • However, there is no cycle. • Observe that process P4 may release its resource R2.That resource can then be allocated to P3,breaking the cycle. 11
  • 12.
    ✘ If graphcontains 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 12
  • 13.
    DEADLOCK PREVENTION ✘ Byensuring that at least one of these conditions cannot hold, We can prevent the occurrence of a deadlock 13
  • 14.
    Eliminate Mutual Exclusion– not required for sharable resources, must hold for non-sharable resources. For example, a printer cannot be shared by several processes. Read only files are good example of sharable resource Eliminate Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Deadlock Prevention ✗ Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization. ✗ For example, if a process requires printer at a later time and we have allocated printer before the start of its execution printer will remain blocked till it has completed its execution. ✗ The process will make a new request for resources after releasing the current set of resources. This solution may lead to starvation. 14
  • 15.
    Eliminate 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. Deadlock Prevention ✘ Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. 15
  • 16.
    Deadlock Prevention Eliminate CircularWait – • Each resource will be assigned with a numerical number. A process can request the resources increasing/decreasing order of numbering. •For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted. 16
  • 17.
    A picture isworth a thousand words 17
  • 18.
    Safe State!When aprocess 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. 18
  • 19.
    19 Safe, Unsafe andDeadlock State Place your screenshot here
  • 20.
    Recovery from Deadlock 20 ProcessTermination Resource Preemption
  • 21.
    Process Termination Abort alldeadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort? 21
  • 22.
    Resource Preemption Selecting avictim Starvation – same process may always be picked as victim, include number of rollback in cost factor Rollback – return to some safe state, restart process for that state. 22
  • 23.