Open In App

Recovery from Deadlock in Operating System

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In today’s world of computer systems and multitasking environments, deadlock is an undesirable situation that can bring operations to a grinding halt. When multiple processes compete for exclusive access to resources and end up in a circular waiting pattern, a deadlock occurs. To maintain the smooth functioning of an operating system, it is crucial to implement recovery mechanisms that can break these deadlocks and restore the system’s productivity.

“Recovery from Deadlock in Operating Systems” refers to the set of techniques and algorithms designed to detect, resolve, or mitigate deadlock situations. These methods ensure that the system can continue processing tasks efficiently without being trapped in an eternal standstill. Let’s take a closer look at some of the key strategies employed.

There is no mechanism implemented by the OS to avoid or prevent deadlocks. The system, therefore, assumes that a deadlock will undoubtedly occur. The OS periodically checks the system for any deadlocks in an effort to break them. The OS will use various recovery techniques to restore the system if it encounters any deadlocks.

When a Deadlock Detection Algorithm determines that a deadlock has occurred in the system, the system must recover from that deadlock.

Approaches To Breaking a Deadlock

Process Termination

To eliminate the deadlock, we can simply kill one or more processes. For this, we use two methods: 

  1. Abort all the Deadlocked Processes: Aborting all the processes will certainly break the deadlock but at a great expense. The deadlocked processes may have been computed for a long time, and the result of those partial computations must be discarded and there is a probability of recalculating them later. 
     
  2.  Abort one process at a time until the deadlock is eliminated: Abort one deadlocked process at a time, until the deadlock cycle is eliminated from the system. Due to this method, there may be considerable overhead, because, after aborting each process, we have to run a deadlock detection algorithm to check whether any processes are still deadlocked. 

Advantages of Process Termination

  • It is a simple method for breaking a deadlock.
  • It ensures that the deadlock will be resolved quickly, as all processes involved in the deadlock are terminated simultaneously.
  • It frees up resources that were being used by the deadlocked processes, making those resources available for other processes.

Disadvantages of Process Termination

  • It can result in the loss of data and other resources that were being used by the terminated processes.
  • It may cause further problems in the system if the terminated processes were critical to the system’s operation.
  • It may result in a waste of resources, as the terminated processes may have already completed a significant amount of work before being terminated.

For Process

  1. Destroy a process: Although killing a process can solve our problem, choosing which process to kill is more important. The operating system typically terminates a process after it has completed the least amount of work.
  2. End all processes: Although not suggestible, this strategy can be used if the issue worsens significantly. Because each process will have to start from scratch after being killed, the system will become inefficient. 

Resource Preemption

To eliminate deadlocks using resource preemption, we preempt some resources from processes and give those resources to other processes. This method will raise three issues – 

  1. Selecting a victim: We must determine which resources and which processes are to be preempted and also in order to minimize the cost. 
     
  2. Rollback: We must determine what should be done with the process from which resources are preempted. One simple idea is total rollback. That means aborting the process and restarting it. 
     
  3. Starvation: In a system, it may happen that the same process is always picked as a victim. As a result, that process will never complete its designated task. This situation is called Starvation and must be avoided. One solution is that a process must be picked as a victim only a finite number of times. 
     

Advantages of Resource Preemption

  1. It can help in breaking a deadlock without terminating any processes, thus preserving data and resources.
  2. It is more efficient than process termination as it targets only the resources that are causing the deadlock.
  3. It can potentially avoid the need for restarting the system.

Disadvantages of Resource Preemption

  1. It may lead to increased overhead due to the need for determining which resources and processes should be preempted.
  2. It may cause further problems if the preempted resources were critical to the system’s operation.
  3. It may cause delays in the completion of processes if resources are frequently preempted.

Resource Allocation Graph (RAG)

The resource allocation graph (RAG) is a popular technique for computer system deadlock detection. The RAG is a visual representation of the processes holding the resources and their current state of allocation. The resources and processes are represented by the graph’s nodes, while their allocation relationships are shown by the graph’s edges. A cycle in the graph of the RAG method denotes the presence of a deadlock. When a cycle is discovered, at least one resource needed by another process in the cycle is being held by each process in the cycle, causing a deadlock. The RAG method is a crucial tool in contemporary operating systems due to its high efficiency and ability to spot deadlocks quickly.

RAG

Priority Inversion

A technique for breaking deadlocks in real-time systems is called priority inversion. This approach alters the order of the processes to prevent stalemates. A higher priority is given to the process that already has the needed resources, and a lower priority is given to the process that is still awaiting them. The inversion of priorities that can result from this approach can impair system performance and cause performance issues. Additionally, because higher-priority processes may continue to take precedence over lower-priority processes, this approach may starve lower-priority processes of resources.

RollBack

In database systems, rolling back is a common technique for breaking deadlocks. When using this technique, the system reverses the transactions of the involved processes to a time before the deadlock. The system must keep a log of all transactions and the system’s condition at various points in time in order to use this method. The transactions can then be rolled back to the initial state and executed again by the system. This approach may result in significant delays in the transactions’ execution and data loss.

Frequently Asked Questions

Q.1: How to detect if there is a deadlock in an operating system? 

Answer: 

There are several approaches to detecting deadlocks in an operating system:

  1. Resource Allocation Graph (RAG): This method involves constructing a graph that represents the resources and processes, and then analyzing the graph to check for cycles.
  2. Deadlock Detection Algorithm: This algorithm periodically checks the resource allocation status and resource requests to identify circular wait conditions.
  3. Banker’s Algorithm: This algorithm uses a mathematical model to determine if a state is safe or unsafe, considering the maximum resource need and available resources.

Q.2: What is the resource allocation graph (RAG) in the context of deadlocks? 

Answer: 

A resource allocation graph (RAG) is a graphical representation of resources and processes in an operating system. It consists of nodes representing processes and resources, and directed edges representing resource requests and allocations. A cycle in the graph indicates the presence of a potential deadlock.

Q.3: What are the necessary conditions for a deadlock to occur? 

Answer: 

Four necessary conditions, known as the Coffman conditions, must hold simultaneously for a deadlock to occur:

  1. Mutual Exclusion: At least one resource must be held in a non-sharable mode, meaning only one process can use it at a time.
  2. Hold and Wait: A process must be holding at least one resource while waiting to acquire additional resources.
  3. No Preemption: Resources cannot be forcibly taken away from a process; they must be released voluntarily.
  4. Circular Wait: A circular chain of two or more processes exists, where each process is waiting for a resource held by the next process in the chain.


Last Updated : 28 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads