Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Methods for Handling Deadlock in Operating System

A deadlock occurs when a process or a collection of processes is stalled while waiting for a resource held by another waiting process. It is an unfavourable state of affairs. If a system encounters a deadlock, there are a few methods that can be used to handle it.

In this article, we will look more into the methods for handling deadlock in operating systems according to the GATE Syllabus for (Computer Science Engineering) CSE. Let us read ahead to find out more about it.

Table of Contents

What are the Methods for Handling Deadlock in an Operating System?

Dealing with deadlocks can be done in four ways. The following are the explanations:

1. Deadlock Prevention

Deadlock happens whenever four conditions occur at the same time. If one of the four conditions can be interrupted, we can prevent the system from becoming stuck in a deadlock.

This mechanism exists for a straightforward reason. To put it another way, we only need to fail in one of the requirements. It’s critical to avoid a given deadlock before it happens. As a result, the system verifies each and every transaction before executing it to ensure that it does not cause a deadlock. If a transaction has even the slightest chance of causing a deadlock, it is then not allowed to run at all. Read more on deadlock prevention here.

The following are some deadlock prevention strategies that make use of timestamps to ensure that a deadlock doesn’t occur:

1.1. Wait-Die Scheme

Here, in case a X1 transaction seeks a resource held by a X2 transaction, then one of these two events may occur under the wait-die scheme:

  • TS(X1) < TS(X2) – Here, in case X1 happens to be older than X2, that is, X1 entered the system before X2, it is permitted to wait for a resource that will become available once X2 has finished its execution.
  • TS(X1) > TS(X2) – Here, in case X1 happens to be younger than X2, that is, if X1 enters the system after the X2, X1 is killed. It’s then restarted with the very same timestamp later.

1.2 Wound-Wait Scheme

Here, in case a X1 transaction demands a resource held by a X2 transaction, then one of these two outcomes may occur in the wound-wait scheme:

  • TS(X1) TS(X2) – In case X1 happens to be older than X2, that is, X1 entered the system before X2, then X2 can be wound or rolled back. X1 then takes the resource and finishes the task. X2 is restarted with the very same timestamp as before.
  • TS(X1) > TS(X2) – In case X1 happens to be younger than X2, that is, if X1 enters the system after the X2, it is permitted to wait for a resource that will be available once X2 has finished its execution.

2. Deadlock Detection

The resource scheduler can detect deadlock because it maintains track of all the resources allotted to different processes. When a deadlock is found, it can be resolved using the methods provided.

  • All of the processes implicated in the deadlock are shut down. This strategy is ineffective since all of the processes’ progress is lost.
  • Some processes’ resources can be diverted to others till the deadlock condition is addressed.

We can check for deadlocks in the system on a regular basis in deadlock detection and recovery. But if there is a deadlock in our system, we use several techniques to break the deadlock. Deadlock detection is accomplished by using an algorithm that tracks cyclic waiting and kills one or more processes in order to break the deadlock. The state of the system is checked on a regular basis to see whether any processes are stuck. A deadlock is broken by aborting and restarting a process, releasing all resources held by the previous process.

When using the deadlock detection and recovery method:

  • There would be no restriction on resource access or process execution.
  • When possible, processes are given requested resources.
  • It never delays the start of the process and makes online handling simple.
  • Pre-emption losses are an inherent disadvantage.

3. Deadlock Avoidance

It is another method for dealing with a deadlock. The OS verifies/examines the system state in this manner, which means it checks whether the system is in a safe or hazardous condition at each step. This procedure is repeated until the system is in safe mode. In the event that the system becomes dangerous, the operating system will take a step backwards.

In simpler words, the OS verifies each allocation to ensure that the system does not experience a deadlock. It is preferable to avoid a given deadlock than to take steps once one has occurred. Here, the wait for graph could be utilised to prevent deadlock. Deadlock avoidance is only effective for smaller DBs since larger databases can get rather complex.

It necessitates an understanding of future process requests. There are two methods for avoiding deadlock:

  • Denial of process initiation
  • Denial of resource allocation

Wait for Graph

The link between transactions and resources is depicted by the wait for graph. A transaction is displayed in the form of an edge on this wait for graph in case it wants a resource or if it already has one. There could be a deadlock in a system if the wait for graph comprises a cycle; otherwise, there isn’t.

methods-for-handling-deadlock-in-operating-system

4. Ostrich Algorithm/Deadlock Ignorance

Because of the ostrich method, the deadlock would be simply disregarded, and it is thought that it would never happen. This is usually done because, in certain systems, the overall cost of dealing with a deadlock is substantially higher than the cost of simply ignoring it because it happens so rarely. As a result, it is simply presumed that the deadlock would never happen, and the system would be rebooted if it did.

The most common method for dealing with deadlocks is ignorance. This can be used in a variety of operating systems for end-users. The OS assumes that a deadlock will not occur and ignores it via the deadlock ignorance mechanism.

One of the appropriate strategies for a single end-user system wherein the user utilises the resources for surfing and other routine tasks is deadlock ignorance. In this case, the user merely needs to restart the system in the event of a deadlock, and this strategy is commonly used in Linux and Windows operating systems.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

 

Also Explore,