In multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources and if the resources are not available then the process enters into the waiting state. Sometimes a waiting process is never again able to change its status because the resources requested by it are held by other waiting processes. This situation is called as deadlock. When a process request for resources held by another waiting process which in turn is waiting for resources held by another waiting process and not a single process can execute its task, then deadlock occurs in the system.
Example: Consider a system with three disk drives and three processes. When each process request one disk drive, system allocates one disk drive to each process. Now there is no more drive available in the system. If all three processes request for one more disk drive, then all three processes will go into the waiting state and system will go in deadlock state. Because any one process from the three can execute only when one of them will release the disk drive allocated to it.
Necessary Conditions:
1. Mutual exclusion: At least one resource must be held in a nonsharable mode; that is, only one process at a time can use the resource.
2. Hold and Wait: A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.
3. No pre-emption: Resources cannot be pre-empted i.e a resource can be released only voluntarily by the process holding it.
4. Circular wait: A set {P0,P1…Pn} of waiting processes must exist such that P0 is waiting for a resource held by P1,P1 is waiting for a resource held by P2,…,Pn-1 is waiting for a resource held by Pn and Pn is waiting for a resource held by P0. Each process is waiting for the resources held by other waiting processes in circular form.