A deadlock occurs when a set of processes are blocked because each process is holding a resource and waiting for another resource held by another process in the set, creating a circular wait. Four necessary conditions must be met for a deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be handled through deadlock avoidance, detection and recovery, or prevention. Avoidance methods like Banker's Algorithm use safe state models to prevent deadlocks, while detection algorithms use resource allocation graphs to detect deadlocks. Once detected, deadlocks are recovered through process termination or resource preemption.
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
Deadlock
1. Deadlocks
A deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
Mahershi Bhavsar
160160107005
Operating Systems
2. Necessary Conditions
A deadlock situation can arise if the following conditions hold simultaneously in a system.
• Mutual Exclusion:
• Hold and Wait:
• No Pre-emption:
• Circular Wait:
At least one resource must be held in a non sharable mode, that is, only one process at
a time can access the resource.
A process must be holding at least one resource while waiting to acquire additional
resource.
Resources cannot be pre-empted. A resource can only be released voluntarily by the
process holding it.
A set of waiting processes must exist such that each process is waiting for a resource
already acquired by some other process in the set.
If all the four conditions holds true, deadlock occurs.
3. Resource Allocation Graph
A resource allocation graph tracks which resource is hold by what process and which process is
waiting for a resource of a particular type. It is a very powerful and simple tool to illustrate how a set of
processes can deadlock.
• If a process is using a resource, an arrow is drawn from the resource
node to the process node.
• If a process is requesting a resource, an arrow is drawn from the process
node to resource node.
Resource Allocation Graph
4. If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one
instance, then the process will deadlock.
In the given example, a deadlock occurs.
Process 1 waits for a resource already held by Process 3 and
Process 3 waits for a resource held by Process 2. Further,
Process 2 is waiting for a resource held by Process 1.
In the given example, only one instance of each resource is
considered. In real life there may be more than one instances
and the situation completely differs.
5. Methods for Handling Deadlocks
Deadlocks can be dealt with by one of the three ways stated below:
1. Using a protocol to ensure never entering a deadlock i.e. deadlock avoidance or prevention.
2. Detect and recover from a deadlocked situation.
3. Pretension. Pretend that deadlocks never occur in the system. (More like faith in God)
6. Deadlock Prevention
Deadlock can be prevented by preventing any one of the four necessary conditions for deadlock from
holding true.
1. Mutual Exclusion:
2. Hold and Wait:
It is not possible to prevent mutual exclusion because some resource needs to be non –
sharable, such as printer.
To ensure this condition doesn’t hold true, we must make sure a process doesn’t hold a
resource while requesting another. All the required resources must be requested and
allocated before the execution of the process begins. This also leads to wastage of
resources that remain idle until used but yet already allocated. Another protocol allows a
process to request resources only when it has none. Before a process requests
additional resources, it must release all currently allocated resources.
7. 3. No Pre-emption:
4. Circular Wait:
To prevent this condition, if a process is holding some resource and requests additional
resources that cannot be immediately delivered, then all the resources being currently
held are pre-empted. These pre-empted resources are added to the list of resources
the process is waiting for. This protocol is often applied to resources whose state can
be easily saved and restored later.
Each resource will be assigned a number. A process can request resources only in the
increasing order or numbering. This ensures no request leads to a loop in the resource
allocation graph.
8. Deadlock Avoidance
Deadlock prevention is an efficient way to prevent deadlock but not efficient utilization of resources. It
leads to low device utilization and reduced system throughput.
An alternative method is to avoid deadlocks by having additional information of resources requested
and allocated. The simplest and most useful model requires each process to state maximum number
of resources of each type will it require.
Safe State:
A state of system is safe if it can allocate resource to each process in a certain order and still prevent
a deadlock.
Banker’s Algorithm is used to avoid deadlock. It confirms the system to be in safe system if the
requested resources are allocated.
9. Banker’s Algorithm
• An algorithm to avoid deadlocks
• When a process enters the system, it must declare the maximum instances of each type of
resource it may need. This number may not exceed the total number of resources of a particular
type in the system.
• Several data structures are maintained to implement this algorithm.
1. Available:
2. Max:
A vector of length m indicates the number of available resources. (A vector is a container type
in programming languages, it basically acts as dynamic array).
A v a i l a b l e [ j ] = k
k instances of resource type Rj are available.
A matrix (n x m) that defines maximum demand of each process.
M a x [ i ] [ j ] = k
Process Pi may request at most k instances of Rj
10. 3. Allocation:
4. Max:
A matrix (n x m) that defines the number of allocation of each type of resource currently
allocated to a process.
A l l o c a t i o n [ i ] [ j ] = k
k instances of Rj are allocated to Pi
A matrix (n x m) that defines remaining resource need of each process.
N e e d [ i ] [ j ] = k
Pi may need k instances if Rj
11. Request Resource Algorithm:
1. If R e q u e s t i ≤ N e e d i , go to step 2. Otherwise, process exceeded its maximum claim.
2. If R e q u e s t i ≤ A v a i l a b l e i , go to step 3. Otherwise, Pi must wait, since resources are not available.
3. Pretend to allocate resource to check for resulting state.
A v a i l a b l e = A v a i l a b l e – R e q u e s t i
A l l o c a t i o n i = A l l o c a t i o n i + R e q u e s t
N e e d i = N e e d i – R e q u e s t i
If the resulting resource state is safe, the transaction is completed.
12. Process Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Illustration of Banker’s Algorithm
Let the system currently be in the state shown. Suppose now P1 requests one additional instance of resource A
and two additional instances of resource C, so R e q u e s t i = ( 1 , 0 , 2 ) .
We first check that R e q u e s t i ≤ A v a i l a b l e , that is ( 1 , 0 , 2 ) ≤ ( 3 , 3 , 2 ) which is true. We then pretend to
allocate the requested resources leading to a new state as follows
13. The obtained state is safe, hence we can immediately grant the newly requested resources to the process.
However a further request of (3,3,0) by P4 cannot be granted, since the resources are not available.
Safe State is determined by viewing all positive values in Available column.
Process Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
14. Deadlock Detection
If a system does not provide deadlock avoidance or deadlock prevention, it must provide
• An algorithm to examine the state of the system, deadlock detection.
• An algorithm to recover from deadlock.
Deadlock can be easily detected using the resource allocation graph. If a single instance of resources are present,
deadlock can be detected by a presence of a cycle in the resource allocation graph.
For a system with several instances of a resource type, this method is not sufficient. An algorithm is designed to
detect deadlock in system with multiple instances of resources of a type.
The algorithm is similar to Banker’s Algorithm. It uses several data structures such as Available, Allocation and
Request.
15. The working of algorithm is shown below.
1. Let Work and Finish be vectors of length m and n respectively.
Work = Available. (initialization)
if A llocation i ≠ 0 , F in ish [i] = false
E lse, F in ish [i] = tru e
2. Find an index i such that
Finish[i] == false
Req u est i ≤ Work
if no such I exists, go to step 4.
3 . Work = Work + A llocation i
F in ish [i] = tru e
Go to Step 2.
4. If F in ish [i] == false , then the system is in a deadlocked state.
16. Deadlock Recovery
When a detection algorithm detects a deadlock, deadlock is manually or automatically handled to recover the safe
state of the system. There are two options of breaking a deadlock, abort one or more processes or to pre-empt
some resources.
17. Process Termination
Abort all deadlocked processes:
This method does break the deadlock but with a great expense. This method involves terminating all the processes
involved in the deadlock state.
Abort one process at a time until deadlock cycle is eliminated:
This method is more efficient than terminating all the processes. A deadlock detection algorithm is implemented to
determine which process to be terminated. Processes whose termination incurs minimum cost are terminated.
18. Resource Pre-emption
Successive pre-emption of resources to deal with deadlock. Three issues needs to be addressed in
this method.
1. Selecting a victim:
2. Rollback:
3. Starvation:
Selection of the resource to be pre-empted. Also involves determining the order of pre-
emption to minimize the cost. Cost factor includes various parameters such as number
of resources a deadlocked process holds or the mount of time the process has
consumed so far.
A process whose resource are pre-empted is rolled back up to a safe state from where
it can restart its execution.
Selecting victim based on cost parameters lead to starvation of the most expensive
process as it is always picked as the victim. The solution to prevent starvation is to
include number of roll backs for a process in the cost factor. A process must be chosen
as victim accordingly.