Prevention Method to avoid Deadlock


Prevention Method to avoid Deadlock

In the last post we discussed about 4 ways to avoid deadlock. Today, we will start with Prevention Method to avoid Deadlock. We will see how we can violate one of the four conditions of deadlock.

Prevention was a mechanism which guarantees that deadlock will not occur in the system. This approach to avoid deadlock shall be used when the deadlock can cause severe harm to the system.
For eg: There is an airplane and all the functionality of airplane is handled by a software. So, can we take the risk of deadlock in that software? Obviously NO because it can cause severe harm to many people. In these type of systems we cannot take the risk of deadlock to happen. So, we use prevention method to avoid deadlock in these systems.

But then how will prevention method avoid deadlock? Prevention method will violate any of the 4 conditions of deadlock so that deadlock cannot occur.

Mutual Exclusion

We cannot violate mutual exclusion condition because it is hardware property of that resource that it can be accessed by only 1 process at a time
eg: One printer can be accessed by 1 device at a time and it cannot be shared by multiple devices.

Hold and Wait

I hope you guys know what hold and wait was, in short it means a process will acquire the resources available and wait for the other resources. To violate the hold and wait condition we have 3 methods:

Conservative approach: Let say there were 2 processes P1 and P2 and 2 resources R1 and R2. P1 wanted R2 so it acquired it and at the same time P2 wanted R1 so it acquired it. But after execution P1 wants R1 now but it is acquired by P2. Similarly, P2 wants R2 now but it is acquired by P1. So, in this method there is a condition that a process is allowed to start execution if and only if it has acquired all its resources. But this approach has some disadvantages like:

  • It is less efficient. Suppose You want to scan an image and then print it. So, according to this approach you will acquire scanner and printer. But while you were using scanner someone else could have used the printer.
  • It cannot be implemented because it is not possible to determine all resources needed by a process initially.
Some advantages are:
  • It is easy.
  • It avoid deadlock.
Do not Hold: In this process will acquire only desired resources, but before making any fresh request it must release all the resources that it currently hold. Advantages of this approach are:
  • It is implementable.
  • It is efficient.
Wait Timeout: We place a maximum time upto which a process can wait. After which a process must release all the holding resources.

No preemption

I hope you know what no preemption was, in short it means that a resource cannot be forcefully taken from a process. To violate this condition we allow the process to forcefully preempt the resource taken by other processes but there are some conditions that it must follow which are given below:
  • This privilege can only be taken by high priority processes only.
  • The process which are in the waiting state must be selected as victims for forceful preemption of resource instead of running process. 
Circular Wait

I hope you know what circular wait is, in short when a process P1 has requested R1, R1 is acquired by P2 and P2 has requested R2 and R2 is acquired by P1.
Circular wait can be eliminated by first giving a natural number N to every resource.
f: N->R
Allow every process to either only in increasing or decreasing order of resource number.
If a process require a lesser( in case of increasing order ), then it must first release all the resources larger than required number.

Conclusion

This was all about Prevention Method to avoid Deadlock. Hence deadlock can be avoided if you break any of the above conditions except Mutual Exclusion.

Post a Comment (0)
Previous Post Next Post