- In previous post I explained about what is deadlock and now in this post I will try to explain necessary conditions to occur deadlock. It focuses on what conditions need to be true for the deadlock creation.
- A system is said to be in a deadlock state if the following four conditions are true simultaneously:
- At least one resource must be held by the processes in a non-shareable mode; that means only one process at a time can use the resource.
- If other process requests that resource then it must wait till that resource is released.
- Consider the following scenario in which process P1 and P2 both are having at least one resource. (P1 is having tap drive and P2 is having printer) and both resources are non-shareable. If process P1 requires printer to complete its task then it must have to wait till printer is released but printer is allocated to process P2 which is also waiting for tap drive to finish its job so both process are waiting for each other to finish so it will lead to the creation of deadlock.
Hold and Wait
- A process must have at least one resource and waiting for other resources which are allocated to other waiting processes.
- In our example P1 has tap drive and is waiting for printer which is held by P2.
- P1 can only finish if printer released by P2 which is not possible as P2 is waiting for tap drive so here, hold and wait condition becomes true and we can say system is in a deadlock state.
- If any process holds any resource but does not waiting for any resource than that process can complete its execution and will release its all resources that can be assigned to other waiting processes later and hence all process will be completed and no deadlock will be created.
- So for the deadlock to occur both hold and wait condition must be true simultaneously.
- Resources cannot be preempted; that means a resource cannot be force fully deallocated and it can be released only by the process voluntarily after completing its task.
- If resources are preempted then it can be forcefully deal located and given to the process waiting for that and hence no deadlock will exist in the system.
- There must exist a set of waiting processes (P0,P1,P2,…Pn) such that P0 is waiting for 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 resource held by P0.
- In our example, we can see that circular wait is exist as P1 is waiting for P2 and P2 is waiting for P1 to complete.
- All four conditions must be true simultaneously to occur a deadlock.
- The circular wait condition also implies the hold and wait condition so all four conditions are interdependent.