Deadlock in Operating system | Explained

Deadlock in Operating system | Explained

 A deadlock is a state where all the processes in the deadlock are blocked by each other or waits for each other by holding the resources.



Let's see Why deadlock occurs, What are the conditions for deadlock, and how to prevent it.

What is Deadlock?

In an operating system, resources might not be unlimited or can be used by one process at a time. To increase the speed of the process some hold the resources before getting the CPU cycles and also need some other process that is in execution. But the Tragic change happens here what if the process in the execution also needs some process that was kept hold by the previous process. Here we can see a lock which is made between the process of how they wait for each other and the CPU stills idle. This process can sometimes lead to serious issues when they are stuck infinitely holding the resources which may lead to the waiting for other processes also. A classic example can be seen in real life like road traffic where vehicles are coming in from both sides but when vehicles come from either side simultaneously then both vehicles will be stuck on the road and also block other vehicles.




Nowadays operating systems are capable of storing multiple processes in the main memory. So the processes hold their resources before even getting CPU cycles which can cause a Deadlock. And also if there is no way to preempt the process solving a deadlock becomes harder. Let's see the necessary condition by which we can say whether a system is in a deadlock or not. There are 4 ways to recognize this.

1. Mutal Exclusion

Mutual Exclusion means the execution of one process depends on the state of the other process and the number of resources that it has kept under hold. Because if there are no such resources which we want and doesn't keep under hold by another process there is no need to wait for that process. If process P1 has taken hold of some resource then other processes cannot take that process because it is kept by P1. Any resource should be kept hold by only one process unless it is only a single instant But the process can hold multiple resources. This is known as mutual exclusion or Mutex.

2. Hold and wait

For A process need to execute it may require several resources. In hold and wait, every process can hold any number of the process which it needs for execution and can wait for the rest of the resources. For example, if process P1 holds resource R1 and can wait for R2 and another process P2 can hold R2 and wait for R1. This is known as hold and wait.




3. Non-Preemptive

As we discussed above process P1 is holding R1 and process P2 is holding R2 and waiting for R2 and R1 respectively. In this case, if process P2 wants a resource it cant take the resources from P1 or else, in other words, every process cannot take the resources which are allocated for other processes. Only when the process voluntarily releases its resources this is known as Non-preemptiveness.

4. Circular wait

While waiting for the process occurs circularly. for example, consider 3 processes and p1 is waiting for p2, p2 is waiting for p3, and p3 is waiting for p1. No one wants to give their resource to other process and every process is holding it resources. Other processes cant preempt the process and take the resource. So here all processes are waiting for each other and no one wants to compromise for the resources. This is known as Circular wait.

How to Handle Deadlock?

Sometimes deadlock can cause serious issues to the operating system. If we allocate CPU cycles in spending how to solve deadlock it will decrease the efficiency and throughput of the OS. Every system can have its own way to handle deadlock it may depend on system architecture. To remove deadlock, we should also kill some processes and rerun the process, and again takes a lot of time to rerun the execution. So there are 4 ways to handle a deadlock.

1. Prevention

Deadlock can happen if and only if all the four above-mentioned conditions are satisfied. So if we break any one of the conditions or even all the conditions we can recover from the deadlock. So we can make the process preempt from CPU or we can take the resources from the process which it holds or We can remove the hold and wait for condition by taking one required resources from others and giving one of the resources to break the loop or if resources can be allocated to multiple processes. The best approach is to Preempt the process or take the resources from the process.

2. Ignorance

This is will be interesting to know that Most Operating systems like Windows or Linux use this method of handling deadlock. In an operating system, the main objective is to improve the performance so it decreases the process which wastes the CPU cycles. Handling deadlock requires CPU cycles and increases the execution time so it is needed to resolve this issue by just acting as if there is no deadlock and deadlock occurs in OS very rarely so there is no need to write the code or method to solve a Deadlock. This is also known as the ostrich algorithm because when there is a sand storm ostrich just keeps its head inside the ground and pretends there is no sand storm. If there is a critical deadlock we cannot be solved even after ignoring it then OS will restart the system.

3. Avoidance

It's like taking measures for the issues which may happen in the future. Making the system perfect so that there will be no deadlock in the future. So the CPU runs in such a way that whenever it allocates a resource it checks whether the conditions are satisfying or else it will not allocate. Every time it ensures the system is in a safe state and the system cannot run into a deadlock. Bankers algorithm is the most used method to detect whether the state it is in can cause a deadlock. Bankers algorithm is checked before allocation of any resources to the process.

4. Detection and recovery

Even after the implementation of the above methods and a deadlock occurs we need to recover from it. The best way to recover from a deadlock is to terminate the process which is involved in the deadlock. It terminates processes one by one until the deadlock is resolved. But sometimes preempting a process may cause some issues and also wastage of CPU cycles so there is another way to handle this. We can only preempt the resources which are causing the deadlock. We should also detect the deadlock very soon or else it may cause problems to other processes.

Don't forget to comment about the article in the comment section.

Post a Comment

Previous Post Next Post