Chapter-5
Deadlock
BY: MADHURI VAGHASIA
Deadlock
EXAMPLES:
1. "It takes money to make money".
2. You can't get a job without experience; you can't get experience without a job.
BACKGROUND:
◦ The cause of deadlocks: Each process needing what another process has. This results from
sharing resources such as memory, devices, links.
◦ Under normal operation, a resource allocations proceed like this::
1. Request a resource (suspend until available if necessary ).
2. Use the resource.
3. Release the resource
What is Deadlock?
for example,
◦ Two processes each want to record a scanned document on a CD.
◦ Process A requests permission to use the scanner and is granted it.
◦ Process B is programmed differently and requests the CD recorder first and is
also granted it.
◦ Now A asks for the CD recorder, but the request is denied until B releases it.
◦ Unfortunately, instead of releasing the CD recorder B asks for the scanner.
◦ At this point both processes are blocked and will remain so forever.
This situation is called a deadlock.
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.
Deadlock
◦ Space is available for allocation of 200Kbytes, and the following sequence of events occur
◦ Deadlock occurs if both processes progress to their second request
P1
. . .
Request 80 Kbytes;
…
Request 60 Kbytes;
P2
. . .
Request 70 Kbytes;
…
Request 80 Kbytes;
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion: only one process at a time can use a resource
2. Hold and wait: a process holding at least one resource is waiting to acquire additional
resources held by other processes
3. No preemption: a resource can be released only voluntarily by the process holding it, after
that process has completed its task
4. Circular wait: there exists a set {P0, P1, …, Pn} 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 Pn is waiting for a resource that is held by P0.
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 multi-set consisting of all resource types
in the system.
◦ request edge – directed edge Pi  Rj
◦ assignment edge – directed edge Rj  Pi
Resource-Allocation Graph(RAG)
◦ Process
◦ Resource Type with 4 instances
◦ Pi requests instance of Rj
◦ Pi is holding an instance of Rj
Pi
Pi
Rj
Example of a Resource Allocation
Graph
Req
Hold
hold
hold
Req
hold
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
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.
The Conditions for Deadlock
1. Mutual exclusion. Only one process may use a resource at a time. No process
may access a resource unit that has been allocated to another process.
2. Hold and wait. A process may hold allocated resources while awaiting assignment of other
resources.
3. No preemption. No resource can be forcibly removed from a process holding it.
The first three conditions are necessary but not sufficient for a deadlock to exist. For deadlock to
actually take place, a fourth condition is required:
4. Circular wait. A closed chain of processes exists, such that each process holds
at least one resource needed by the next process in the chain .
The Conditions for Deadlock (Cont…)
Possibility of Deadlock
1. Mutual exclusion
2. No preemption
3. Hold & Wait
Existence of Deadlock
1. Mutual exclusion
2. No preemption
3. Hold & Wait
4. Circular wait
HOW TO HANDLE DEADLOCKS – GENERAL
STRATEGIES
Methods for Handling Deadlocks:
1. Ignore Deadlocks: Most Operating systems do this!!
Ensure deadlock never occurs using either(Before)
2. Prevention Prevent any one of the 4 conditions from happening.
3. Avoidance Allow all deadlock conditions, but calculate cycles about to happen and stop
dangerous operations..
Allow deadlock to happen. This requires using both: (After)
4. Detection Know a deadlock has occurred.
5. Recovery Regain the resources.
Deadlock Prevention
Elimination of “Mutual Exclusion” Condition:
◦ Some resources, such as the tap drive and printer, are inherently non-
shareable. Note that shareable resources like read-only-file do not
require mutually exclusive access and thus cannot be involved in
deadlock.
Elimination of “Hold and Wait” Condition– 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.
◦ Cons: Lower resource utilization
Restrain the ways request can be made.
Deadlock Prevention (Cont.)
Elimination of “No-preemption” Condition - 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.
Elimination of “Circular Wait” Condition– impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of enumeration.
◦ Then resource Ri precedes Rj in the ordering if i < j. Now suppose that two processes, A and B, are
deadlocked because A has acquired Ri and requested Rj, and B has acquired Rj and requested Ri. This
condition is impossible because it implies i < j and j < i.
Deadlock Avoidance
If we have prior knowledge of how resources will be requested, it's possible to determine if we
are entering an "unsafe" state.
Possible states are:
1. Deadlock No forward progress can be made.
2. Unsafe state A state that may allow deadlock.
3. Safe state A state is safe if a sequence of processes exist such that there are
enough resources for the first to finish, and as each finishes and releases its
resources there are enough for the next to finish.
The rule is simple: If a request allocation would cause an
unsafe state, do not honor that request.
NOTE: All deadlocks are unsafe, but all unsafe are NOT deadlocks.
NOTE: All deadlocks are unsafe, but all unsafe are NOT deadlocks.
SAFE
DEADLOCK
UNSAFE
Only with luck will
processes avoid
deadlock.
O.S. can avoid
deadlock.
Deadlock Avoidance
Let's assume a very simple model: each process declares its maximum needs. In
this case, algorithms exist that will ensure that no unsafe state is reached.
Maximum needs does NOT mean it must use that many resources – simply that it
might do so under some circumstances.
EXAMPLE:
There exists a total of 12 resources. Each resource is used exclusively by a process.
The current state looks like this:
In this example, < p1, p2, p0 >
is a workable sequence.
Suppose p2 requests and is
given one more resource.
What happens then?
Process Max Needs Allocated Current
Needs
P0 10 5 5
P1 4 2 2
P2 7 3 4
There are multiple instances of the resource
in these examples.
Deadlock Avoidance( Banker’s algo)
Banker’s Algorithm(Safty Algo)
1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2. Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3. Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4. if Finish [i] = true for all i
then the system is in a safe state
Deadlock Detection
◦ Allow system to enter deadlock state
◦ Detection algorithm
◦ Recovery scheme
Deadlock Detection
Maintain wait-for graph
◦ Nodes are processes.
◦ Pi  Pj if Pi is waiting for Pj.
Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a
deadlock.
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.
Deadlock Detection
Detection algorithms depends on 2 factors:
1. How often is a deadlock likely to occurs?
2. How many processes will be affected by deadlock when it
happens?
Deadlock Recovery
So, the deadlock has occurred. Now, how do we get the resources back and gain forward progress?
PROCESS TERMINATION:
 Could delete all the processes in the deadlock -- this is expensive.
 Delete one at a time until deadlock is broken ( time consuming ).
 Select who to terminate based on priority, time executed, time to completion, needs for completion, or
depth of rollback
 In general, it's easier to preempt the resource, than to terminate the process.
RESOURCE PREEMPTION:
 Select a victim - which process and which resource to preempt.
 Rollback to previously defined "safe" state.
 Prevent one process from always being the one which is preempted ( starvation ).

Ch 4 deadlock

  • 1.
  • 2.
    Deadlock EXAMPLES: 1. "It takesmoney to make money". 2. You can't get a job without experience; you can't get experience without a job. BACKGROUND: ◦ The cause of deadlocks: Each process needing what another process has. This results from sharing resources such as memory, devices, links. ◦ Under normal operation, a resource allocations proceed like this:: 1. Request a resource (suspend until available if necessary ). 2. Use the resource. 3. Release the resource
  • 3.
    What is Deadlock? forexample, ◦ Two processes each want to record a scanned document on a CD. ◦ Process A requests permission to use the scanner and is granted it. ◦ Process B is programmed differently and requests the CD recorder first and is also granted it. ◦ Now A asks for the CD recorder, but the request is denied until B releases it. ◦ Unfortunately, instead of releasing the CD recorder B asks for the scanner. ◦ At this point both processes are blocked and will remain so forever. This situation is called a deadlock.
  • 4.
    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.
  • 5.
    Deadlock ◦ Space isavailable for allocation of 200Kbytes, and the following sequence of events occur ◦ Deadlock occurs if both processes progress to their second request P1 . . . Request 80 Kbytes; … Request 60 Kbytes; P2 . . . Request 70 Kbytes; … Request 80 Kbytes;
  • 6.
    Deadlock Characterization Deadlock canarise if four conditions hold simultaneously. 1. Mutual exclusion: only one process at a time can use a resource 2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes 3. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task 4. Circular wait: there exists a set {P0, P1, …, Pn} 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 Pn is waiting for a resource that is held by P0.
  • 7.
    Resource-Allocation Graph ◦A setof 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 multi-set consisting of all resource types in the system. ◦ request edge – directed edge Pi  Rj ◦ assignment edge – directed edge Rj  Pi
  • 8.
    Resource-Allocation Graph(RAG) ◦ Process ◦Resource Type with 4 instances ◦ Pi requests instance of Rj ◦ Pi is holding an instance of Rj Pi Pi Rj
  • 9.
    Example of aResource Allocation Graph Req Hold hold hold Req hold
  • 10.
  • 11.
    Graph With ACycle But No Deadlock
  • 12.
    Basic Facts ◦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.
  • 13.
    The Conditions forDeadlock 1. Mutual exclusion. Only one process may use a resource at a time. No process may access a resource unit that has been allocated to another process. 2. Hold and wait. A process may hold allocated resources while awaiting assignment of other resources. 3. No preemption. No resource can be forcibly removed from a process holding it. The first three conditions are necessary but not sufficient for a deadlock to exist. For deadlock to actually take place, a fourth condition is required: 4. Circular wait. A closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain .
  • 14.
    The Conditions forDeadlock (Cont…) Possibility of Deadlock 1. Mutual exclusion 2. No preemption 3. Hold & Wait Existence of Deadlock 1. Mutual exclusion 2. No preemption 3. Hold & Wait 4. Circular wait
  • 15.
    HOW TO HANDLEDEADLOCKS – GENERAL STRATEGIES Methods for Handling Deadlocks: 1. Ignore Deadlocks: Most Operating systems do this!! Ensure deadlock never occurs using either(Before) 2. Prevention Prevent any one of the 4 conditions from happening. 3. Avoidance Allow all deadlock conditions, but calculate cycles about to happen and stop dangerous operations.. Allow deadlock to happen. This requires using both: (After) 4. Detection Know a deadlock has occurred. 5. Recovery Regain the resources.
  • 16.
    Deadlock Prevention Elimination of“Mutual Exclusion” Condition: ◦ Some resources, such as the tap drive and printer, are inherently non- shareable. Note that shareable resources like read-only-file do not require mutually exclusive access and thus cannot be involved in deadlock. Elimination of “Hold and Wait” Condition– 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. ◦ Cons: Lower resource utilization Restrain the ways request can be made.
  • 17.
    Deadlock Prevention (Cont.) Eliminationof “No-preemption” Condition - 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. Elimination of “Circular Wait” Condition– impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. ◦ Then resource Ri precedes Rj in the ordering if i < j. Now suppose that two processes, A and B, are deadlocked because A has acquired Ri and requested Rj, and B has acquired Rj and requested Ri. This condition is impossible because it implies i < j and j < i.
  • 18.
    Deadlock Avoidance If wehave prior knowledge of how resources will be requested, it's possible to determine if we are entering an "unsafe" state. Possible states are: 1. Deadlock No forward progress can be made. 2. Unsafe state A state that may allow deadlock. 3. Safe state A state is safe if a sequence of processes exist such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish. The rule is simple: If a request allocation would cause an unsafe state, do not honor that request. NOTE: All deadlocks are unsafe, but all unsafe are NOT deadlocks.
  • 19.
    NOTE: All deadlocksare unsafe, but all unsafe are NOT deadlocks. SAFE DEADLOCK UNSAFE Only with luck will processes avoid deadlock. O.S. can avoid deadlock. Deadlock Avoidance
  • 20.
    Let's assume avery simple model: each process declares its maximum needs. In this case, algorithms exist that will ensure that no unsafe state is reached. Maximum needs does NOT mean it must use that many resources – simply that it might do so under some circumstances. EXAMPLE: There exists a total of 12 resources. Each resource is used exclusively by a process. The current state looks like this: In this example, < p1, p2, p0 > is a workable sequence. Suppose p2 requests and is given one more resource. What happens then? Process Max Needs Allocated Current Needs P0 10 5 5 P1 4 2 2 P2 7 3 4 There are multiple instances of the resource in these examples. Deadlock Avoidance( Banker’s algo)
  • 21.
    Banker’s Algorithm(Safty Algo) 1.Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish[i] = false; for i=1, 2, 3, 4….n 2. Find an i such that both a) Finish[i] = false b) Needi <= Work if no such i exists goto step (4) 3. Work = Work + Allocation[i] Finish[i] = true goto step (2) 4. if Finish [i] = true for all i then the system is in a safe state
  • 22.
    Deadlock Detection ◦ Allowsystem to enter deadlock state ◦ Detection algorithm ◦ Recovery scheme
  • 23.
    Deadlock Detection Maintain wait-forgraph ◦ Nodes are processes. ◦ Pi  Pj if Pi is waiting for Pj. Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. 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.
  • 24.
    Deadlock Detection Detection algorithmsdepends on 2 factors: 1. How often is a deadlock likely to occurs? 2. How many processes will be affected by deadlock when it happens?
  • 25.
    Deadlock Recovery So, thedeadlock has occurred. Now, how do we get the resources back and gain forward progress? PROCESS TERMINATION:  Could delete all the processes in the deadlock -- this is expensive.  Delete one at a time until deadlock is broken ( time consuming ).  Select who to terminate based on priority, time executed, time to completion, needs for completion, or depth of rollback  In general, it's easier to preempt the resource, than to terminate the process. RESOURCE PREEMPTION:  Select a victim - which process and which resource to preempt.  Rollback to previously defined "safe" state.  Prevent one process from always being the one which is preempted ( starvation ).