VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/program-for-deadlock-free-condition-in-operating-system/

⇱ Program for Deadlock Free Condition in Operating System - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for Deadlock Free Condition in Operating System

Last Updated : 11 Jul, 2025

Deadlock occurs when two or more processes are stuck, each holding a resource while waiting for another, creating a cyclic dependency. This halts system progress indefinitely. Deadlocks occur when all four conditions are present which are Mutual Exclusion, Hold and Wait, No Preemption and Circular Wait.

To ensure smooth execution in an operating system, deadlocks must be effectively managed. To achieve this, we will explore how to implement a deadlock-free condition in an operating system.

Mathematical Condition for Deadlock Avoidance

In a system with R identical resources and P processes competing for them, the goal is to determine the minimum number of resources required to ensure a deadlock never occurs.

The condition for avoiding deadlock is:

R ≄ P(Nāˆ’1) + 1

Where:

  • R is the total available resources.
  • P is the number of processes.
  • N is the maximum resources a process may need.

This expression ensures deadlock prevention by guaranteeing at least one process always completes execution.

  • If every process holds N-1 resources, the total resources in use are P(N-1).
  • With at least one extra resource (+1), at least one process can acquire the final needed resource, execute, and release resources.
  • This prevents circular wait, ensuring resources are always freed for the next process.

Thus, deadlock is avoided as processes will never be indefinitely blocked.

Examples:

Input : P = 3, N = 4
Output : R >= 10

Input : P = 7, N = 2
Output : R >= 8

Implementing Deadlock-Free Conditions in an Operating System

Deadlock prevention is an important technique used by operating systems to avoid the occurrence of deadlocks. Below are some program for achieving deadlock-free conditions in an Operating System:

Implementation Using Mathematical Condition

For P processes with a need of N resources each, the formula for the minimum resources required to avoid deadlock is:

R ≄ P Ɨ (Nāˆ’1) + 1

Consider three processes: A, B, and C, each with a need of 4 resources.

  • Maximum resources required: 3 processes Ɨ 4 resources = 12 resources.
  • Minimum resources required: 3 Ɨ (4 - 1) + 1 = 10 resources.

Program Implementation:


Output
R >= 10

By allocating 3 resources to each process, we are left with 1 resource. This single resource can be given to any process, allowing it to proceed. Once that process finishes, the resource is released, and another process can use it. This ensures that deadlock will never occur.

Implementation Using Semaphores

This program implements the Dining Philosophers Problem using semaphores to ensure deadlock-free execution. The solution avoids deadlocks by introducing a slight variation in the chopstick-picking order for one philosopher.


Output:

Philosopher 1 is eating
Philosopher 3 is eating
Philosopher 1 is thinking
Philosopher 0 is eating
Philosopher 3 is thinking
Philosopher 2 is eating
Philosopher 0 is thinking
Philosopher 4 is eating
Philosopher 2 is thinking
Philosopher 1 is eating
Philosopher 4 is thinking
...

Explanation:

  • Each philosopher tries to pick up two chopsticks (left and right).
  • Philosophers 0 to 3 pick up the left chopstick first, while philosopher 4 picks up the right chopstick first.
  • This ensures that at least one philosopher can always proceed, avoiding deadlock.
  • Code will run indefinitely.
Comment