Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 14

Deadlock Prevention Strategies

Recommended resources

Hello! Let's dive into the fourth lesson of our module on Deadlocks.

In our previous lessons, we defined the four necessary conditions for a deadlock to occur—Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait—and learned how to detect a deadlock using a Resource Allocation Graph (RAG). Today, we'll shift from detection to prevention. Our goal is to apply strategies that ensure a deadlock can never happen in the first place. This is a crucial topic for the GATE exam, testing both your conceptual understanding and your problem-solving skills.

Today's learning outcome is to apply deadlock prevention strategies by negating one of the four necessary conditions. We will systematically examine each condition and explore how breaking it can make the system deadlock-free.

The Principle of Deadlock Prevention

The core idea of deadlock prevention is straightforward: design the system in such a way that at least one of the four necessary conditions for deadlock can never hold true. If even one condition is impossible, deadlocks are structurally prevented.

Deadlock Prevention: Conditions for Elimination
This diagram succinctly summarizes our goal. To prevent deadlock, we must ensure that at least one of these four conditions—Mutual Exclusion, Hold & Wait, No Preemption, or Circular Wait—is never satisfied.

We will now explore the strategies for negating each of these conditions. The following video from Gate Smashers provides an excellent overview.

L-4.4: Deadlock Handling Methods and Deadlock Prevention | Operating System

Please watch this video, 'L-4.4: Deadlock Handling Methods and Deadlock Prevention', which walks through the four main prevention techniques. We will discuss each point in more detail below.

Watch the segment from 06:00 to 20:30. The video explains the core concept of prevention and then discusses how to negate each of the four necessary conditions for deadlock.

Let's break down these strategies one by one, supplementing the video with some additional details.

1. Negating Mutual Exclusion

  • The Idea: Ensure that no resource is ever held exclusively. If all resources were sharable, processes would never have to wait for them, and deadlock would be impossible.
  • Practicality: This is generally not feasible. Some resources are inherently non-sharable. For example, a printer cannot produce two documents simultaneously without scrambling the output. While some resources like read-only files can be shared, many, such as printers, tape drives, or even a database record being updated, require exclusive access.
  • Conclusion: We usually cannot prevent deadlocks by denying the mutual exclusion condition.

2. Negating Hold and Wait

  • The Idea: To break the "hold and wait" condition, we must ensure that a process cannot hold some resources while waiting for others.
  • Practicality: There are two main approaches:
    1. Request all resources at once: A process must request all the resources it will ever need before it begins execution. The system will only grant its requests if all of them can be satisfied simultaneously.
    2. Release before requesting: A process can request resources as needed, but to get a new resource, it must first release all resources it currently holds.
  • Drawbacks: Both approaches suffer from poor resource utilization. In the first approach, a process might hold a resource for hours before it's actually needed, making it unavailable to other processes. This can also lead to starvation if a process needs many popular resources. The second approach is often impractical and inefficient.

3. Negating No Preemption

  • The Idea: If a process is holding some resources and requests another that cannot be immediately allocated, the system can preempt (take away) the resources it currently holds. These preempted resources are added to the list of available resources, and the process is placed in a waiting state until it can get all its old and new resources.
  • Practicality: This can be a viable strategy, especially if resource allocation is managed by priority. For example, if a high-priority process requests a resource held by a low-priority process, the system might preempt the resource.
  • Drawbacks: This approach is difficult to implement for all resource types. Imagine preempting a printer mid-job—you would have to roll back the state, which can be complex.

4. Negating Circular Wait

  • The Idea: This is the most practical prevention strategy. We impose a total ordering of all resource types and require that each process requests resources in an increasing order of enumeration.
  • Example: Let's say we number our resources as follows: , , .
    • A process can request and then because .
    • However, if a process is holding , it cannot then request , because . It must request resources in increasing numerical order.
  • How it works: This rule makes a circular wait impossible. A circle requires a process holding to request , while another process holding requests . If we enforce an ordering, say , then can request while holding , but cannot request while holding . The circular dependency is broken.

For a concise summary of these strategies, you can refer to the following article.

Deadlock Detection | Deadlock Prevention

The article 'Deadlock Detection | Deadlock Prevention' from GateVidyalay provides a clear, structured list of the prevention strategies and their practical limitations.

Read the sections under the heading 'Deadlock Prevention', which cover the violation of Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait.


Quantitative Prevention: A Key GATE Technique

A very common type of GATE question asks for the minimum number of resources required to guarantee a deadlock-free system, given a number of processes and their maximum needs. This is a specific prevention technique based on ensuring the "Hold and Wait" condition never leads to a deadlock.

The logic is based on a "worst-case" scenario. A deadlock can occur if every process is holding some resources and waiting for just one more.

Let's formalize this:

  • Let n be the number of processes.
  • Let m be the total number of instances of a resource type R.
  • Let max_need_i be the maximum number of instances of R required by process i.

The worst-case for deadlock is when each of the n processes holds max_need_i - 1 resources. At this point, they are all waiting for one more resource to complete. The total number of resources held in this state is:

If the system has exactly this many resources, a deadlock is possible because no process can get its final resource. To guarantee that deadlock does not occur, we just need one more resource. This extra resource can be given to one of the waiting processes, allowing it to finish and release its resources for others.

Therefore, the condition for a deadlock-free system is:

Or, more simply, the minimum number of resources m to guarantee no deadlock is:

The following video solves a GATE 2018 question using this exact logic and also derives a useful formula.

L-4.8: GATE 2018 Question Explaination on deadlock | Operating system

This video, 'L-4.8: GATE 2018 Question Explanation on deadlock', is essential viewing. It demonstrates how to solve a typical GATE problem by analyzing the worst-case scenario and then derives a simple, powerful formula you can use in the exam.

Watch the segment from 01:06 to 19:36. Pay close attention to how the worst-case scenario is constructed (each process holding one less than its max need). The derived formula, Total Resources + Total Processes > Total Demand, is a critical takeaway.

Test your understanding!

A system has 4 processes and 12 instances of a single resource type. Each process needs a maximum of K instances. What is the largest value of K for which the system is guaranteed to be deadlock-free?

Use the formula from the video: Total Resources (m) + Total Processes (n) > Total Demand.

Show answer

We are given:

  • m = 12 (Total Resources)
  • n = 4 (Total Processes)
  • Total Demand = n * K = 4 * K

The condition for being deadlock-free is:

Plugging in the values:



Since K must be an integer, the largest value of K that satisfies this inequality is 3.

Let's double-check with the other formula: m >= (Sum(K_i - 1)) + 1





Again, the largest integer value for K is 3.

You can find more practice problems of this type in the article "Deadlock in OS | Deadlock Problems | Questions" (resource ID LINK), particularly Problems 05-10 which ask for the maximum number of processes or the max need K.


Conclusion

In this lesson, we transitioned from merely identifying deadlocks to actively preventing them. You learned how to design a system where deadlocks are impossible by violating one of the four necessary conditions.

Key Takeaways:

  • Deadlock Prevention: The strategy of designing a system to negate at least one of the four necessary conditions for deadlock (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait).
  • Practicality Trade-offs: While conceptually simple, negating most conditions comes with significant performance or implementation costs. Negating Mutual Exclusion is often impossible, while negating Hold-and-Wait or No-Preemption can lead to low resource utilization and starvation.
  • Resource Ordering: Negating the Circular Wait condition by enforcing a strict ordering of resource requests is the most common and practical prevention technique.
  • Quantitative Prevention for GATE: For single-resource-type problems, you can guarantee a deadlock-free system by ensuring the number of resources m satisfies m > Sum(max_need_i - 1). This is a vital formula for quickly solving certain GATE questions.

In our next and final lesson of this module, we will consolidate your knowledge by focusing exclusively on solving a variety of GATE problems related to deadlock conditions, RAG analysis, and the prevention techniques you've just learned.