Deadlock Prevention

Deadlock prevention can be achieved by making any one condition false from the following four conditions. Because of the deadlock, these four conditions are compulsory.

1. Mutual exclusion
2. No preemptive
3. Hold and wait
4. Circular wait

“Here, we will see how to make a condition false.”

1. Eliminate Mutual exclusion

If all the resources are shareable, then deadlock will never occur, but it is not possible.

To make all resources shareable, like CPU, is shareable for all resources. But printer and tape drive are not shareable for all resources. So, to make resources shareable, like printers, leads to incorrect results. As process 1 was printing some documents and P2 came for printing, we could not start printing P2 at the same time.

2. Eliminate No Preemptive

In simple words, make the resources preemptive to prevent a deadlock situation.

Eliminate-No-Preemptive

If process P2 requests resources R1 while process P1 is holding R1, and P1 releases R1 for P2, the deadlock can be resolved. It is also not a good solution in all cases, like printing, but it might be suitable for CPU utilization.

If a resource is 90% used by a process, then it is preempted, and then it will be very costly.

3. Eliminate Hold and wait

As the new process demands an allocated resource, then that allocated resource is given to the demanding process. For example, process P1 came, and its request for resources R1, R2 and R3. These resources are allocated to some other process. Then, a different process will release the r1, r2, r3 process at the same time for P1. It is called No Hold and Wait. If they don’t release it, then it will be held and wait.

4. Eliminate Circular wait

If the last process is demanding for the first resource and that resource is allocated to the first process. A loop kind of situation is created there is called circular wait.

eliminate circular wait

The solution of circular wait is explained under

The process can only request high-order resources. The process cannot request low-order resources to remove the circular wait.

In the above diagram, P3 cannot request R1 or R2 because P3’s previous request was R3, and the next request will be greater than R3.

So, the next request for resources must be greater than the previous request for resources to remove circular wait. In this way, by eliminating circular wait, a deadlock can be prevented.

Note: If we make any one condition or more false from the above 4 conditions, then a deadlock can be prevented. Because the condition of deadlock is to fulfill the above four conditions, in this way, deadlock is removed.