Deadlock
Chapter 4
1
Operating System
2
Deadlocks
• System Model
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
• Combined Approach to Deadlock Handling
3
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: Suppose that process A asks for CD-ROM drive and gets it.
A moment later, process B asks for the flatbed plotter and gets it, too.
Now process A asks for the plotter and blocks waiting for it. Finally
process B asks for CD-ROM drive and also blocks. This situation is
called a deadlock.
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.
4
5
Resources
• A resource is anything that must be acquired, used, and released
over the course of time
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.
• A resource is anything that can only be used by a single process at
any instance (at any amount).
• Each process utilizes a resource as follows:
 request
 use
 release
6
Preemptable and Non-preemptable
Resources
Preemptable resource
 Can be taken away from a process
 Example : Memory
Non-preemptable resource
 Cannot be taken away from its current owner without causing
the computation to fail.
 Example : CD-ROM
• Deadlock involves non-preemptable resources.
7
What is Deadlock?
• A Deadlock is a situation where a group of processes are permanently
blocked as a result of each process having acquired a subset of the
resources needed for its completion and waiting for release of the
remaining resource held by others in the same group.
• Deadlock occurs when two or more processes are each waiting for an
event that will never occur, since it can only be generated by another
process in that set.
• A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
8
Deadlock Characterization
• Deadlock can arise if 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, …, 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.
9
Deadlock Modeling: Resource-Allocation
Graph
• Two kinds of nodes:
 Squares— Represent resources –Instances of the resource are represented
as dots within the box.
 Circles — Represent threads / processes.
• Two kinds of (directed) edges:
 Request edge — from process to resource — indicates the process has
requested the resource, and is waiting to acquire it.
 Assignment edge —from resource-instance to process— indicates the
Process is holding the resource instance.
 When a request is made, a request edge is added.
 When request is fulfilled, the request edge is transformed into an
assignment edge.
 When process releases the resource, the assignment edge is deleted.
10
Resource-Allocation Graph (Cont.)
11
Resource-Allocation Graph (Cont.)
12
Example of a Resource Allocation Graph
13
Resource Allocation Graph With A Deadlock
14
Resource Allocation Graph With A Cycle But
No Deadlock
15
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.
16
Methods for Handling Deadlocks
• Four strategies are used for dealing with deadlocks.
1. Just ignore the problem.
2. Detection and recovery
3. Dynamic avoidance
4. Prevention
17
The Ostrich Algorithm
The simplest approach is the ostrich algorithm: stick your
head in the sand and pretend (assume) there is no problem
18
Deadlock Detection and Recovery
• Allow system to enter deadlock state
• Detection algorithm
• Recovery scheme
19
Deadlock Detection with One Resource of
Each Type
• For such a system, we can construct a resource graph.
• If this graph contains one or more cycles, a deadlock exists.
• Any process that is part of a cycle is deadlocked.
• If no cycles exist, the system is not deadlocked.
20
Deadlock Detection with One Resource of
Each Type
• Example: consider a system with seven processes, A through G, and six
resources, R through W. The state of which resources are currently
owned and which ones are currently being requested is as follows:
1. Process A holds R and wants S.
2. Process B holds nothing but wants T.
3. Process C holds nothing but wants S.
4. Process D holds U and wants S and T.
5. Process E holds T and wants V.
6. Process F holds W and wants S.
7. Process G holds V and wants U.
•Q: Is this system deadlocked? And if so, which processes are involved?
21
Resource-Allocation Graph
22
Recovery from Deadlock
Recovery through Preemption
• Select a victim
• Suspend that process (Mark it as not runnable).
• Take a resource away from the process and give it to another process.
• Restart the suspended process.
• Recovering this way is frequently difficult or impossible.
• Choosing the process to suspend depends largely on which ones have
resources that can easily be taken back.
23
Recovery from Deadlock
Recovery through Rollback
• Having processes check-pointed (a place where actions were stoped)
periodically.
• Check-pointing a process means that its state is written to a file so
that it can be restarted later.
• The checkpoint contains not only the memory image, but also the
resource state, in other words, which resources are currently
assigned to the process.
• To do the recovery, a process that owns a needed resource is rolled
back to a point in time before it acquired that resource by starting at
one of its earlier checkpoints.
24
Recovery from Deadlock
Recovery through Killing Processes
• Abort(end) one process at a time until the deadlock cycle is eliminated.
• If this does not help, it can be repeated until the cycle is broken.
• Alternatively, a process not in the cycle can be chosen as the victim in order
to release its resources.
• In which order should we choose to abort?
 Priority of the process.
 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?
25
Deadlock Avoidance
• Requires that the system has some additional a priori information
available.
 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.
26
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.
27
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.
⇒
28
Safe, unsafe , deadlock state spaces
29
Deadlock Prevention
Restrain the ways request can be made
• Mutual Exclusion – not required for sharable resources; must hold
for non-sharable resources.
• Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources.
 Require process to request and be allocated all its resources
before it begins execution, or allow process to request
resources only when the process has none.
 Low resource utilization; starvation possible.
30
Deadlock Prevention (Cont.)
• 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.
31
Combined Approach to Deadlock Handling
Prevention (Cont.)
• 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.
32
The End

Algorithm 4Chapter Four- Deadlock (5).pptx

  • 1.
  • 2.
    2 Deadlocks • System Model •Deadlock Characterization • Methods for Handling Deadlocks • Deadlock Prevention • Deadlock Avoidance • Deadlock Detection • Recovery from Deadlock • Combined Approach to Deadlock Handling
  • 3.
    3 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: Suppose that process A asks for CD-ROM drive and gets it. A moment later, process B asks for the flatbed plotter and gets it, too. Now process A asks for the plotter and blocks waiting for it. Finally process B asks for CD-ROM drive and also blocks. This situation is called a deadlock.
  • 4.
    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. 4
  • 5.
    5 Resources • A resourceis anything that must be acquired, used, and released over the course of time • Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices • Each resource type Ri has Wi instances. • A resource is anything that can only be used by a single process at any instance (at any amount). • Each process utilizes a resource as follows:  request  use  release
  • 6.
    6 Preemptable and Non-preemptable Resources Preemptableresource  Can be taken away from a process  Example : Memory Non-preemptable resource  Cannot be taken away from its current owner without causing the computation to fail.  Example : CD-ROM • Deadlock involves non-preemptable resources.
  • 7.
    7 What is Deadlock? •A Deadlock is a situation where a group of processes are permanently blocked as a result of each process having acquired a subset of the resources needed for its completion and waiting for release of the remaining resource held by others in the same group. • Deadlock occurs when two or more processes are each waiting for an event that will never occur, since it can only be generated by another process in that set. • A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.
  • 8.
    8 Deadlock Characterization • Deadlockcan arise if 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, …, 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.
  • 9.
    9 Deadlock Modeling: Resource-Allocation Graph •Two kinds of nodes:  Squares— Represent resources –Instances of the resource are represented as dots within the box.  Circles — Represent threads / processes. • Two kinds of (directed) edges:  Request edge — from process to resource — indicates the process has requested the resource, and is waiting to acquire it.  Assignment edge —from resource-instance to process— indicates the Process is holding the resource instance.  When a request is made, a request edge is added.  When request is fulfilled, the request edge is transformed into an assignment edge.  When process releases the resource, the assignment edge is deleted.
  • 10.
  • 11.
  • 12.
    12 Example of aResource Allocation Graph
  • 13.
  • 14.
    14 Resource Allocation GraphWith A Cycle But No Deadlock
  • 15.
    15 Basic Facts • Ifgraph 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.
  • 16.
    16 Methods for HandlingDeadlocks • Four strategies are used for dealing with deadlocks. 1. Just ignore the problem. 2. Detection and recovery 3. Dynamic avoidance 4. Prevention
  • 17.
    17 The Ostrich Algorithm Thesimplest approach is the ostrich algorithm: stick your head in the sand and pretend (assume) there is no problem
  • 18.
    18 Deadlock Detection andRecovery • Allow system to enter deadlock state • Detection algorithm • Recovery scheme
  • 19.
    19 Deadlock Detection withOne Resource of Each Type • For such a system, we can construct a resource graph. • If this graph contains one or more cycles, a deadlock exists. • Any process that is part of a cycle is deadlocked. • If no cycles exist, the system is not deadlocked.
  • 20.
    20 Deadlock Detection withOne Resource of Each Type • Example: consider a system with seven processes, A through G, and six resources, R through W. The state of which resources are currently owned and which ones are currently being requested is as follows: 1. Process A holds R and wants S. 2. Process B holds nothing but wants T. 3. Process C holds nothing but wants S. 4. Process D holds U and wants S and T. 5. Process E holds T and wants V. 6. Process F holds W and wants S. 7. Process G holds V and wants U. •Q: Is this system deadlocked? And if so, which processes are involved?
  • 21.
  • 22.
    22 Recovery from Deadlock Recoverythrough Preemption • Select a victim • Suspend that process (Mark it as not runnable). • Take a resource away from the process and give it to another process. • Restart the suspended process. • Recovering this way is frequently difficult or impossible. • Choosing the process to suspend depends largely on which ones have resources that can easily be taken back.
  • 23.
    23 Recovery from Deadlock Recoverythrough Rollback • Having processes check-pointed (a place where actions were stoped) periodically. • Check-pointing a process means that its state is written to a file so that it can be restarted later. • The checkpoint contains not only the memory image, but also the resource state, in other words, which resources are currently assigned to the process. • To do the recovery, a process that owns a needed resource is rolled back to a point in time before it acquired that resource by starting at one of its earlier checkpoints.
  • 24.
    24 Recovery from Deadlock Recoverythrough Killing Processes • Abort(end) one process at a time until the deadlock cycle is eliminated. • If this does not help, it can be repeated until the cycle is broken. • Alternatively, a process not in the cycle can be chosen as the victim in order to release its resources. • In which order should we choose to abort?  Priority of the process.  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?
  • 25.
    25 Deadlock Avoidance • Requiresthat the system has some additional a priori information available.  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.
  • 26.
    26 Safe State • Whena 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.
  • 27.
    27 Basic Facts • Ifa 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. ⇒
  • 28.
    28 Safe, unsafe ,deadlock state spaces
  • 29.
    29 Deadlock Prevention Restrain theways request can be made • Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.  Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none.  Low resource utilization; starvation possible.
  • 30.
    30 Deadlock Prevention (Cont.) •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.
  • 31.
    31 Combined Approach toDeadlock Handling Prevention (Cont.) • 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.
  • 32.