It is requested to all the readers to please read the previous post on Introduction to Deadlock fro better understanding of this post.
Before solution of deadlock one must have complete knowledge of why deadlock problem arises. So Today we are going to discuss about 4 conditions that lead to deadlock. So, let's start, shall we?
Conditions for Deadlock to Occur
Mutual Exclusion
It says that at-least one resource in the system which can be used in non-sharable mode i.e One at a time. In simple words it means that there must be at-least one resource can be used by 1 process at a time.
e.g : Printer is a device which can be used by one process at a time.
Hold and Wait
It means that a process acquires a resource and then requests another resource which is being held by another process. In this a process acquire a resource if it is available otherwise the process might go into starvation.
e.g : You want to drink soup so you took a bowl but there was no spoon so you will probably acquire bowl and search for a spoon. In the similar fashion a process holds a resource and waits for another.
No Preemption
It means that a resource cannot be preempted from one process to another process. Resource can be released only voluntarily be the process holding it.
e.g : You want to drink soup so you took a bowl but there was no spoon so you will probably acquire bowl and search for a spoon. You want a spoon but you cannot forcefully take the spoon from another person.
Circular Wait
Each process must be waiting for a resource which is being held by another process which in turn is waiting for a process to release its resource.
eg: You want to drink soup so you took a bowl but there was no spoon so you will probably acquire bowl and search for a spoon. You find a person who has a spoon but no bowl so you asked him to give you the spoon but he says you to give him your bowl. This is circular wait.
Now let's check if all these condition leads to deadlock or not. Consider the picture below which shows 3 process P1, P2, P3. P1 has requested for a resource to P2 and P2 has requested for a resource to P3.
1. P1, P2, P3 have non-shareable resources other wise each of the processes could have executed and no process has to wait for other process. Hence, Mutual Exclusion is satisfied.
2. It might be possible that each process is holding some resource and has requested for another resource. Hence, Hols and Wait is also satisfied.
3. If forcefully processes could have acquired resources then P1 won't have to request to P2 or P2 won't have requested to P3 or P3 won't have to request to P1. Hence, no-preemption is also satisfied.
4. P1 is waiting for P2, P2 is waiting for P3 and P3 is waiting for P1. So, all 3 processes will wait for the other process to release resource indefinitely. So, circular wait is also satisfied.
5. Hence the above picture shows that a P1, P2, P3 are in Deadlock.