What are Deadlock handling techniques in Operating System?
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:
- Mutual Exclusion
- Hold and Wait
- No Preemption
- Circular Wait
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.
1. Deadlock Prevention
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.
- Mutual Exclusion: Mutual exclusion says that a resource can only be held by one process at a time. If another process is also demanding the same resource then it has to wait for the allocation of that resource. So, practically, we can't violate the mutual exclusion for a process because in general, one resource can perform the work of one process at a time. For example, a printer can't print documents of two users at the same time.
- Hold and Wait: Hold and wait arises when a process holds some resources and is waiting for some other resources that are being held by some other waiting process. To avoid this, the process can acquire all the resources that it needs, before starting its execution and after that, it starts its execution. In this way, the process need not wait for some resources during its execution. But this method is not practical because we can't know the resources required by a process in advance, before its execution. So, another way of avoiding hold and wait can be the "Do not hold" technique. For example, if the process needs 10 resources R1, R2, R3,...., R10. At a particular time, we can provide R1, R2, R3, and R4. After performing the jobs on these resources, the process needs to release these resources and then the other resources will be provided to the process. In this way, we can avoid the hold and wait condition.
- No Preemption: This is a technique in which a process can't forcefully take the resource of other processes. But if we found some resource due to which, deadlock is happening in the system, then we can forcefully preempt that resource from the process that is holding that resource. By doing so, we can remove the deadlock but there are certain things that should be kept in mind before using this forcefull approach. If the process is having a very high priority or the process is a system process, then only the process can forcefully preempt the resources of other processes. Also, try to preempt the resources of those process which are in the waiting state.
- Circular Wait: Circular wait is a condition in which the first process is waiting for the resource held by the second process, the second process is waiting for the resource held by the third process and so on. At last, the last process is waiting for the resource held by the first process. So, every process is waiting for each other to release the resource. This is called a circular wait. To avoid this, what we can do is, we can list the number of resources required by a process and we assign some number or priority to each resource(in our case, we are using R1, R2, R3, and so on). Now, the process will take the resources in the ascending order. For example, if the process P1 and P2, requires resource R1 and R2, then initially, both the process will demand the resource R1 and only one of them will get resource R1 at that time and the other process have to wait for its turn. So, in this way, both the process will not be waiting for each other. One of them will be executing and the other will wait for its turn. So, there is no circular wait here.
2. Deadlock Avoidance
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.
3. Detection and Recovery
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.
4. Deadlock Ignorance
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 :)