2. Deadlock Problem
• A set of blocked processes each holding a resource and waiting to acquire a resource held by
another process in the set.
• A process requests for resources that may not be readily available at the time of the request. In
such a case the process goes into a wait state.
• It may so happen that this process may never change state because the requested resources are
held by other processes which themselves are waiting for additional resources and hence in a wait
state.
• Deadlock occurs when we have a set of processes [not necessarily all the processes in the
system], each holding some resources, each requesting some resources, and none of them is able
to obtain what it needs, i.e. to make progress.
• Resources can be preemptable or non-preemptable
3. System Model
• Resource types R1, 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
4. Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
• Mutual exclusion: At least one of the resources is non-sharable, 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.
5. Resource-Allocation Graph
• Deadlocks can be described by a resource allocation graph.
• The resource allocation graph is a directed graph consisting of vertices and directed edges.
• The vertex set is partitioned into two types, a subset representing processes and another subset representing
resources.
• A directed edge from a process to a resource (Pi Rj) signifies a request from a process Pi for an instance
of the resource Rj and Pi is waiting for Rj.
• A directed edge from a resource to a process (Rj Pi) indicates that an instance of the resource Rj has been
allotted to process Pi.
• A request edge is introduced into the graph when a process requests for a resource.
• This edge is converted into an assignment edge when the resource is granted. When the process releases
the resource, the assignment edge is deleted.
6. 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
7. Resource-Allocation Graph (Cont.)
• Process
• Resource Type with 4 instances
• Pi requests instance of Rj
• Pi is holding an instance of Rj
Pi
Pi
Rj
Rj
8. • If a resource allocation graph has no cycles
(a closed loop in the direction of the
edges), then the system is not in a state of
deadlock.
• If on the other hand, there are cycles, then
a deadlock may exist.
• Here two cycles exist:
• P1-> R1 -> P2 -> R3 -> P3 -> R2-> P1
• P2 ->R3 ->P3 ->R2 ->P2
9.
10. 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.
11. How Deadlock happen
• During file request, when a process request to a file, that currently being held
by another
• During device allocation, when a process attempts to get a device, but being
blocked by another process.
12. Methods for Handling Deadlocks
• We can use a protocol to prevent or avoid deadlock ensuring that the system
will never enter a deadlock state.
• We can Allow the system to enter a deadlock state, detect it and then recover.
• We can Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.
13. Deadlock Prevention
• If any one of the above four conditions does not hold, then deadlocks will not occur.
Thus prevention of deadlock is possible by ensuring that at least one of the four
conditions cannot hold.
• Mutual Exclusion – not required for sharable resources; must hold for nonsharable
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.
14. • 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.
15. Deadlock Avoidance
• 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.
Requires that the system has some additional a priori information available.
16. Safe State
• When a process requests an available resource, system must decide if immediate allocation leaves the system
in a safe state.
• A system is said to be in a safe state if it can allocate resources upto the maximum available and is not in a
state of deadlock.
• A safe sequence of processes always ensures a safe state.
• A sequence of processes < P1, P2, ....., Pn > is safe for the current allocation of resources to processes.
• A safe state is not a deadlock state. Conversely a deadlock state is an unsafe state. But all
unsafe states are not deadlock states.
• If a system is in a safe state it can stay away from an unsafe state and thus avoid deadlock. On
the other hand, if a system is in an unsafe state, deadlocks cannot be avoided
18. 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.
19. Illustration
• A system has 12 instances of a resource type and 3 processes using these resources.
• The maximum requirements for the resource by the processes and their current allocation at an
instance say t0 is as shown below:
• At the instant t0, the system is in a safe state
and one safe sequence is < P1, P0, P2 >.
• This is true because of the following facts:
• Out of 12 instances of the resource, 9 are currently
allocated and 3 are free.
20. At time t0 system is in a safe state
Process
has max
P0 5 10
P1 2 4
P2 2 9
Process
has max
P0 5 10
P1 4 4
P2 2 9
Process
has max
P0 5 10
P1 0 -
P2 2 9
Process
has max
P0 10 10
P1 0 -
P2 2 9
Free 3 Free 1. Here, P1 need only 2 & it
immediately be allocated and return
then
Free 5, P1 terminates
Free 0. Here, P0 can get all its tape
and return
Process
has max
P0 0 -
P1 0 -
P2 2 9
Process
has max
P0 0 -
P1 0 -
P2 9 9
Free 10, P0 terminates
Free 3. Here, P2 can get all 7 and
return then P2 terminates
Thus, <P1, P0, P2 > is a Safe Sequence.
21. Suppose at time t1 P2 request one more instance of tape drive and allocation is
made
• Here now we are stuck. We only have 4 whereas P0 need 5 & P2 need 6.
• Since both can't be granted resources, they want. Thus result is deadlock.
• At time t0 is Safe State & t1 is unsafe state.
• Here the mistake was to grant the request from Process P2 for one more drive.
Process
has max
P0 5 10
P1 2 4
P2 3 9
Process
has max
P0 5 10
P1 4 4
P2 3 9
Process
has max
P0 5 10
P1 0 -
P2 3 9
Free 2
Free 0. Here, P1 need only 2 & it
immediately be allocated and return
Free 4, P1 terminates
22. Resource-Allocation Graph Algorithm
• Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line.
• Claim edge converts to request edge when a process requests a resource.
• Request edge converted to an assignment edge when the resource is allocated to the process.
• When a resource is released by a process, assignment edge reconverts to a claim edge.
• Resources must be claimed a priori in the system.
• Suppose that process Pi requests a resource Rj
• The request can be granted only if converting the request edge to an assignment edge does not
result in the formation of a cycle in the resource allocation graph
25. Banker’s Algorithm
• Multiple instances.
• 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.
26. Data Structures for the Banker’s Algorithm
• Available: Vector of length m. If available [j] = k, there are k instances of resource
type Rj available.
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances
of resource type 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].
Let n = number of processes, and m = number of resources types.
27. Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
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.
28. 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 – Request;
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
29. Example of Banker’s Algorithm
• 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances).
• Snapshot at time T0:
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
30. • From above, Allocation is A=7, B=2, C=5
• Available =Total Resource Type-Allocation Work Matrix(3,3,2) (10-7, 5-2, 7-5)
• The content of Matrix Need is defined to be (Max – Allocation).
• Steps are as follows:
• Step1: P0 need 7,4,3 & Available is 3,3,2
• Here Need < Available which is not executed So Finish Flag is False (F)
• P0- Finish [0]= F
• Step2: P1 need 1,2,2 & Available is 3,3,2
• Here Need < Available which is executed, we update
• Work[i] Work = Work + Allocation 3,3,2 + 2,0,0=5,3,2
• P1-Finish[1]=T
31. • Step3: P2 need 6,0,0 & Available is 5,3,2
• Here Need < Available which is not executed
• P2-Finish[2]=F
• Step4: P3 need 0,1,1 & Available is 5,3,2
• Here Need < Available which is executed, we update
• Work[i] Work = Work + Allocation 5,3,2 + 2,1,1=7,4,3
• P3-Finish[3]=T
• Step5: P4 need 4,3,0 & Available is 7,4,3
• Here Need < Available which is executed, we update
• Work[i] Work = Work + Allocation 7,4,3 + 0,0,2=7,4,5
• P4-Finish[4]=T
32. • Step6: P0 need 7,4,3 & Available is 7,4,5
• Here Need < Available which is executed, we update
• Work[i] Work = Work + Allocation 7,4,5 + 0,1,0=7,5,5
• P0-Finish[0]=T
• Step7: P2 need 6,0,0 & Available is 7,5,5
• Here Need < Available which is executed, we update
• Work[i] Work = Work + Allocation 7,5,5 + 3,0,2=10,5,7 (Equal to resource Type)
• P2-Finish[2]=T
• Here We claim that the system is currently safe state.
• Indeed the sequence <P1,P3,P4,P0,P2> satisfies the safety criteria.
34. Single Instance of Each Resource Type
• Maintain wait-for graph
• Nodes are processes.
• Pi Pj if Pi is waiting for Pj.
• Use a variant of the resource-allocation graph, called a wait-for graph (remove the resources from
the usual graph and collapse edges)
• The wait-for graph is a directed graph having vertices and edges.
• Pi Pj if Pi is waiting for Pj to release a resource that Pi needs.
• An edge Pi Pj exists in a wait-for graph if and only if the corresponding resource allocated
graph contains two edge Pi Rq and Rq Pj for some resource Rq
• Two edges Pi Rq and Rq Pj in the resource allocation graph are replaced by one edge Pi
Pj in the wait-for graph.
36. Recovery from Deadlock
• Once a deadlock has been detected, it must be broken.
• Breaking a deadlock may be manual by the operator when informed of the deadlock or
automatically by the system.
• There exist two options for breaking deadlocks:
• 1. abort one or more processes to break the circular-wait condition causing deadlock (Process
Termination)
• 2. preempting resources from one or more processes which are deadlocked (Resource
Preemption)
37. • Prevention, avoidance and detection are the three basic approaches to handle
deadlocks.
• But they do not encompass all the problems encountered.
• Thus a combined approach of all the three basic approaches is used.