Operating System - Handling DeadLock



Read this chapter to get an overview of the four different ways that an operating system can employ to handle a deadlock, when it encounters one.

Methods to Handle a Deadlock in OS

To handle deadlock, an operating system can use one of the following methods −

Deadlock Prevention

Deadlock Prevention is a method where the operating system should ensure that at least one of the four necessary conditions for deadlock is not allowed to occur. Following are those conditions −

  • Mutual Exclusion − At least one resource must be held in a non-shareable mode.
  • Hold and Wait − A process holding at least one resource is waiting get more resources.
  • No Preemption − Resources cannot be forcefully taken away from processes holding them.
  • Circular Wait − A set of processes exist such that each process is waiting for a resource that another process hold in a circular chain.

The deadlock will happen only if all the above four conditions hold simultaneously. So, to prevent deadlock, the operating system can ensure that at least one of these conditions is never going to happen.

Following are the types of operating system that prefer deadlock prevention to handle deadlock −

  • Real-time operating systems
  • Embedded operating systems
  • Flight control systems

Deadlock Avoidance

Deadlock Avoidance is a method handling deadlock where the operating system uses special algorithms to analyze the available resource and process states to ensure that a deadlock will never occur. To do this, the system must have additional information about how resources are to be requested.

One of the most commonly used algorithms for deadlock avoidance is the Banker's Algorithm. This is similar to the way a banker would allocate money to customers. The banker must ensure that there is enough money in the bank to satisfy the maximum possible requests of all customers. Similarly, before allocating a resources to a process, operating system should calculate and determine that the allocation will not lead the system into an unsafe state.

Following are the types of operating system that prefer deadlock avoidance to handle deadlock −

  • Multi-user operating systems
  • Time-sharing operating systems
  • General-purpose operating systems

Deadlock Detection and Recovery

This is another method to handle deadlock. Here, the operating system does not try to prevent or avoid deadlock, instead it start acting only after a deadlock has occurred. At regular intervals, the operating system checks for deadlock using a deadlock detection algorithm. If a deadlock is detected, the system takes action to recover from the deadlock.

There are two main approaches to recover from deadlock −

  • Process Termination − This means a process that involved in the deadlock is terminated permanently. So the resources held by the process are free now. Now, the extra resources can be allocated to other processes to continue their execution. Sometimes, more than one process may need to be terminated to break the deadlock.
  • Resource Preemption − In this approach, resources are taken away from one or more processes involved in the deadlock and are given to other processes until the deadlock is resolved. After some time, the resources may be returned to the original process and it can continue its execution.

The deadlock detection and recovery method is generally used in operating systems where deadlocks are expected to occur frequently and the cost of preventing or avoiding deadlock is higher than the cost of detecting and recovering from it. Examples of such operating systems include DBMS systems and transaction processing systems.

Ignoring Deadlock

This is a method used by most of the modern computer operating systems like Windows and Linux. Here, the operating system simply ignores deadlock problem. The assumption is that deadlocks are very rare and the cost of dealing with them is higher than the cost of ignoring them.

In this approach, the operating system does not implement any specific mechanisms to prevent, avoid, or detect deadlocks. You might have seen occasional messages like "The program is not responding" or "The application has stopped working" in your computer. These messages may indicate that a deadlock has occurred. You can forcefully close the program or restart your computer to resolve the issue.

Conclusion

A deadlock can lead to unexpected system behavior and resource wastage. We have discussed four different methods to handle deadlock in an operating system - Deadlock Prevention, Deadlock Avoidance, Deadlock Detection and Recovery, and Ignoring Deadlock.

Depending on the purpose, the operating system decides which method to use for deadlock handling. For example, an operating system of an aviation system may use deadlock prevention or avoidance to ensure no deadlock occurs at any moment. On the other hand, the operating system of a personal computer may ignore deadlock as it is less likely to happen and the cost of handling it may be higher.

Advertisements