A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does.
* Assume thread/process T1 has exclusive access to resource R1.
* Thread/ process T2 has exclusive access to resource R2.
* If T1 needs exclusive access to R2 and T2 needs exclusive access to R1,
* Neither thread can continue.
* They are deadlocked.
The simplest example is that of two tasks: 1 and 2. Each task requires two mutexes: A and B. If Task 1 takes mutex A and waits for mutex B while Task 2 takes mutex B and waits for mutex A, then each task is waiting for the other to release the mutex.:
These tasks may run without problems for a long time, but eventually one task may be preempted in between the wait calls, and the other task will run. In this case, Task 1 needs mutex B to be released by Task 2, while Task 2 needs mutex A to be released by Task 1. Neither of these events will ever happen.
Causes of Deadlock
Mutual exclusion: only one process at a time can use a resource
Hold and wait: A process holding at least one resource is waiting to acquire additional resources held by other resources
No preemption: A resource can be released only voluntarily by the process holding it after that process has completed the task
Circular wait: A set of processes- P1 to Pn …P1 waiting for the resource held by P2, P2 waiting for resource held by P3 etc.