Unit-3 - OS (Deadlock Avidance, Detection & Recovery)
Unit-3 - OS (Deadlock Avidance, Detection & Recovery)
I
B.Tech CSE/IT 2nd Year/4th Semester
Unit-3
Er Parag Rastogi
Assistant Professor, Department of CSE, RD Engineering College
Deadlock Avoidance
Overview
Deadlock Avoidance is used by Operating System to Avoid Deadlock in the System. The processes need to specify the
maximum resources needed to the Operating System so that the Operating System can simulate the allocation of available
resources to the requesting processes and check if it is possible to satisfy the need of all the processes requirements.
Operating System avoids Deadlock by knowing the maximum resources requirements of the processes initially, and also
Operating System knows the free resources available at that time. Operating System tries to allocate the resources
according to the process requirements and checks if the allocation can lead to a safe state or an unsafe state. If the resource
allocation leads to an unsafe state, then Operating System does not proceed further with the allocation sequence.
Let's understand the working of Deadlock Avoidance with the help of an intuitive example.
Let's consider three processes P1, P2, P3. Some more information on which the processes tells the Operating System are
:
P1 process needs a maximum of 9 resources (Resources can be any software or hardware Resource like tape drive
or printer etc..) to complete its execution. P1 is currently allocated with 5 Resources and needs 4 more to complete
its execution.
P2 process needs a maximum of 5 resources and is currently allocated with 2 resources. So it needs 3 more
resources to complete its execution.
P3 process needs a maximum of 3 resources and is currently allocated with 1 resource. So it needs 2 more
resources to complete its execution.
Operating System knows that only 2 resources out of the total available resources are currently free.
But only 2 resources are free now. Can P1, P2, and P3 satisfy their requirements? Let's try to find out.
As only 2 resources are free for now, then only P3 can satisfy its need for 2 resources. If P3 takes 2 resources and
completes its execution, then P3 can release its 3 (1+2) resources. Now the three free resources which P3 released can
satisfy the need of P2. Now, P2 after taking the three free resources, can complete its execution and then release 5 (2+3)
Er Parag Rastogi
CSE Department
resources. Now five resources are free. P1 can now take 4 out of the 5 free resources and complete its execution. So, with
2 free resources available initially, all the processes were able to complete their execution leading
I
to Safe State. The order
of execution of the processes was <P3, P2, P1>.
What if initially there was only 1 free resource available? None of the processes would be able to complete its execution.
Thus leading to an unsafe state.
We use two words, safe and unsafe states. What are those states? Let's understand these concepts.
Safe State - In the above example, we saw that Operating System was able to satisfy the need of all three processes, P1, P2,
and P3, with their resource requirements. So all the processes were able to complete their execution in a certain order like
P3->P2->P1.
So, if Operating System is able to allocate or satisfy the maximum resource requirements of all the processes in any
order then the system is said to be in Safe State.
Unsafe State - If Operating System is not able to prevent Processes from requesting resources which can also lead to
Deadlock, then the System is said to be in an Unsafe State.
Unsafe State does not necessarily cause deadlock it may or may not causes deadlock.
So, in the above diagram shows the three states of the System. An unsafe state does not always cause Deadlock.
Some unsafe states can lead to Deadlock, as shown in the diagram.
Bankers Algorithm
Bankers algorithm in Operating System is used to avoid deadlock and for resource allocation safely to each process in the
system. As the name suggests, it is mainly used in the banking system to check whether the loan can be sanctioned to a
person or not. Banker’s algorithm in OS is a combination of two main algorithms: safety algorithm (to check whether the
system is in a safe state or not) and resource request algorithm (to check how the system behaves when a process makes a
resource request). It was designed by Edsger Dijkstra.
To understand this algorithm in detail, let us discuss a real-world scenario. Supposing there are 'n' account holders in
a particular bank and the total sum of their money is 'x'. Now, if a person applies for a loan (let's say for buying a car),
then the loan amount subtracted from the total amount available in the bank gives us the remaining amount and that should
be greater than 'x', then only the loan will be sanctioned by the bank. It is done keeping in mind about the worst case where
all the account holders come to withdraw their money from the bank at the same time. Thus, the bank always remains in
a safe state.
Er Parag Rastogi
CSE Department
Banker’s algorithm in OS works similarly. Each process within the system must provide all the important necessary details
to the operating system like upcoming processes, requests for the resources, delays, etc. Based
I
on these details, OS decides
whether to execute the process or keep it in the waiting state to avoid deadlocks in the system. Thus, Bankers algorithm is
sometimes also known as the Deadlock Detection Algorithm.
There are some important data structure terms that are used to implement Bankers' algorithm in OS and they are as follows:
1. Available: Example: Available[i] = k indicates that there are 'k' instances of resource type 'Rj' available within the
system.
2. Max: Example: Max[i][j] = k indicates that process 'Pi' can request for maximum k instances of resource type 'Rj' within
the system.
3. Allocation: Example: Allocation[i][j] = k indicates that currently process ‘Pi’ is allocated ‘k’ instances of resource type
'Rj' within the system.
4. Need: Example: Need [i, j] = k indicates that ‘Pi’ process presently requires ‘k’ instances of resource type ‘Rj’ for its
execution.
5. Finish Example: Finish[i] = true indicates that process Pi has been allocated all the required number of resources by the
system.
While implementing or working with Bankers algorithm in OS, it is quite important to keep a track of three things:
(i) How many resources of each type, a process can request for and it is denoted by the [MAX] array.
(ii) How many resources of each type, a process is currently holding or allocated and it is denoted by
[ALLOCATION] array.
(iii) How many resources of each type are currently available within the system and it is denoted by the
[AVAILABLE] array.
1. Safety Algorithm - Safety algorithm check for the safe state of the system. If system is in safe state with any of
the resource allocation permutation then deadlock can be avoided.
Step-1: Let there be two vectors Work and Finish of length m and n, respectively. Work array represents the total
available resources of each type (R0,R1,R2,...Rm) and Finish array represents if the particular process Pi has
finished execution or not.It can have a boolean value of true/false.
Work = Available
Initially all process are assumed to be unfinished so for all i Finish[i] = false.
Step-2: Need is also an array of size m for each process. We can iterate over each process from i=0,1,2,3... to n
and find if any process Pi exist for which Need[i]<Work[i] and Finish[i]=false.
Er Parag Rastogi
CSE Department
Need[i]<Work[i]
Finish[i]=false , i=0,1,2,3...n I
If no such process exist and finish[i]= false for all process, then the system is not in safe state as there is no such process
which can complete its execution from the available resources. So, the system can go in deadlock state. If Finish[i]=
true for all process then proceed to step 4.
Step-3: If any process exist for which Need<Available as in step2 then allocate those resources to that particular
process and increase the allocated array for that process by Allocated[i].
Work[i]=Work[i]+Allocated[i]
Finish[i]=true
Once this process is executed then proceed to step-2 and repeat for other process until all the process complete there
execution and Finish[i]= true for all i=0,1,2,3...n.
Step-4: If finish[i]=true for all i=0,1,2,3..n then the system is considered to be in safe state.
2. Resource request algorithm - Resource request algorithm checks if the request can be safely granted or not to
the process requesting for resources.
If a process Pi request for c instances for resource of type j then it can be represented as Request [i,j]=c.
● Step-1: If Request [i,j]<= Need[i,j] then it means the process is requesting for resources less than or equal its need
for execution. If Request [i,j]>Need[i,j] then an error flag can be raised stating that process is requesting for resources
beyond its need. If Request [i,j]<= Need[i,j] then proceed to step2.
Step-2: If Request [i.j]<Available[i,j] then it means the requested resources are available in the system and can be
allocated to the process. If Request [i.j]<Available[i,j] then proceed to step-3 else the process has to wait for the
available resource to be greater than equal to the requested resources.
Step-3 - Now, if Request[i.j]<Available[i,j] then resource are allocated to the process Pi and
the Available[i,j],Allocation[i,j]and Need[i,j] will updated as given below:
Available[i,j]=Available[i,j]-Request[i,j]
Allocation[i,j]=Allocation[i,j]+Request[i,j]
Need[i,j]=Need[i,j]-Request[i,j]
If the above resource allocation is safe then the transaction is completed and Pi is allocated its resources for execution.
But, if the new state of the system is unsafe then the process Pi has to wait for the resources to fulfill its request demands
and the old allocation state is again restored.
Er Parag Rastogi
CSE Department
Example 1. In this example, we have a process table with number of processes that contains allocation field (for showing
the number of resources of type: A, B and C allocated to each process in the table), max field
I
(for showing the maximum
number of resources of type: A, B, and C that can be allocated to each process) and also, the available field (for showing
the currently available resources of each type in the table).
Considering the above processing table, we need to calculate the following two things:
Q.1 Calculate the need matrix? Q.2 Is the system in a safe state?
Ans.1 We can easily calculate the entries of need matrix using the formula: (Need)i=(Max)i−(Allocation)i
Process Need
ABC
432
P0
221
P1
612
P2
844
P3
P4 111
1. For process P0, Need = (4, 3, 2) and Available = (3, 2, 1) clearly, the resources needed are more in number than the
available ones. So, now the system will move to process the next request.
2. For Process P1, Need = (2, 2, 1) and Available = (3, 2, 1) clearly, resources needed are less than equal to the available
resources within the system. Hence, request of P1 is granted.
3. For Process P2, Need = (6, 1, 2) and Available = (5, 3, 3) Clearly, the resources needed are more in number than the
available ones. So, now the system will move to process the next request.
4. For Process P3, Need = (8, 4, 4) and Available = (5, 3, 3) Clearly, the resources needed are more in number than the
available ones. So, now the system will move to process the next request.
Er Parag Rastogi
CSE Department
5. For Process P4, Need = (1, 1, 1) and Available = (5, 3, 3) Clearly, resources needed are less than equal to the available
resources within the system. Hence, request of P4 is granted. I
6. Now again check for Process P2, Need = (6, 1, 2) and Available = (6, 4, 5) clearly, resources needed are less than
equal to the available resources within the system. Hence, request of P2 is granted.
7. Now again check for Process P3, Need = (8, 4, 4) and Available = (9, 4, 6) clearly, resources needed are less than
equal to the available resources within the system. Hence, request of P3 is granted.
8. Now again check for Process P0, Need = (4, 3, 2), and Available (9, 6, 6) clearly, the request for P0 is also granted.
The system has allocated all the required number of resources to each process in a particular sequence. Therefore, it is
proved that the system is in a safe state.
Deadlock detection and recovery is the process of detecting and resolving deadlocks in an operating system
1. Prevention: The operating system takes steps to prevent deadlocks from occurring by ensuring that the system is
always in a safe state, where deadlocks cannot occur. This is achieved through resource allocation algorithms such
as the Banker’s Algorithm.
2. Detection and Recovery: If deadlocks do occur, the operating system must detect and resolve them. Deadlock
detection algorithms, such as the Wait-For Graph, are used to identify deadlocks, and recovery algorithms, such as
the Rollback and Abort algorithm, are used to resolve them. The recovery algorithm releases the resources held by
one or more processes, allowing the system to continue to make progress.
Deadlock Detection:
In this case for Deadlock detection, we can run an algorithm to check for the cycle in the Resource Allocation
Graph. The presence of a cycle in the graph is a sufficient condition for deadlock.
Er Parag Rastogi
CSE Department
I
In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 → P1 → R2 → P2. So,
Deadlock is confirmed.
Wait-for-Graph: Contains only Processes after removing the Resources while conversion from
Resource Allocation Graph.
Algorithm:
Step 1: Take the first process (Pi) from the resource allocation graph and check the path in which it is
acquiring resource (Ri), and start a wait-for-graph with that particular process.
Step 2: Make a path for the Wait-for-Graph in which there will be no Resource included from the
current process (Pi) to next process (P j), from that next process (P j) find a resource (Rj) that will be
acquired by next Process (Pk) which is released from Process (P j).
Now we will see the working of this Algorithm with an Example. Consider a Resource Allocation
Graph with 4 Processes P1, P2, P3, P4, and 4 Resources R1, R2, R3, R4.
Find if there is a deadlock in the Graph using the Wait for Graph-based deadlock detection algorithm.
Step 1: First take Process P1 which is waiting for Resource R1, resource R1 is acquired by Process P2, Start a
Wait-for-Graph for the above Resource Allocation Graph.
Step 2: Now we can observe that there is a path from P1 to P2 as P1 is waiting for R1 which is been acquired
by P2. Now the Graph would be after removing resource R1 looks like.
Step 3: From P2 we can observe a path from P2 to P3 as P2 is waiting for R4 which is acquired by P3. So make
a path from P2 to P3 after removing resource R4 looks like.
Er Parag Rastogi
CSE Department
I
Step 4: From P3 we find a path to P4 as it is waiting for P3 which is acquired by P4. After removing R3 the
graph looks like this.
Step 5: Here we can find Process P4 is waiting for R2 which is acquired by P1. So finally the Wait-for-Graph
is as follows:
Step 6: Finally In this Graph, we found a cycle as the Process P4 again came back to the Process P1 which is
the starting point (i.e., it's a closed-loop). So, According to the Algorithm if we found a closed loop, then the
Er Parag Rastogi
CSE Department
system is in a deadlock state. So here we can say the system is in a deadlock state.
I
Now consider another Resource Allocation Graph with 4 Processes P1, P2, P3, P4, and 3 Resources R1, R2, R3.
Find if there is a deadlock in the Graph using the Wait for Graph-based deadlock detection algorithm.
Step 1: First take Process P1 which is waiting for Resource R1, resource R1 is acquired by Process P2, Start a
Wait-for-Graph for the above Resource Allocation Graph.
Step 2: Now we can observe that there is a path from P1 to P2 and also from P1 to P4 as P1 is waiting for R1
which is been acquired by P2 and P1 is also waiting for R2 which is acquired by P4. Now the Graph would be
after removing resources R1 and R2 looks like.
Step 3: From P2 we can observe a path from P2 to P3 as P2 is waiting for R3 which is acquired by P3. So make
a path from P2 to P3 after removing resource R3 looks like.
Er Parag Rastogi
CSE Department
I
Step 4: Here we can find Process P4 is waiting for R3 which is acquired by P3. So finally the Wait-for-Graph
looks like after removing Resource R3 looks like.
Step 5: In this Graph, we don't find a cycle as no process came back to the starting point (i.e., there is no closed
loop). So, According to the Algorithm if we found a closed loop, then the system is in a deadlock state. But here
we didn't find any closed loop so the system is not in a deadlock state. The system is in a safe state.
Note: In Example 2, even though it looks like a loop but there is no process that has reached the first process,
or starting point again. So there is no closed loop.
Deadlock Recovery:
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is a time and space-
consuming process. Real-time operating systems use Deadlock recovery.
Er Parag Rastogi
CSE Department