Table of contents
1.
Introduction
2.
What is a deadlock?
3.
Necessary Conditions for deadlock
3.1.
Mutual Exclusion
3.2.
No Preemption
3.3.
Hold and wait
3.4.
Circular wait
3.5.
Resource Utilization
4.
Methods to handle deadlocks
4.1.
Deadlock ignorance(Ostrich Method)
4.2.
Deadlock prevention
4.3.
Deadlock Avoidance
4.4.
Deadlock detection and Recovery
5.
Advantages and Disadvantages of using the methods to handle deadlocks
5.1.
Advantages
5.2.
Disadvantages
6.
Frequently Asked Questions
6.1.
What causes deadlock?
6.2.
How do you avoid “hold and wait” conditions?
6.3.
What is the Ostrich Algorithm?
6.4.
How can a system recover from deadlocks?
6.5.
What is the difference between Deadlock and Starvation?
7.
Conclusion
Last Updated: Mar 27, 2024

Deadlocks In OS

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas!! Got stuck with the word deadlock? Don't worry; Coding Ninjas got you covered. As the article will start with what a deadlock is, followed by the necessary conditions for a deadlock to occur and how can a deadlock can be handled.

Imagine that you and your friend are doing a project together. You have different understandings of the topic and cannot move forward with the project as your opinions are different, so there will be a deadlock because the process of doing the project cannot proceed further.

Let's proceed with this in more detail.

Also Read About, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

What is a deadlock?

A deadlock in an operating system is a condition where two or more processes are waiting for some event, but the event does not occur; in this case, those two processes are stuck in a deadlock state.

Deadlock occurs when a process cannot change its state indefinitely due to other processes using the resources it requests. Technically, there is no synchronization between the processes.

Consider two processes, Process_1 and Process_2. At the current moment, process_1 is waiting for Resource_2 while holding Resource_1. 

While this is happening, Process_2 has been allocated to the resource Resource_2 and is waiting for Resource_1. 

 

Deadlock Occurence

Fig. 1

As a result, Process_P1 awaits the release of its resource while Process_2 waits for Resource_1 allocated to Process_1 for release. 

However, no resource is released. Therefore, both are waiting for each other to release the resource. There is no work done here because neither process releases the resource. This is referred to as a deadlock.

Learn about Demand Paging in OS click here.

FCFS Scheduling

Necessary Conditions for deadlock

There are four necessary conditions for a deadlock.

Mutual Exclusion

Any resources you are using should be used mutually exclusively, meaning both processes cannot use the same resource simultaneously. 

The short form of Mutual Exclusion is Mutex, which is a binary semaphore that is a type of communication method that manages access to shared resources. It uses inheritance to avoid the problem of extended priority inversion. By applying this method, tasks with higher priority can be blocked as long as possible.

Deadlocks never occur with shared resources like read-only files but with exclusive access to resources like tape drives and printers.

Exclusive access of resource

Fig.2

No Preemption

  • When a process completes its task, it can release a resource voluntarily. A process that holds some resources requests another that cannot be allocated to it immediately and, in that case, all resources will become available.
  • There is a process that is waiting for the list of preempted resources. If the process can recover its old resources and request a new one, it will be restarted.
  • Whenever another resource is available, it is given to the process requesting it. We release it and give it to the requesting process if it was being held by another process waiting for another resource.

 

Hold and wait

When this condition exists, the process must be stopped if it simultaneously holds one or more resources while waiting for another resource. In the below diagram, the Process_1 currently holding the Resource_1 is waiting for additional Resources_2.

Hold and Wait

Fig.3

Circular wait

In Fig 1, a Circular wait is occurring. It is a condition when the first process awaits a resource held by the second process, which waits for the resource held by the third process, etc. In the end, The last process is waiting for the resource that the first process holds. Therefore, every process is waiting for the other processes to release the resources, and no one is releasing them. But no one is releasing any resources. This is known as a circular wait.

Resource Utilization

Resources are utilized in the following ways:

  • Request: If the requested resources are unavailable, the process must wait until they become available.
  • Use: During the process, resources are used.
  • Release: By releasing the resource, the process relinquishes control. As a result, other processes can use it.

 

Note: For more details regarding the conditions of a deadlock, you must visit the Guided Path on Coding Ninjas Studio.

Methods to handle deadlocks

Various methods to handle deadlocks are:

Deadlock ignorance(Ostrich Method)

In this method, it is assumed that there will never be a deadlock. It is best suited to single-user systems where users merely browse the internet and do regular tasks. Most Windows and Linux users use this approach.

Deadlock prevention

When Mutual Exclusion, hold and wait, and no preemption all have simultaneously, a deadlock occurs. There can never be a deadlock in a system if one of the four conditions can be violated at any time.

Deadlock Avoidance

During deadlock avoidance, the operating system tests each step it performs to see if the system is in a safe or unsafe state. Once a safe state is reached, the process continues. The OS must take one step back when the system is dangerous. One of the methods for deadlock avoidance is the Deadlock Avoidance Algorithm which uses Banker's algorithm to avoid a deadlock state. 

Deadlock detection and Recovery

Processes can get stuck in a deadlock with this approach and then periodically check if a deadlock has occurred in the system. A deadlock can be removed by applying recovery methods to the system.
 

Must Read Process Management in OS

Advantages and Disadvantages of using the methods to handle deadlocks

Advantages

  • It does not require preemption.
  • This is a convenient way to save and restore the state of resources whose state can be easily preserved and restored.
  • It can also be feasible at compile-time checks.
  • It does not require run-time computation as the problem is resolved during the design phase.

Disadvantages

  • Initiation of the process is delayed.
  • Future resource requirements should be known.
  • It frequently preempts the situation.
  • Don't allow incremental resource requests.
  • Inherent preemption losses.

Also see, Open Source Operating System, Lock based protocol in DBMS

Frequently Asked Questions

What causes deadlock?

Two processes attempt to access a resource and cannot do so because the other prevents them from obtaining exclusive access to it. The two processes are thus unable to proceed. Deadlocks can only be broken by terminating one of the processes.

How do you avoid “hold and wait” conditions?

To avoid a "hold and wait" situation, a process should request all its required resources simultaneously and be blocked until all requests are granted simultaneously.

What is the Ostrich Algorithm?

The strategy is to ignore potential problems by claiming they are extremely rare. It is named after the ostrich effect, which means "sticking one's head in the sand and pretending there is no problem."

How can a system recover from deadlocks?

Resource Preemption: The basic principle of preemption is to remove deadlocks by sequentially transferring resources from one process to another until the deadlock cycle is broken.
Three issues that need to be addressed are:
1) Selecting a victim
2) Rollback
3) Starvation
Process Termination: By calling exit() on the operating system, a process ends after executing its final statement and requesting deletion from the OS. When this occurs, its parent process may receive a status value from the child process (via the wait() system call). Operating systems deallocate all of the resources -such as physical memory, virtual memory, open files, and buffers -of a process.the 

What is the difference between Deadlock and Starvation?

Deadlock

Starvation

This occurs when a process is blocked.  A process with a high priority is allowed to run while a low priority process is blocked. 
It suffers from starvation. Not every starvation suffers from a deadlock.
It is an infinite process.  It is not an infinite process.
After a deadlock, there might be the mutual exclusion and hold and wait. Preemption and circular wait do not take place simultaneously. This is due to uncontrolled priorities and resource management.

 

Conclusion

Operating systems often experience deadlocks. Various operating systems may ignore them using the Ostrich approach, while others may handle them differently. 

Having a deadlock that involves only one process is impossible. The Banker's algorithm, the safety algorithm, the Detection algorithm, deadlock ignorance, etc., are all used to resolve this deadlock condition. This will prevent future deadlocks by allocating resources differently.

So its time for you now to refer to other articles based on a similar topic, i.e., deadlock in operating system-

 

We hope that this blog has helped you enhance your knowledge regarding deadlocks. With this fantastic operating system course, test series, and problems available on Coding Ninjas Studio, you can make learning more enjoyable and stress-free. Are you planning to ace the interviews of reputed product-based companies like Amazon, Microsoft, Google, and more? Attempt our mock test series and participate in the contests hosted on Coding Ninjas Studio now!

Do upvote our blog to help other ninjas grow. Good luck with your preparation!

Keep Coding!
Live masterclass