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 following scenario: we have two processes, P1 and P2. Process P1 is having resource R1 and waiting for resource R2. Process P2 has the resource R2 and is waiting for the resource R1 at the same time. As a result, process P1 is waiting to acquire a resource from process P2, and process P2 is also waiting for process P1 to release its resource. But neither of them is releasing any resources. Hence, they are waiting for each other to relinquish the resource. This results in endless waiting and is thus referred to as “deadlock”.
Processes in an operating system (distributed or centralized) and database transactions are two common contexts where deadlocks can occur. Distributed system deadlocks are more difficult to avoid, prevent, or even detect. Because all relevant information is distributed over multiple machines, they are difficult to track down and cure.
In distributed systems, there are two main categories of deadlocks:
- Resource Deadlock: Resource deadlock refers to the deadlock state when the resource required by the first process is locked by the second one and the resource required by the second process is locked by the first process. So, the waiting state is indefinite between them resulting in deadlock.
- Communication Deadlock: In distributed systems, communication deadlock refers to a state when processes are waiting for the messages to start the execution.
Difference between Resource Deadlock and Communication Deadlock in Distributed System:
|Sr. Nusedo.||Parameter||Resource Deadlock||Communication Deadlock|
|1.||Definition||Resource deadlock refers to the deadlock state when the resource required by the first process is locked by the second one and the resource required by the second process is locked by the first process.||In distributed systems, communication deadlock refers to a state when processes are waiting for the messages to start the execution.|
|2.||Execution||A process cannot start running until it obtains all of the resources it is waiting for.||A process cannot start executing until it can communicate with one of the processes it is waiting for.|
|3.||Waiting for||Here, processes wait for the resources.||Here, processes wait for the messages.|
|4.||Set of processes||The process set is resource deadlocked if each process in the set wants resources that are using by another process in the set and must get all of the required resources to become unblocked.||The set is communication Deadlocked if each process in the set is waiting to communicate with another process in the set, and no process in the set ever initiates any more communication until it receives the communication for which it is waiting.|
|5.||Condition for execution||The AND condition can also be used to reflect the fact that the process will only run if it has all of the requisite resources.||Because it requires at least one of the resources to continue its Process, it can be described using OR conditions.|
For example, a system is having two processes P1 and P2 and for the execution of these processes, R1 and R2 resources are required. In this scenario, process P1 is having resource R1 and waiting to acquire resource R2 while process P2 is having resource R2 and waiting to acquire resource R1. Both Processes are waiting for each other for releasing the resources after completion. Hence, a Resource deadlock occurred.
For example, a system is having three processes, namely, P1, P2, and P3. Here, a process waits for another process in a group of processes to communicate with it. Process P1 is waiting to get a message from process P2, process P2 is waiting to get a message from process P3, and process P3 is waiting to get a message from process P1 hence none of them will be able to execute and reach an unblocking state. Thus, a communication deadlock occurs.
Necessary Conditions for Deadlock in Distributed System:
The following are the necessary and sufficient conditions for deadlock in a distributed system. These conditions are also known as Coffman conditions.
- Mutual Exclusion: This condition implies the existence of atleast one resource in the system which can be used by only one process at a given point in time. Example- printer as a resource that can be used by only one process at a moment. If no such resource exists in the system, then deadlock will not occur. Another illustrative example is shown below, is if a process P1 is allocated resource R1 at a particular instant of time then process P2 cannot use that resource R1 simultaneously with process P1.
- Hold and wait: This condition implies the existence of a process holding some resources and waiting for another one that is held by some other process in the system. For example, if a process P1 is having resource R1 and at the same time, it can request another resource R2 which is currently allocated to process P2.
- No Preemption: This condition implies that resources once allocated to a process cannot be taken away until and unless the process voluntarily releases them. For example, if a process P1 is having resource R1 which is required by process P2 then process P2 cannot forcefully take that resource until and unless process P1 voluntarily releases that resource.
- Circular wait: This condition refers to the waiting of processes in a circular manner where the last process waits for the resource which is held by the first one. For example, process P1 is waiting for the resource R which is currently held by process P2, process P2 is waiting for the resource R which is currently held by process P3, process P3 is waiting for the resource R which is currently held by process P4 and process P4 is waiting for the resource R which is currently held by process P1. This is called circular wait and is shown below.
Note: All the above four conditions need to occur simultaneously for the occurrence of deadlock in the system.
Necessary and Sufficient Conditions for Deadlock in Distributed System:
If the graph is having a directed cycle then deadlock is present. The resource allocation state can be easily recorded with a directed graph. In such a graph, there are n processes and m resources:
- P1 –> R1 implies resource R1 is requested by process P1
- P1 <– R1 implies resource R1 is allocated to process P1
Resource Allocation graphs can also be used for the detection of deadlocks. If the graph contains no cycles then the system is not in a deadlock state. The deadlock exists if the graph contains cycles and there is the availability of only one instance of each process. But the deadlock state is not ensured in case there is the availability of multiple instances of each process. So the Necessary and Sufficient Conditions for deadlock in distributed systems are as follows:
- A cycle is a necessary condition for deadlock in a distributed system.
- If there is only a single unit of each resource type/instance involved in the cycle, a cycle is both a necessary and a sufficient condition for a deadlock to exist.
- If one or more of the resource types /instances involved in the cycle have more than one unit, a knot is a sufficient condition for a deadlock to exist.