Deadlock Avoidance in Distributed System
Last Updated :
26 May, 2022
Deadlocks are the fundamental problems in distributed systems. A Deadlock is a situation where a set of processes are blocked as each process in a Distributed system is holding some resources and that acquired resources are needed by some other processes. In this situation, a cycle arrives at a deadlock.
Conditions of deadlock: A process in distributed systems uses different resources in the following way.
That means, a process requests the resource and uses it in its execution and after execution, it releases the resource.
Methods for Handling Deadlock :
There are four methods for handling Deadlock:
- Deadlock Prevention
- Deadlock Avoidance
- Deadlock Detection
- Deadlock Recovery
Deadlock Avoidance:
In Deadlock Avoidance, the system will be checked if it is in a safe state or an unsafe state. Safe state is ensured when the request for the resource by the process is permitted when there is no deadlock found in the system. If there is deadlock found then the system will be in an unsafe state.
To avoid deadlocks the process should inform the system that how many resources that a process should request for its execution. To make that happen we use Algorithm.
Step 1: Work and finish the 2 vectors of size m & n. Initialize work with available and finish[i] = false
for i=1 to n , m=>#resources and n=>#processes.
Step 2: Find an i such that both (i) finish[i] = false (ii) need(i) <= work , if no such i exists then go to Step 4.
Step 3: Work = work + allocation
finish[i] = true
go to Step 2.
Step 4: if finish[i] = true for all i then the system is in a safe state.
Suppose that there are m = 4 (A, B, C, D) resources and n=5 (P0, P1, P2, P3, P4) processes. Build a safe sequence to keep the system in a safe state.
Here,
n => #processes = 5 = <P0, P1, P2, P3, P4>
m => #resources = 4 = (A, B, C, D)
Available: It defines how many instances that are available for a particular resource. In the above example A – 1, B – 5, C – 2, D – 0 resources available.
Size = Available [m] => 1 D array.
Allocation: It defines the number of instances that are allocated for all the resources of a particular process. In the above example for Process P0 we are having A – 0, B – 0, C – 1, D – 2 resources allocated.
Size = Allocation [n x m] => 2 D array.
Max: It defines the maximum number of instances that are available for all the resources of a particular process. In the above example for Process P0, we are having a maximum of A – 0, B – 0, C – 1, and D – 2 resources available.
Size = Max [n x m] => 2 D array.
Need: It tells how many instances will be required more for a resource of a process.
Size = Need [n x m] => 2 D array.
To check for a safe sequence first we need to calculate the Need matrix.
Need(i) = Max(i) - Allocation(i)
For example, if we take P0 then the need would be:
A = (0-0)
B = (0-0)
C = (1-1)
D = (2-2)
similarly for all processes.
After calculating the need matrix now start according to the algorithm.
Step 1: Initialize work and finish
Step 2: Now take Process P0 and according to step-2 in algorithm we need to find an i such that both conditions
(i) finish[i] = false
(ii) need(i) <= work should be satisfied.
we found for P0, finish[i] = false and need(i) <= work
such that 0<=1 -> True, 0<=5 -> True, 0<=2 -> True, 0<=0 -> True. So both conditions are satisfied.
Now according to step-3 in algorithm execute (work = work + allocation) & finish[i] = true and go to step-2 in algorithm again.
Step 3: Now take Process P1, we found finish [i] = false but while coming to need(i) <= work, the condition became false
such that 0<=1 -> True, 7<=5 -> False, 5<=3 -> False, 0<=2 -> True. Condition failed, so skip Process P1 and go to Process P2.
Step 4: For Process P2, we found both the conditions are satisfied. So calculate according to step-3 in the algorithm.
Step 5: For Process P3, we found both the conditions are satisfied. So calculate according to step 3 in the algorithm.
Step 6: For Process P4, we found both the conditions are satisfied. So calculate according to step 3 in the algorithm.
Step 7: Now we are remaining with the Process P1. So, if we check for Process P1, we found both the conditions are satisfied. So calculate according to step 3 in the algorithm.
Step 8: So when we go to step 2 in the algorithm again we can’t find any ‘i’ for both the conditions as all processes are completed. So now go to step 4 in the algorithm. we find all finish [i] = true. So according to the algorithm, the system is in a safe state.
Finally, the safe sequence for the above example would <P0, P2, P3, P4, P1>.
Cross-checking: We can cross-check the solution by adding the Allocation matrix values for each resource for all the processes with the Available matrix for each resource then it would be the final Work matrix values for each resource.
In the above example if we add
=> Allocation + Available for resource 'A'
for all processes = 0 + 1 + 1 + 0 + 0 + 1 = 3.
=> Allocation + Available for resource 'B'
for all processes = 0 + 0 + 3 + 6 + 0 + 5 = 14.
=> Allocation + Available for resource 'C'
for all processes = 1 + 0 + 5 + 3 + 1 + 2 = 12.
=> Allocation + Available for resource 'D'
for all processes = 2 + 0 + 4 + 2 + 4 + 0 = 12.
Here, we can clearly observe the values generated after adding Allocation & Available matrix values are equal to the final. Work matrix values. So the system is in a safe state.
Disadvantage:
- It has a fixed number of processes and resources that are required while executing.
- No other processes can be executed in the middle and also any resource cannot be allocated to another process until deadlock arrives.
- Maximum resources that are acquired for the processes should be known and allocated in advance.
- In Banker’s Algorithm, all resources can be requested for a certain amount of time but the time is one year.
- So these processes which are acquiring resources can release those resources after that certain amount of time, until then remaining processes should wait for the acquisition of resources. This is the starvation problem.
Similar Reads
Conditions for Deadlock in Distributed System
This article will go through the concept of conditions for deadlock in distributed systems. Deadlock refers to the state when two processes compete for the same resource and end up locking the resource by one of the processes and the other one is prevented from acquiring that resource. Consider the
7 min read
Phantom Deadlock in Distributed System
Phantom deadlocks, in distributed systems, refer to situations where multiple processes or threads get stuck and can't move forward because of conflicts in synchronization and resource allocation. This happens when different tasks are being executed at the time causing each process to wait for a res
4 min read
Advanced Distributed Systems
Advanced Distributed Systems provides an in-depth look into the cutting-edge methods and technologies shaping today's distributed systems. It covers their architecture, scalability, and fault tolerance, illustrating how these advancements enable robust, high-performance, and adaptable solutions in a
7 min read
Deadlock Detection in Distributed Systems
Prerequisite - Deadlock Introduction, deadlock detection In the centralized approach of deadlock detection, two techniques are used namely: Completely centralized algorithm and Ho Ramamurthy algorithm (One phase and Two-phase). Completely Centralized Algorithm - In a network of n sites, one site is
2 min read
Deadlock detection in Distributed systems
Deadlock detection in distributed systems is crucial for ensuring system reliability and efficiency. Deadlocks, where processes become stuck waiting for resources held by each other, can severely impact performance. This article explores various detection techniques, their effectiveness, and challen
9 min read
Availability in Distributed System
In distributed systems, availability refers to the system's ability to remain operational and accessible despite failures or disruptions. This article explores key concepts, challenges, and strategies for ensuring high availability in distributed environments, emphasizing the balance between reliabi
12 min read
Centralized Architecture in Distributed System
The centralized architecture is defined as every node being connected to a central coordination system, and whatever information they desire to exchange will be shared by that system. A centralized architecture does not automatically require that all functions must be in a single place or circuit, b
4 min read
Bully Algorithm in Distributed System
The Bully Algorithm is a popular method for electing a coordinator in distributed systems. When nodes or processes in a system need a leader to manage tasks or make decisions, this algorithm helps them choose one, even if some nodes fail. The process involves nodes "bullying" each other by checking
9 min read
Distributed Consensus in Distributed Systems
In distributed systems, achieving consensus among nodes is critical for maintaining coherence and reliability. This article explores the principles, algorithms, challenges, and applications of distributed consensus, which are essential for ensuring agreement across decentralized networks. Important
11 min read
Common Antipatterns in Distributed Systems
Distributed systems offer scalability and fault tolerance, but improper design can lead to inefficiencies known as antipatterns. This article explores common antipatterns in distributed systems, highlighting pitfalls such as Single Points of Failure and tight coupling, and provides strategies to avo
8 min read