Individually necessary and jointly sufficient conditions for deadlock
A deadlock situation on a resource can arise only if all of the following conditions occur simultaneously in a system: # '' Mutual exclusion:'' At least one resource must be held in a non-shareable mode; that is, only one process at a time can use the resource. Otherwise, the processes would not be prevented from using the resource when necessary. Only one process can use the resource at any given instant of time. # ''Hold and wait'' or ''resource holding:'' a process is currently holding at least one resource and requesting additional resources which are being held by other processes. # ''No preemption:'' a resource can be released only voluntarily by the process holding it. # ''Circular wait:'' each process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource. In general, there is a set of waiting processes, ''P'' = , such that ''P''1 is waiting for a resource held by ''P''2, ''P''2 is waiting for a resource held by ''P''3 and so on until ''P''''N'' is waiting for a resource held by ''P''1. These four conditions are known as the ''Coffman conditions'' from their first description in a 1971 article byDeadlock 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 Common 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: # ''Process termination:'' one or more processes involved in the deadlock may be aborted. One could choose to abort all competingPrevention
Deadlock prevention works by preventing one of the four Coffman conditions from occurring. * Removing the ''mutual exclusion'' condition means that no process will have exclusive access to a resource. This proves impossible for resources that cannot be spooled. But even with spooled resources, the deadlock could still occur. Algorithms that avoid mutual exclusion are called non-blocking synchronization algorithms. * The ''hold and wait'' or ''resource holding'' conditions may be removed by requiring processes to request all the resources they will need before starting up (or before embarking upon a particular set of operations). This advance knowledge is frequently difficult to satisfy and, in any case, is an inefficient use of resources. Another way is to require processes to request resources only when it has none; First, they must release all their currently held resources before requesting all the resources they will need from scratch. This too is often impractical. It is so because resources may be allocated and remain unused for long periods. Also, a process requiring a popular resource may have to wait indefinitely, as such a resource may always be allocated to some process, resulting in resource starvation. (These algorithms, such as serializing tokens, are known as the ''all-or-none algorithms''.) * The ''no preemption'' condition may also be difficult or impossible to avoid as a process has to be able to have a resource for a certain amount of time, or the processing outcome may be inconsistent or thrashing may occur. However, the inability to enforce preemption may interfere with a ''priority'' algorithm. Preemption of a "locked out" resource generally implies a rollback, and is to be avoided since it is very costly in overhead. Algorithms that allow preemption include lock-free and wait-free algorithms and optimistic concurrency control. If a process holding some resources and requests for some another resource(s) that cannot be immediately allocated to it, the condition may be removed by releasing all the currently being held resources of that process. * The final condition is the ''circular wait'' condition. Approaches that avoid circular waits include disabling interrupts during critical sections and using a hierarchy to determine a partial ordering of resources. If no obvious hierarchy exists, even the memory address of resources has been used to determine ordering and resources are requested in the increasing order of the enumeration. Dijkstra's solution can also be used.Deadlock avoidance
Similar to deadlock prevention, deadlock avoidance approach ensures that deadlock will not occur in a system. The term "deadlock avoidance" appears to be very close to "deadlock prevention" in a linguistic context, but they are very much different in the context of deadlock handling. Deadlock avoidance does not impose any conditions as seen in prevention but, here each resource request is carefully analyzed to see whether it could be safely fulfilled without causing deadlock. Deadlock avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime. Deadlock avoidance algorithm analyzes each and every request by examining that there is no possibility of deadlock occurrence in the future if the requested resource is allocated. The drawback of this approach is its requirement of information in advance about how resources are to be requested in the future. One of the most used deadlock avoidance algorithm is Banker's algorithm.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 byDistributed 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).See also
* Aporia * Banker's algorithm * Catch-22 (logic) * Circular reference * Dining philosophers problem * File locking * Gridlock (in vehicular traffic) * Hang (computing) * Impasse * Infinite loop * Linearizability * Model checker can be used to formally verify that a system will never enter a deadlock * Ostrich algorithm * Priority inversion * Race condition * Readers-writer lock * Sleeping barber problem *References
Further reading
* * * * * * * *External links
*