Introduction of Deadlock in Operating System - GeeksforGeeks (2024)

A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process.In this article, we will discuss deadlock, its necessary conditions, etc. in detail.

What is Deadlock?

Deadlock is a situation in computing where two or more processes are unable to proceed because each is waiting for the other to release resources. Key concepts include mutual exclusion, resource holding, circular wait, and no preemption.

Consider an example when two trains are coming toward each other on the same track and there is only one track, none of the trains can move once they are in front of each other. This is a practical example of deadlock.

How Does Deadlock occur in the Operating System?

Before going into detail about how deadlock occurs in the Operating System, let’s first discuss how the Operating System uses the resources present. A process in an operating system uses resources in the following way.

  • Requests a resource
  • Use the resource
  • Releases the resource

A situation occurs in operating systems when there are two or more processes that hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.

Introduction of Deadlock in Operating System - GeeksforGeeks (1)

Examples of Deadlock

There are several examples of deadlock. Someof them are mentioned below.

1. The system has 2 tape drives. P0 and P1 each hold one tape drive and each needs another one.

2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as follows:

  • P0 executes wait(A) and preempts.
  • P1 executes wait(B).
  • Now P0 and P1 enter in deadlock.

P0

P1

wait(A);

wait(B)

wait(B);

wait(A)

3. Assume the space is available for allocation of 200K bytes, and the following sequence of events occurs.

P0

P1

Request 80KB;

Request 70KB;

Request 60KB;

Request 80KB;

Deadlock occurs if both processes progress to their second request.

Necessary Conditions for Deadlock in OS

Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)

  • Mutual Exclusion: Two or more resources are non-shareable (Only one process can use at a time).
  • Hold and Wait: A process is holding at least one resource and waiting for resources.
  • No Preemption: A resource cannot be taken from a process unless the process releases the resource.
  • Circular Wait: A set of processes waiting for each other in circular form.

What is Deadlock Detection?

Deadlock detection is a process in computing where the system checks if there are any sets of processes that are stuck waiting for each other indefinitely, preventing them from moving forward. In simple words, deadlock detection is the process of finding out whether any process are stuck in loop or not. There are several algorithms like

  • Resource Aloocation Graph
  • Banker’s Algorithm

These algorithms helps in detection of deadlock in Operating System.

What are the Methods For Handling Deadlock?

There are three ways to handle deadlock

  • Deadlock Prevention or Avoidance
  • Deadlock Recovery
  • Deadlock Ignorance

Deadlock Prevention or Avoidance

Deadlock Prevention and Avoidance is the one of the methods for handling deadlock. First, we will discuss Deadlock Prevention, then Deadlock Avoidance.

What is Deadlock Prevention?

The idea is to not let the system into a deadlock state. This system will make sure that above mentioned four conditions will not arise. These techniques are very costly so we use this in cases where our priority is making a system deadlock-free.
One can zoom into each category individually, Prevention is done by negating one of the above-mentioned necessary conditions for deadlock. Prevention can be done in four different ways:

  • Eliminate mutual exclusion
  • Solve Hold and Wait
  • Allow Preemption
  • Circular Wait Solution

What is Deadlock Avoidance?

Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to make an assumption. We need to ensure that all information about resources that the process will need is known to us before the execution of the process. We use Banker’s algorithm (Which is in turn a gift from Dijkstra) to avoid deadlock.

In prevention and avoidance, we get the correctness of data but performance decreases.

What is Deadlock Recovery?

If Deadlock prevention or avoidance is not applied to the software then we can handle this by deadlock detection and recovery. which consist of two phases:

  1. In the first phase, we examine the state of the process and check whether there is a deadlock or not in the system.
  2. If found deadlock in the first phase then we apply the algorithm for recovery of the deadlock.

In Deadlock detection and recovery, we get the correctness of data but performance decreases.

Methods of Deadlock Recovery

There are several Deadlock Recovery Techniques:

  • Manual Intervention
  • Automatic Recovery
  • Process Termination
  • Resource Preemption

1. Manual Intervention

When a deadlock is detected, one option is to inform the operator and let them handle the situation manually. While this approach allows for human judgment and decision-making, it can be time-consuming and may not be feasible in large-scale systems.

2. Automatic Recovery

An alternative approach is to enable the system to recover from deadlock automatically. This method involves breaking the deadlock cycle by either aborting processes or preempting resources. Let’s delve into these strategies in more detail.

3. Process Termination

  • Abort all Deadlocked Processes
    • This approach breaks the deadlock cycle, but it comes at a significant cost. The processes that were aborted may have executed for a considerable amount of time, resulting in the loss of partial computations. These computations may need to be recomputed later.
  • Abort one process at a time
    • Instead of aborting all deadlocked processes simultaneously, this strategy involves selectively aborting one process at a time until the deadlock cycle is eliminated. However, this incurs overhead as a deadlock-detection algorithm must be invoked after each process termination to determine if any processes are still deadlocked.
    • Factors for choosing the termination order:
      • The process’s priority
      • Completion time and the progress made so far
      • Resources consumed by the process
      • Resources required to complete the process
      • Number of processes to be terminated
      • Process type (interactive or batch)

4. Resource Preemption

  • Selecting a Victim
    • Resource preemption involves choosing which resources and processes should be preempted to break the deadlock. The selection order aims to minimize the overall cost of recovery. Factors considered for victim selection may include the number of resources held by a deadlocked process and the amount of time the process has consumed.
  • Rollback
    • If a resource is preempted from a process, the process cannot continue its normal execution as it lacks the required resource. Rolling back the process to a safe state and restarting it is a common approach. Determining a safe state can be challenging, leading to the use of total rollback, where the process is aborted and restarted from scratch.
  • Starvation Prevention
    • To prevent resource starvation, it is essential to ensure that the same process is not always chosen as a victim. If victim selection is solely based on cost factors, one process might repeatedly lose its resources and never complete its designated task. To address this, it is advisable to limit the number of times a process can be chosen as a victim, including the number of rollbacks in the cost factor.

What is Deadlock Ignorance?

If a deadlock is very rare, then let it happen and reboot the system. This is the approach that both Windows and UNIX take.we use the ostrich algorithm for deadlock ignorance.

In Deadlock, ignorance performance is better than the above two methods but the correctness of data is not there.

Safe State

A safe state can be defined as a state in which there is no deadlock. It is achievable if:

  • If a process needs an unavailable resource, it may wait until the same has been released by a process to which it has already been allocated. if such a sequence does not exist, it is an unsafe state.
  • All the requested resources are allocated to the process.

Aspect

Deadlock

Starvation

Definition

A condition where two or more processes are blocked forever, each waiting for a resource held by another.

A condition where a process is perpetually denied necessary resources, despite resources being available.

Resource Availability

Resources are held by processes involved in the deadlock.

Resources are available but are continuously allocated to other processes.

Cause

Circular dependency between processes, where each process is waiting for a resource from another.

Continuous preference or priority given to other processes, causing a process to wait indefinitely.

Resolution

Requires intervention, such as aborting processes or preempting resources to break the cycle.

Can be mitigated by adjusting scheduling policies to ensure fair resource allocation.

Questions for Practice

1. Suppose n processes, P1, …. Pn shares m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process Pi is Si, where Si > 0. Which one of the following is a sufficient condition for ensuring that deadlock does not occur? (GATE CS 2005)

Introduction of Deadlock in Operating System - GeeksforGeeks (2)

(A) A
(B) B
(C) C
(D) D

For the solution, see Question 4 of https://www.geeksforgeeks.org/operating-systems-set-16/

2. Which of the following is NOT a necessary condition for deadlock?

A) Mutual exclusion

B) Hold and wait

C) No preemption

D) Circular wait

Answer: C) No preemption

Frequently Asked Questions on Deadlock in OS – FAQs

How does the operating system handle deadlock?

Operating systems employ deadlock prevention, avoidance, or detection and recovery strategies to manage deadlock situations, such as resource allocation algorithms and deadlock detection algorithms.

Can deadlock occur in multi-threaded applications?

Yes, deadlock can occur in multi-threaded applications when threads within the same process or across different processes compete for resources in a way that leads to circular dependencies.

Can disk scheduling algorithms be used in SSDs (Solid State Drives)?

While SSDs have no moving parts and thus different characteristics than HDDs, scheduling algorithms can still help optimize the order of I/O operations to improve performance.



`; tags.map((tag)=>{ let tag_url = `videos/${getTermType(tag['term_id__term_type'])}/${tag['term_id__slug']}/`; tagContent+=``+ tag['term_id__term_name'] +``; }); tagContent+=`
Introduction of Deadlock in Operating System - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6128

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.