Open In App

Program for Deadlock free condition in Operating System

Improve
Improve
Like Article
Like
Save
Share
Report

Given: A system has R identical resources, P processes competing for them and N is the maximum need of each process. The task is to find the minimum number of Resources required So that deadlock will never occur. 

Deadlock prevention is an important technique used by operating systems to avoid the occurrence of deadlocks. Here is an example program for achieving deadlock-free conditions in an operating system:

C




#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
 
sem_t chopstick[5]; // Semaphore for each chopstick
 
void* philosopher(void* arg)
{
    int id = *(int*)arg;
    int left_chopstick
        = id; // Philosopher picks up left chopstick first
    int right_chopstick
        = (id + 1)
          % 5; // Philosopher picks up right chopstick next
 
    while (1) {
        // Wait until both chopsticks are available
        sem_wait(&chopstick[left_chopstick]);
       
        sem_wait(&chopstick[right_chopstick]);
 
        // Eat for some time
        printf("Philosopher %d is eating\n", id);
        sleep(2);
 
        // Release both chopsticks
        sem_post(&chopstick[left_chopstick]);
        sem_post(&chopstick[right_chopstick]);
 
        // Think for some time
        printf("Philosopher %d is thinking\n", id);
       
       // Making thread to sleep
        sleep(2);
    }
}
 
int main()
{
    pthread_t philosophers[5];
 
    // Initialize semaphore for each chopstick
    for (int i = 0; i < 5; i++) {
        sem_init(&chopstick[i], 0, 1);
    }
 
    // Create thread for each philosopher
    for (int i = 0; i < 5; i++) {
        int* id = (int*)malloc(sizeof(int));
        *id = i;
        pthread_create(&philosophers[i], NULL, philosopher,
                       id);
    }
 
    // Wait for all threads to complete
    for (int i = 0; i < 5; i++) {
        pthread_join(philosophers[i], NULL);
    }
 
    return 0;
}


The output of the above program would be a continuous cycle of philosophers eating and thinking. Each philosopher would pick up their left chopstick first, then their right chopstick, and begin eating for two seconds. After finishing eating, they would release both chopsticks and start thinking for another two seconds. This cycle would repeat indefinitely.

The output would look something like this:

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

Formula: 

R >= P * (N - 1) + 1 

Examples:  

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

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

Approach: 

Consider, 3 process A, B and C. 
Let, Need of each process is 4 
Therefore, The maximum resources require will be 3 * 4 = 12 i.e, Give 4 resources to each Process. 
And, The minimum resources required will be 3 * (4 – 1) + 1 = 10. 
i.e, Give 3 Resources to each of the Process, and we are left out with 1 Resource. 
That 1 resource will be given to any of the Process A, B or C. 
So that after using that resource by any one of the Process, It left the resources and that resources will be used by any other Process and thus Deadlock will Never Occur. 

C++




// C++ implementation of above program.
#include <bits/stdc++.h>
using namespace std;
 
// function that calculates
// the minimum no. of resources
int Resources(int process, int need)
{
    int minResources = 0;
 
    // Condition so that deadlock
    // will not occur
    minResources = process * (need - 1) + 1;
 
    return minResources;
}
 
// Driver code
int main()
{
    int process = 3, need = 4;
 
    cout << "R >= " << Resources(process, need);
    return 0;
}


Java




// Java implementation of above program
 
class GFG
{
 
// function that calculates
// the minimum no. of resources
static int Resources(int process, int need)
{
    int minResources = 0;
 
    // Condition so that deadlock
    // will not occur
    minResources = process * (need - 1) + 1;
 
    return minResources;
}
 
// Driver Code
public static void main(String args[])
{
    int process = 3, need = 4;
     
    System.out.print("R >= ");
    System.out.print(Resources(process, need));
}
}


Python3




# Python 3 implementation of
# above program
  
# function that calculates
# the minimum no. of resources
def Resources(process, need):
 
    minResources = 0
 
    # Condition so that deadlock
    # will not occur
    minResources = process * (need - 1) + 1
 
    return minResources
 
# Driver Code
if __name__ == "__main__" :
 
    process, need = 3, 4
 
    print("R >=", Resources(process, need))
 
# This Code is Contributed
# by Naman_Garg


C#




// C# implementation of above program
using System;
 
class GFG
{
 
// function that calculates
// the minimum no. of resources
static int Resources(int process, int need)
{
    int minResources = 0;
 
    // Condition so that deadlock
    // will not occur
    minResources = process * (need - 1) + 1;
 
    return minResources;
}
 
// Driver Code
public static void Main()
{
    int process = 3, need = 4;
     
    Console.Write("R >= ");
    Console.Write(Resources(process, need));
}
}
 
// This code is contributed
// by Sanjit_Prasad


Javascript




function Resources(process, need) {
  let minResources = 0;
 
  minResources = process * (need - 1) + 1;
 
  return minResources;
}
 
let process = 3, need = 4;
 
console.log(`R >= ${Resources(process, need)}`);
 
// This code is contributed by ishankhandelwals.


Output

R >= 10

The program for the deadlock-free condition in operating systems does not have clear advantages or disadvantages as it is a solution to prevent deadlocks from occurring in concurrent programs. However, we can discuss the benefits and limitations of the solution itself.

Advantages:

  1. Prevents deadlocks from occurring in concurrent programs
  2. Allows all resources to be utilized efficiently without causing conflicts or starvation
  3. Maintains the system’s stability and reliability by preventing program crashes and hangs

Limitations:

  1. The solution may lead to underutilization of resources, as some processes may not be able to acquire all necessary resources at once due to the limited availability of the resources.
  2. The solution may require additional overhead and complexity to implement, which can increase the overall system complexity and reduce the system’s performance.
  3. The solution may not be suitable for all types of programs, as some programs may require certain resources to be acquired simultaneously, which may lead to conflicts.
  4. Overall, the deadlock-free solution is a useful technique to prevent deadlocks from occurring in concurrent programs, but its implementation should be carefully considered in relation to the specific requirements of the program and system resources.


Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads