OS_Chapter_6_Deadlock[1]
OS_Chapter_6_Deadlock[1]
Deadlock
The Deadlock Problem
✓ A deadlock is a situation where a process or a set of processes is blocked, waiting for some
✓ A set of blocked processes each holding a resource and waiting to acquire a resource held by
✓ If each process now requests another drive, the two processes will be in a
deadlocked state.
Bridge Crossing Example
✓ If a deadlock occurs, it can be resolved if one car backs up (preempt resources and
rollback).
the set.
✓ A system consists of a finite number of resources to be distributed among a number of competing processes.
➢ Resource types R1, R2, . . ., Rm (like CPU cycles, memory space, printers or any other i/o devices, etc…)
➢ The resources may be either physical (printers, tape drives, memory space, and CPU cycles) or logical (files and
semaphores).Semaphore: is a variable or abstract data type that is used for controlling access by
multiple processes to a common resource in a concurrent system such as a multiprogramming
operating system.
Con’t…
➢ A process must request a resource before using it, and must release the resource after
using it.
1. Request: If the request cannot be granted immediately, then the requesting process must
wait until it can acquire the resource.
2. Use: The process can operate on the resource (example, print on printer).
(Examples: request and release device, open and close file, and
allocate and free memory system calls).
Hold and wait- A process must be simultaneously holding at least one resource and
waiting for at least one resource that is currently being held by some other process.
No preemption- Resources cannot be preempted; that is a resource can be released only
voluntarily by the process holding it, after that process has completed its task.
Circular Wait- There must be a circular chain of two or more processes, each of which
is waiting for a resource held by the next member of the chain.
Resource-Allocation Graph
✓ A deadlock is described in terms of a direct graph called a system RAG
✓ 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 Pi → Rj
✓ Assignment edge – directed edge Rj → Pi
Con’t…
• Process
• Pi requests instance of Rj Pi
• Pi is holding an instance of Rj
Pi
Example of a Resource Allocation Graph
Process State:
✓ P1 is holding an instance of R2 and waiting for an
instance of R1.
✓ P2 is holding an instance of R1 and R2, and is
✓ P2→R3→P3→R2→P2
✓ We have a cycle:
P1→R1→P3→R2→P1
✓P4 may release its instance of R2 and
➢If a resource category contains more than one instance, then the
presence of a cycle in the resource-allocation graph indicates
the possibility of a deadlock, but does not guarantee one.
Methods for Handling Deadlocks
Generally speaking we can deal with the deadlock problem in one of
four ways:
• Just ignore the problem. Maybe if you ignore it, it will ignore you.
• Detection and recovery. Let deadlocks occur, detect them, and take action.
4.4.1. Deadlock Prevention
• Protocol 1: request all tape, disk, printer and hold for entire
execution; note printer will be idle for a long time.
Cont…
• Deadlock arises due to the fact that a process can't be stopped once it
starts.
✓This ensures that not a single process can request a resource which is being utilized
by some other process and no cycle will be formed.
✓To ensure that the circular-wait condition never holds is to determine a total
ordering of all resource types, and to require that each process requests resources
in an increasing order of enumeration.
✓ Impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration.
Con’t…
Example: Let R={R1, R2, …, Rm} be the set of resource types.
✓Assign to each resource type a unique integer number to compare two resources
and to determine whether one proceeds another in ordering.
✓ For example: If the set of resource types R includes tape drives, disk drives, and
printers, then a function F might be defined as follows:
• Among all the methods, violating Circular wait is the only approach that can be
implemented practically.
4.4.2. Deadlock Avoidance
✓ It is to prevent deadlocks from ever happening, by preventing at least one of the aforementioned conditions.
✓ Requires that the system has some additional a priori information about how resources are to be requested.
✓ 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.
Cont…
• The main difference between deadlock prevention and deadlock
avoidance is that the deadlock prevention ensures that at least one of
the necessary conditions to cause a deadlock will never occur, while
deadlock avoidance ensures that the system will not enter an unsafe
state.
Safe State
✓A state is safe if the system can allocate all resources requested by all
..., PN }
✓Sequence of all processes <P1, P2, …, Pn> is safe if for each Pi, the resources
unsafe state.
✓ Claim edge Pi ----->Rj indicated that process Pi may request resource Rj for future (represented by a dashed
line)
✓ Request edge converted to an assignment edge when the resource is allocated to the process
✓ Figure 2 shows an unsafe state in a resource-allocation graph, because there is a cycle in the graph.
✓ The resource-allocation graph algorithm is not applicable to a resource allocation system with
multiple instances of each resource type.
2. Banker’s Algorithm
✓ Banker’s algorithm is applicable to a resource-allocation system with multiple instances
of each type.
✓ From it’s name, it could be used in a banking system to ensure that the bank never allocates
its available cash such that it can no longer satisfy the needs of all customers.(banks never
run out of the resources or money and always been in a safe state).
Let “n” be the number of processes and “m” be the number of resources types. So we needs the
following DS to implement Bankers algorithm.
Available: Vector of length “m” indicates the number of available resources of each type.
If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.
Con’t….
Allocation: An (n*m) matrix defines the number of resources of each type currently allocated to each process.
Need: An (n*m) matrix indicates the remaining resource need of each process.
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].
Safety Algorithm
Step-1: Let Work and Finish be vectors of length m and n, respectively.
Initialize: Work = Available and
Step-1: If Requesti Needi, go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.
Step-2: If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.
Step-3: Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available - Requesti;
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
Example of Banker’s Algorithm
✓ 5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances, and C (7 instances).
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
✓ An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process 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-allocation graph
contains two edges Pi Rq and Rq Pj for some resource Rq.
✓ A deadlock exists in the system if and only if the wait-for graph contains a cycle.
Con’t…
✓ To detect deadlocks, the system needs to:-
– Maintain the wait-for graph.
– Periodically invoke an algorithm that searches for a cycle in the
graph.
Note: “n” is number of processes in the system and “m” is the number of resource types.
Available: A vector of length “m” indicates the number of available resources of each type.
Allocation: An “n*m” matrix defines the number of resources of each type currently allocated to
each process.
Request: An “n*m” matrix indicates the current request of each process. If Request [i,j] = k, then
process Pi is requesting k more instances of resource type Rj.
Detection Algorithm
Step-1: Let Work and Finish be vectors of length m and n, respectively Initialize:
Work = Available, For i = 1,2, …, n, if Allocation i 0, then ,Finish[i] = false;
otherwise, Finish[i] = true.
Step-4: If Finish[i] = false, for some i, 1 i n, then the system is in deadlock state. Moreover, if
Finish[i] = false, then Pi is deadlocked.
✓ If deadlocks occur frequently, then the detection algorithm should be invoked frequently.
✓ Resources allocated to deadlocked processes will be idle until the deadlock can be broken.
✓ In addition, the number of processes involved in the deadlock cycle may grow.
✓ Deadlocks occur only when some process makes a request that cannot be granted immediately.
4.4.4. Recovery from Deadlock
✓ When a detection algorithm determines that a deadlock exists, several alternatives are available.
✓ One possibility is to inform the operator that a deadlock has occurred and to let the operator deal
with the deadlock manually.
✓ Another possibility is to let the system recover from the deadlock automatically.
– To preempt some resources from one or more of deadlock processes (Resource Preemption).
Recovery from Deadlock: Process Termination
• Two methods to eliminate deadlocks by aborting a process. In both methods, the system reclaims
all resources allocated to the terminated processes:
A. Abort all deadlocked processes: Clearly will break the deadlock cycle, but at great expense.
✓ The deadlocked processes may have computed for a long time, and the results of these partial
computations must be discarded and probably will have to be recomputed later.
B. Abort one process at a time until the deadlock cycle is eliminated:
• Aborting a process may not be easy. If the process was in the midst of
updating a file, terminating it will leave that file in an incorrect state.
Recovery from Deadlock: Resource Preemption
✓ If preemption is required to deal with deadlocks, then three issues need to be addressed:
Selecting a victim: which resources and which processes are to be preempted? (minimize cost)
Rollback: If we preempt a resource from a process, what should be done with that process? We must roll back
the process to some safe state, and restart it from that state.
Starvation: Same process may always be picked as victim, include number of rollback in cost factor.
Combined Approach to Deadlock Handling
✓ Combine the three basic approaches
– prevention
– avoidance
– detection
allowing the use of the optimal approach for each of resources in the system.
✓ Use most appropriate technique for handling deadlocks within each class.
Chapter 7