1. Home
Operating System

OS Tutorial: Learn Operating Systems Basics

Learn Operating System fundamentals: concepts, processes, memory management, and more. Start your journey to mastering OS with our comprehensive tutorial.

  • 47
  • 7 Hours
right-top-arrow

Tutorial Playlist

47 Lessons
21

Deadlock in Operating System

Updated on 19/07/2024466 Views

What do you think happens when there are multiple processes running on a single system simultaneously? 

Each process will have to compete for resource allocation so that it can be completed first. The processes, thus, get stuck and do not make any progress. This condition is termed as a “Deadlock in Operating System.” 

Having worked in the line of software engineering, I have had my fair share of experience with deadlocks numerous times. This is what I will discuss with you in this tutorial. 

Let me teach you all you need to know about a deadlock in OS including the types of deadlock in OS, how to remove them, and their advantages and disadvantages.

Happy reading! 

What is Deadlock in OS? (With Example) 

We all understand that every process taking place in a system requires the assistance of a resource in order to attain execution. This resource can either be the central processing unit (CPU) of the system or some other plugged-in device. When one process ends, the resource that was in use is released. 

To properly explain the concept of Deadlock, here’s an example. Refer to the picture below. Here, P1 and P2 are two processes and R1 and R2 are two resources. 

P1 is actually assigned to R1, whereas it waits for R2. On the other hand, P2 is assigned to R2, but it ends up waiting for R1. 

As a result, neither of the processes are getting executed, because the resources that are needed by them are not being vacated. This is a classic example of deadlock in OS

How to Differentiate Deadlock From Starvation?

If your concept of the two terms “deadlock” and “starvation” is not clear, here is an explanation for the same. Let me first put it in simple terms. 

In case of deadlock, the execution of processes takes longer because resource allocation takes time. On the other hand, in case of starvation, resource is never allocated to low-priority processes, as a result of which they are never executed, 

I will list down the differences between the two in the table below:

Parameter 

Deadlock

Starvation 

What is it?

Here, there is more than one process running in a system. This hinders the execution of the processes due to no resource allocation. 

Resources are never allocated to low-priority processes. As a result, they are never executed. 

Also known as

Circular wait

Lived lock

Resource allocation

Resource allocation is not done.

Resources are allocated to high-priority processes. 

Execution 

No processes are executed. 

Only high-priority processes are executed. 

What are the Conditions that Result in Deadlock in OS?

It is not always necessary that whenever there is more than one process running in a system requiring the involvement of multiple resources, the system will face a deadlock. 

There are four conditions that lead to a deadlock in OS. I have listed them down below:

  1. Mutual exclusion: Every process has a designated resource that cannot be shared with another process. When two processes are running simultaneously, one process cannot ask for the resource that is designated to be used by the other process. 
  2. Hold and wait: In this deadlock condition, one process has to wait for an occupied resource to be free. Unless a resource in use is done assisting the completion of one process, the other cannot start using it. 
  3. No preemption:  It is not possible to schedule the execution of two processes simultaneously. This means that when one process is duly completed, the allocated or occupied resource will be released and made free to be assigned for the completion of another process.  
  4. Circular wait: All the processes continue to wait in a cyclic manner. As a result, they get stuck in a cycle of eternal wait, thereby never getting executed. 

This causes them to be also known as "circular wait."

Hope this gives you a clear idea about the four conditions for deadlock in OS. Learn more about operating systems here.

How to Handle Deadlock in OS?

Are you trying to figure out methods for handling deadlock in OS? Let me jot down the ways: 

  1. Detecting a deadlock and recovering:

 In this method, you need to identify a deadlock situation and look for ways in which it can be recovered. Users can abort the processes that can result in potential deadlocks. 

It is advisable to go by this process one step at a time. This will help to detect which process is causing the deadlock. Another way of preventing it could be to free the resources from allocation until the deadlock condition perishes. This is known as "resource preemption."

  1. Preventing a deadlock

If a user is able to avoid any of the four conditions that are responsible for causing a deadlock,  it might be possible to prevent one. 

  1. Averting a possible deadlock

When a process seeks resource allocation, an algorithm is run to determine whether a resource is safe to be allocated. 

If it is found unsafe, the request for allocation is turned down. This algorithm is known as the deadlock avoidance in OS technique. 

You can look into a resource allocation graph in operating system, which is a pictorial representation that provides information on the processes that are using some resources or are awaiting the allocation of some resources. 

This resource allocation graph in OS example that I have highlighted provides information on whether there are resources available to be used by other processes.

  1. Deadlock ignorance

Some operating systems like Windows and UNIX are obvious to deadlocks. They, therefore, ignore the situation of deadlock whenever it arises. 

This technique is called the "Ostrich algorithm." In case a situation of deadlock arises, you simply have to reboot the PC, and you will be able to resolve the deadlock issue in no time. 

Advantages of Deadlock in OS

There are several factors you can take advantage of when you encounter a deadlock in OS. They are:

  • Convenience of reclaiming resources: If you use the recovery method, you will be able to free up resources and allow their allocation to other processes so that they can be executed. This helps to reduce the impact of deadlock on the performance of the system. 
  • Scope for risk reduction: Users can restrict the number of resources that can be allocated to the process, which in turn helps to reduce the risk of deadlock. This is a great way of ensuring that the processes do not consume a large number of resources unnecessarily.  
  • Guaranteed resource availability: Ensuring resource availability is a key benefit of deadlocks, as they help prioritize critical processes and essential tasks. Detecting and resolving deadlocks promptly improves system robustness, ensuring smooth operation. 
  • Easy detection and resolution: If deadlocks are detected proactively, it becomes easier to nip the problem at the bud. This enhances the robustness of the system and makes sure that the system can continue to function smoothly.

Disadvantages of Deadlock in OS

Now that I have discussed the advantages of deadlock in operating system, let me throw light on the other side of the coin as well. The disadvantages of deadlock in OS are as follows:

  • Added complexity: Some methods of deadlock handling in OS may make the process more complex. It may be a resource-intensive task to implement and maintain these methods. 
  • Delayed execution: Methods of deadlock resolution may include process termination. This, in turn, can hinder the entire process of execution and result in a delay. Such disruption can also result in inconsistency.
  • Complex decision-making: Handling a deadlock involves complex decision-making. Users have to determine which process to wait for and which process to abort. 

Such frequent recovery and deduction processes may impact the overall performance of the system, and induce latency. This can have a negative impact on time-sensitive applications where responsiveness may be crucial. 

Wrapping Up

I hope I've clarified what a deadlock in OS is. With this basic understanding, spotting and resolving deadlocks becomes simpler. Knowing how to prevent deadlocks is crucial for keeping systems stable and efficient. 

If you're keen on delving deeper into such intuitive concepts, consider exploring upGrad. The platform offers tailored courses designed by experts, ideal for working professionals seeking to enhance their skills.

Frequently Asked Questions

  1. What is deadlock and its 4 types?

During a deadlock in OS, processes are blocked because one resource is being used by a process. The processes in queue have to wait for another resource to be allocated. The four types of deadlocks are- mutual exclusion, hold and wait, no preemption, and circular wait. 

  1. What is a deadlock example?

We can understand deadlock as two trains running in opposite directions towards each other on a single track. Both the trains wait for the track to be cleared so that they can move forward, but neither of them can actually proceed. 

  1. What is deadlock and its prevention?

Deadlock is a condition in which two or more processes access the same resource, and both fail to access it simultaneously, thereby causing the process to get stuck. It can be prevented by detecting and preventing, avoidance, and ignorance. 

  1. What are the 4 components of deadlock?

The four components include mutual exclusion, no preemption, hold and wait, and circular wait. 

  1. What causes deadlock in OS?

Deadlock in OS happens when there are more than one process awaiting to utilize a single resource, whereas neither is able to do so.

  1. How is the deadlock resolved?

The most common solution for a deadlock in OS is to cancel, restart,or abort a process. 

  1. How to detect deadlock in OS?

Algorithms can help detect deadlocks in operating systems. 

  1. What are the advantages of deadlock?

You can free up resources by using the recovery method when you encounter a deadlock. 

  1. Is deadlock important in OS?

Deadlocks are important in operating systems because they help make a system stable and efficient. 

  1. Can we remove deadlock?

Yes. Deadlocks can be easily removed. 

Pavan Vadapalli

Pavan Vadapalli

Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and l…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...