AfterAcademy Tech
•
08 Nov 2019

In our last blog, we saw what is Deadlock and what are the four necessary conditions of Deadlock. If you haven't read that blog then please read the blog from here and then come back to this blog.
So, you know what Deadlock is and what are those four necessary conditions. Cool. The four conditions of deadlock are:
To remove deadlock from our system, we need to avoid any one of the above four conditions of deadlock. So, there are various ways of deadlock handling. Let's see all of them, one by one.
In this method, the system will prevent any deadlock condition to happen i.e. the system will make sure that at least one of the four conditions of the deadlock will be violated. Since we are preventing any one of four conditions to happen by applying some techniques. These techniques can be very costly. So, you should apply deadlock prevention in only those situation which has a drastic change in the system if deadlock happens.
For example, in hospitals, we have generators or inverters installed. So that in case of a power cut, no life-saving machines should stop otherwise it can lead to the death of a patient. There can be chances that in the area of the hospital, the power cut happens rarely. But since it is a case of life-death, then you must Prevent this by installing generators or inverters. No doubt, you have to bear the cost of generators. Now, think of other situation, if there is a temple in the same area, then you need not install generators because here we are not dealing with some life-death situation and the power cut in the area is also very rare. So, prevention technique should be applied only when there will be a drastic change if deadlock happens. So, before using the deadlock prevention mechanism, make sure that if deadlock happens in your system then it will have an adverse effect on your system or not.
Let's see how we can avoid the four conditions of deadlock by using the deadlock prevention technique.
In the deadlock avoidance technique, we try to avoid deadlock to happen in our system. Here, the system wants to be in a safe state always. So, the system maintains a set of data and using that data it decides whether a new request should be entertained or not. If the system is going into the bad state by taking that new request, then the system will avoid those kinds of request and will ignore the request. So, if a request is made for a resource, from a system, then that request should only be approved if the resulting state of the system is safe i.e. not going into deadlock.
In this approach, the CPU assumes that at some point of time, a deadlock will happen in the system and after that, the CPU will apply some recovery technique to get rid of that deadlock. The CPU periodically checks for the deadlock. The Resource Allocation Graphs are used to detect the deadlock in a system.
For recovery, the CPU may forcefully take the resource allocated to some process and give it to some other process but that process should be of high priority or that process must be a system process.
In most of the systems, deadlock happens rarely. So, why to apply so many detection and recovery techniques or why to apply some method to prevent deadlock? As these processes of deadlock prevention are costly, so, the Operating System assumes that the deadlock is never going to happen. It simply ignores the deadlock. This is the most widely used methods of deadlock handling.
We have to compromise between correctness and performance. In the above three methods, the correctness is good but the performance of the system is low because the CPU has to check for deadlock after a regular interval. But if we ignore the deadlock then there might be cases where deadlock can happen but that is rare of the rarest case. We can simply restart the system and get rid of deadlock if some deadlock happens in our system. But at the same time, you will lose your data that is not being saved.
So, you have to think that you want correctness or performance. If you want performance, then your system should ignore deadlock otherwise you can apply some deadlock prevention technique. It totally depends on the need of the situation. If your system is dealing with some very very important data and you can't lose that if deadlock happens then you should definitely go for deadlock prevention.
That's it for this blog. Hope you enjoyed this blog. Do share this blog with your friends to spread the knowledge.
Visit our YouTube channel for more content.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn what an Operating System is and what are the goals of an Operating System. We will also learn the functionalities of an Operating System that helps in achieving the goal of the OS.

AfterAcademy Tech
In this blog, we will learn what is a deadlock situation, what are various deadlock handling techniques like deadlock prevention, deadlock avoidance, deadlock detection, and deadlock ignorance.

AfterAcademy Tech
In this blog, we'll learn about the Network Operating System and its various features. We'll also see the types of Network Operating System, their advantages, and disadvantages.

AfterAcademy Tech
In this blog, we will learn what is Spooling in Operating System and what are the advantages of using Spooling. We will also see how we can use Spooling to imporve the performance of the system.
