Introduction of Deadlock in Operating System - GeeksforGeeks

Introduction of Deadlock in Operating System

Last Updated : 15 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report
 

A process in operating system uses resources in the following way. 

  1. Requests a resource 
  2. Use the resource 
  3. Releases the resource 

A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. 

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. A similar 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. 

deadlock

 Examples Of Deadlock

  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.  

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. 

Methods for handling deadlock 
There are three ways to handle deadlock 
1) Deadlock prevention or avoidance:

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:

       1. Eliminate mutual exclusion                                        3. Allow preemption

       2. Solve hold and Wait                                                   4. Circular wait Solution           

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.

2) Deadlock detection and 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.

Recovery from Deadlock

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.

Recovery from Deadlock: Process Termination:

1. 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.

2. 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)

Recovery from Deadlock: Resource Preemption:

1. 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.

2. 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.

3. 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.

3) 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.
     

Exercise: 
  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) 
 

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

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

See QUIZ ON DEADLOCK for more questions. 

 



Previous Article
Next Article

Similar Reads

Difference between Deadlock Prevention and Deadlock Avoidance
1. Deadlock Prevention : Deadlock prevention means to block at least one of the four conditions required for deadlock to occur. If we are able to block any one of them then deadlock can be prevented. The four conditions which need to be blocked are:- Mutual ExclusionHold and WaitNo PreemptionCircular Wait Spooling and non-blocking synchronization a
2 min read
Program for Deadlock free condition in Operating System
Given: A system has R identical resources, P processes competing for them and N is the maximum need of each process. The task is to find the minimum number of Resources required So that deadlock will never occur. Deadlock prevention is an important technique used by operating systems to avoid the occurrence of deadlocks. Here is an example program
6 min read
Recovery from Deadlock in Operating System
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
7 min read
Conditions for Deadlock in Operating System
A Deadlock is a situation that involves the interaction of more than one resource and process with each other. We can visualize the occurrence of deadlock as a situation where there are two people on a staircase. Disadvantages in DeadlockDeadlock is an infinite Process it means that once a process goes into deadlock it will never come out of the lo
7 min read
Deadlock Ignorance in Operating System
In this article we will study in brief about what is Deadlock followed by Deadlock Ignorance in Operating System. What is Deadlock? If each process in the set of processes is waiting for an event that only another process in the set can cause it is actually referred as called Deadlock. In other words, one event which has to happen by one process wi
5 min read
Deadlock Detection Algorithm in Operating System
Deadlock Avoidance Algorithm/ Bankers Algorithm:  What is a deadlock detection algorithm in operating systems?A deadlock detection algorithm is a technique used by an operating system to identify deadlocks in the system. This algorithm checks the status of processes and resources to determine whether any deadlock has occurred and takes appropriate
6 min read
Operating Systems | Deadlock | Question 1
Suppose n processes, P1, …. Pn share 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? (A) A (B) B (C) C (D) D Answer: (C) Explanation: See Question 4 on foll
1 min read
Operating Systems | Deadlock | Question 2
Consider the following snapshot of a system running n processes. Process i is holding Xi instances of a resource R, 1 <= i <= n. currently, all instances of R are occupied. Further, for all i, process i has placed a request for an additional Yi instances while holding the Xi instances it already has. There are exactly two processes p and q su
1 min read
Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS
Deadlock occurs when each transaction T in a schedule of two or more transactions waiting for some item locked by some other transaction T' in the set. Thus, both end up in a deadlock situation, waiting for the other to release the lock on the item. Deadlocks are a common problem and we have introduced the problem while solving the Concurrency Cont
7 min read
Deadlock System model
Overview :A deadlock occurs when a set of processes is stalled because each process is holding a resource and waiting for another process to acquire another resource. In the diagram below, for example, Process 1 is holding Resource 1 while Process 2 acquires Resource 2, and Process 2 is waiting for Resource 1. System Model : For the purposes of dea
8 min read
Article Tags :