Deadlock

In concurrent computing, a deadlock is a state in which each member of a group waits for another member, including itself, to take action, such as sending a message or more commonly releasing a lock. Deadlocks are a common problem in multiprocessing systems, parallel computing, and distributed systems, where software and hardware locks are used to arbitrate shared resources and implement process synchronization.
In an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process. If a process is unable to change its state indefinitely because the resources requested by it are being used by another waiting process, then the system is said to be in a deadlock.
In a communications system, deadlocks occur mainly due to lost or corrupt signals rather than resource contention.
Necessary conditions.
A deadlock situation on a resource can arise if and only if all of the following conditions hold simultaneously in a system:
These four conditions are known as the "Coffman conditions" from their first description in a 1971 article by Edward G. Coffman, Jr.
While these conditions are sufficient to produce a deadlock on single-instance resource systems, they only indicate the possibility of deadlock on systems having multiple instances of resources.
Deadlock handling.
Most current operating systems cannot prevent deadlocks. When a deadlock occurs, different operating systems respond to them in different non-standard manners. Most approaches work by preventing one of the four Coffman conditions from occurring, especially the fourth one. Major approaches are as follows.
Ignoring deadlock.
In this approach, it is assumed that a deadlock will never occur. This is also an application of the Ostrich algorithm. This approach was initially used by MINIX and UNIX. This is used when the time intervals between occurrences of deadlocks are large and the data loss incurred each time is tolerable.
Ignoring deadlocks can be safely done if deadlocks are formally proven to never occur. An example is the RTIC framework.
Detection.
Under the deadlock detection, deadlocks are allowed to occur. Then the state of the system is examined to detect that a deadlock has occurred and subsequently it is corrected. An algorithm is employed that tracks resource allocation and process states, it rolls back and restarts one or more of the processes in order to remove the detected deadlock. Detecting a deadlock that has already occurred is easily possible since the resources that each process has locked and/or currently requested are known to the resource scheduler of the operating system.
After a deadlock is detected, it can be corrected by using one of the following methods:
Prevention.
Deadlock prevention works by preventing one of the four Coffman conditions from occurring.
Livelock.
A "livelock" is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing.
The term was coined by Edward A. Ashcroft in a 1975 paper in connection with an examination of airline booking systems. Livelock is a special case of resource starvation; the general definition only states that a specific process is not progressing.
Livelock is a risk with some algorithms that detect and recover from "deadlock". If more than one process takes action, the deadlock detection algorithm can be repeatedly triggered. This can be avoided by ensuring that only one process (chosen arbitrarily or by priority) takes action.
Distributed deadlock.
"Distributed deadlocks" can occur in distributed systems when distributed transactions or concurrency control is being used.
Distributed deadlocks can be detected either by constructing a global wait-for graph from local wait-for graphs at a deadlock detector or by a distributed algorithm like edge chasing.
"Phantom deadlocks" are deadlocks that are falsely detected in a distributed system due to system internal delays but do not actually exist.
For example, if a process releases a resource "R1" and issues a request for "R2", and the first message is lost or delayed, a coordinator (detector of deadlocks) could falsely conclude a deadlock (if the request for "R2" while having "R1" would cause a deadlock).