Deadlock Prevention in OS

Deadlock prevention in OS means that before the deadlock has occurred we should have programs ready to prevent deadlock. This simply reflects the philosophy that prevention is better than cure. For deadlock prevention, we have to avoid the deadlock by designing our system in such a manner that it is completely able to avoid deadlock conditions. There are some main conditions from which the deadlock arises and if one of the four conditions due to which deadlock occurs can be avoided, then we can avoid the deadlock situation completely.

The four conditions due to which deadlock arises are:

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

Mutual exclusion

The mutual exclusion arises when the resources are made reserved for the use by one process at a time. They occur generally when one process can change the values by the other process, this is similar when you run two programs using similar variables simultaneously. To avoid the change in the variable values we use mutual exclusion. But the mutual exclusion method may lead to a deadlock so we have to prevent the deadlock by reverting mutual exclusion. So, now we have to make the resources shareable that were earlier reserved by the mutual exclusion. The resource such as memory and processing can be shared by many processes at a time by right programming but resources such as printers and tape drive are not designed in such a way that it can be shareable. So there exist some resources that cannot be shareable and thus we have to look into other deadlock conditions and check whether we can avoid them.



The non-preemption is the case when one process keeps on running until it has completed its task, which means that it does not empty the resources or preempt until and unless its work is completed. This is a deadlock situation because the one process might go in an infinite loop and then the other process will not be assigned the resources ever. Therefore the Non-preemption has to convert into the preemption such that the running process can stop and provide resources to other processes to work. This can be done with the help of the time Quantum method, in which each process are given some time to use the resource and complete the task and if the task is not completed in that time quanta then it can complete the remaining task when it is again allocated the time quanta and this happens for every process. So, this way we can prevent the deadlock.


Hold and wait

The hold and wait have to be avoided to prevent the deadlock situation. The hold and wait have to be converted into no hold and wait. The meaning by hold and wait is that one process will only begin to complete its task once all the resources are acquired by that process in this case it will have to wait once the other process has completed their task and left the emptied the resourced for another process to use by, then at that time it will acquire that resource and similarly, it has to hold other resources once they have been emptied by the other process. As the name suggests hold and wait it means that it has to hold the resource which it gets and not give them to another process this is creating the deadlock for the other process and then keep on waiting until it has all the resources. So, to prevent the deadlock situation, we have to avoid the hold and wait for the method.


Circular wait

Circular wait happens when processes are waiting for resource allocation which is held by other processes and in such a way no process can get the resource and this is creating a deadlock situation. For example, there are four resources such as a printer, register, CPU, and scanner and the process one has the printer allocated to it but it is waiting for the CPU to process the data to print but the CPU is allocated to the second process and it the second process has processed its data and it is waiting for the printer to print. Now in such a case, the first process is waiting for the resources that are held by the second process, and similarly, the second process is waiting for the first process to free the allocated resources. They are waiting circularly in the need for the resources. Now, we have to avoid the circular wait. The deadlock prevention strategy that was developed to avoid the circular wait is by numbering the resources and allocating resources in increasing order. From the above example, the resources such as a printer, CPU, register, and scanner we number these resources in an increasing order such that the printer is numbered one, the CPU is numbered second, the register is numbered third and the scanner is numbered fourth. Now, the process one is assigned the printer which is numbered one in the resource list Now the second process can only be assigned the resource which is greater than one in the resource list, this method can break the circular wait and prevent the deadlock. Similarly, if the process one is assigned a second resource then the process second cannot have any resources that are less than number two but it can have more than two resources.


Other Methods to Handle Deadlock in OS

Deadlock Ignorance

Deadlock ignorance also is known as the ostrich method as the name suggests is complete avoidance if the deadlock occurs. Whenever the deadlock situation occurs in the operating system we have to shut down the operating system to solve the deadlock situation and then restart the system. This is a widely used method for the deadlock in the operating system. You may be wondering why in the world there is a deadlock solution by the method of complete avoidance of the deadlock situation. The reason is that the deadlock situation occurs very rarely in the operating system, the situation may arise like once in a year for a situation that occurs so rare the programmer avoids writing a full-fledged code. This code will require space in the operating system and also consume some processing power that could have been used by other resources in the operating system such as resource management, memory management, etc. This is the reason that when the operating system halts completely then the best choice is to restart the computer. The deadlock avoidance technique also has the advantage of increasing the speed of the operating system that has become one of the influencing factors in the user experience. This is also known as the ostrich method because the ostrich submerges its head in the sand whenever there is a sand storm in the desert and the ostrich solves the problem by completely avoiding it and the same is the case with the deadlock avoidance technique.


Deadlock avoidance

The deadlock avoidance is again a strategy for avoiding deadlock situations. In such cases, we check which is the situation where the deadlock occurs and we only choose the safest option from all the situations where the possibility of the deadlock situation to occur is the least. This method is also known as the banker’s algorithm and is invented by the scientist named Dijkstra who is popularly known for his shorted path Dijkstra algorithm. The banker algorithm as the name suggests is made to solve a banking problem of granting loans to the customer. The algorithm has to check whether the amount retained in the account of the customer should be more than a granting benchmark, then only the algorithm should initiate the loan allocation. This way the algorithm initiates the resources allocation only when all the conditions are met and the choice is the safest one for the process to begin.


Deadlock detection and recovery

The deadlock detection and recovery is a complicated method wherein you have to create a program that can detect what is responsible for the deadlock for example some processes causing deadlock or resources are allocated circularly. This way when you have detected the deadlock then you have to completely shut down or stop the process or preempt the resource which will the method of recovery from the deadlock and this part is one of the most complicated in this method in which you have to make the system for recovery because there can be many conditions involved. For example, there may be two or more processes creating the deadlock then you can try stopping the one process and then check whether the deadlock is removed and if not stop another to check now this can include the priority of the process and many other conditions to follow.


Among all the methods to avoid or prevent deadlock the most used in the real world is the deadlock avoidance ignorance where you simply ignore any deadlock condition and restart the system if the deadlock occurs. But the other strategies to handle the deadlock are also important to develop a program where the deadlock should not occur in any case like baking and finance where a lot of many are involved in such cases the deadlock has to be taken seriously.


Leave a Comment