In multiprogramming systems, more than one process is being executed at the same time. All of these processes compete for finite number of resources in order to complete their execution. When one thread wants to have access to a resource which is currently being used by some other thread, the first thread waits for the other theard to leave the resource. During this competetion there may exist a condition where a in a set of processes all of the processes wanted to have access to the same resource which could have made them enter the state of waiting. All of them will now have to wait for some other porcess in that set to do something in order for one of them to come out of the waiting state.
Since all of the processes in the set are depending on each other for the resource they will stay in that situation for ever. This situation is called a deadlock.
Image Courtesy: Wikipedia
For a deadlock to appear there are some necessary conditions for it to hold true. These conditions are following.
- Mutual Exclusion. There must be at least one resource that is in non-shareable mode meaning that only one thread can access that resource at a time.
- Hold and Wait. A process must have access to one resource while asking for some other resource which are also held by some other threads.
- No Preemption. A resource can only be left voluntarily by the process which have access to it.
- Circular Wait. Each process must be asking for some resource which might be held by some other process which is also asking for some other process.