DEV Community

Cover image for Deadlocks in Operating Systems
Aryan Dev Shourie
Aryan Dev Shourie

Posted on • Updated on

Deadlocks in Operating Systems

An Operating System is defined as a program that, after being initially loaded into the computer by a boot program, manages all of the other application programs in a computer.
The Operating System basically acts as an interface between the computer user and the computer hardware, and controls the execution of programs.

In this article, I will explain in detail about the Deadlocks in Operating System.

WHAT ARE DEADLOCKS?

When a process executes in an Operating System, it uses the resources in the following way -

  • Requests a resource

  • Use the resource

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

deadlock

To define Deadlock in an Operating System, it is a situation where two or more processes or threads are unable to proceed because each process is waiting for the other to release a resource or terminate.

EXAMPLE OF A DEADLOCK

There are two Processes - Process 1 and Process 2.
There are two Resources - Resource 1 and Resource 2.

  1. The Resource 1 is assigned to the Process 1 and the Resource 2 is assigned to the Process 2.
  2. The Process 1 is waiting for the Resource 2 which is acquired by Process 2 and the Process 2 is waiting for the Resource 1 which is acquired by the Process 1.

Refer to the following diagram to understand the condition -

deadlock example

In this above situation, the requirements of both Process 1 and Process 2 are not being fulfilled, thus this is defined as the condition of Deadlock.

NECESSARY CONDITIONS FOR DEADLOCK TO OCCUR (COFFMAN CONDITIONS)

A Deadlock can arise if the following four conditions occur simultanoeusly, these conditions were first introduced by Edward G. Coffman -

  • Mutual Exclusion - It means that two or more resources are non-shareable.

  • Hold and Wait - A process is holding atleast one resource and waiting for other 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.

conditions

DEADLOCK HANDLING

It is absolutely necessary to handle the condition of a Deadlock, otherwise it can deteriorate your system and hamper the execution of multiple processes.

Deadlock Handling refers to the strategies and techniques employed to prevent, detect and resolve Deadlocks in Operating Systems.

There are multiple techniques and strategies which can be used to handle the Deadlocks -

  1. Deadlock Ignorance
  2. Deadlock Prevention
  3. Deadlock Avoidance
  4. Deadlock Detection and Recovery

1. Deadlock Ignorance -
It is the most widely used approach among all the mechanisms. In this approach, the Operating System assumes that the Deadlock never occurs, it simply ignores the Deadlock. This approach is best suitable for a single end user system where the User uses the system only for browsing and all the other stuff.

2. Deadlock Prevention -
The condition of Deadlock occurs only when Mutual Exclusion, Hold and Wait, No Preemption and Circular Wait holds simultaneously. If we can somehow negate one of the four conditions at any time, then the Deadlock can never occur in the system.
Thus, the main idea behind this approach is to fail one of the 4 necessary conditions for the Deadlock to occur.

3. Deadlock Avoidance -
In this technique, the Operating System checks whether the system is in safe state or in unsafe state at every step which the Operating System performs. This process continues until the system is in the safe state. Once the system moves to the unsafe state, the OS has to backtrack one step.

4. Deadlock Detection and Recovery -
This approach lets the process fall into the Deadlock and then periodically check whether the Deadlock occurs or not. If the Deadlock occurs then it applies some of the above discussed recovery methods to get rid of the Deadlock.

lock

CONCLUSION

The conclusion is, that the Operating System generally uses the Deadlock Ignorance technique to handle Deadlocks as the other techniques would slow down the Operating System. Still, there are components that need more protection than others. For this the OS uses a combination of the previous algorithms, usually trying to break the circular wait somehow.

And thats it! You have successfully learnt all about the Deadlocks in Operating Systems!

Connect with me on Linkedin :- Linkedin

Do check out my Github for amazing projects:- Github

View my Personal Portfolio :- Aryan's Portfolio

Top comments (4)

Collapse
 
navyaarora01 profile image
Navya Arora

Great Content!

Collapse
 
aryan_shourie profile image
Aryan Dev Shourie

Thanks!

Collapse
 
navyaarora01 profile image
Navya Arora

Do wrote a article about difference between deadlock, starvation and livelocks!

Collapse
 
aryan_shourie profile image
Aryan Dev Shourie

Sure! I will write it very soon