Table of contents
1.
Introduction
2.
Algorithm for calculating the minimum number of resources
2.1.
Program for deadlock-free condition(Java)
3.
Frequently Asked Questions
3.1.
A system has 3 processes sharing 4 resources. If each process needs a maximum of 2 resources, what will happen?
3.2.
The system contains three programs, and three tape units can operate each. What is the minimum number of tape units the system needs to ensure that deadlock is never encountered?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Program for Deadlock-free Condition

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

Deadlock is when processes wait for each other to complete their execution. It is a possibility that one process holds some resources for making their execution, but another process holds the same resources, then this condition is known as deadlock. 

In an operating system, deadlock can occur when multiple processes are trying to access some particular mutually exclusive resources.

For this reason, we need to calculate how many resources should be available to ensure that deadlock won't occur.

Let's understand this condition with the help of an example:

We are given the P number of processes in memory and the N number of resources each process needs to complete its execution. The task is to determine the number of resources R that will never result in a deadlock.

A system is in a deadlock-free condition if it satisfies the given condition, i.e.,

If Request for resources(R) <= Available Resources for a process.

Also see, Deadlock Detection and Recovery, Multiprogramming vs Multitasking

Algorithm for calculating the minimum number of resources

Condition for deadlock is: R ≥ P(N−1)+1

where,

R: The number of resources available.

P:  The be the number of processes

N:  The maximum number of resources needed by a process.

 

1. Create a function in which we will calculate the minimum number of resources.

static int no_resources(int P, int N)
num_of_resources = P * (N - 1) + 1;
return num_of_resources;

 

2. Inside the main function

Provide the number of processes and the resources needed for each process, and call the function to calculate the resources

int P = sc.nextInt();
int N = sc.nextInt(); 
System.out.print(no_resources(P, N));

 

Also Read About, FCFS Scheduling Algorithm and Open Source Operating System.

Program for deadlock-free condition(Java)

A system has R identical resources, P number of processes completed for them, and N is the maximum need of the resources for each of the processes. The task is to find the minimum number of Resources required so that deadlock never occurs.

import java.util.Scanner;
public class deadlocks {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number processes: ");
        int P = sc.nextInt(); //Getting Number of processes
        System.out.print("Enter max number of resources needed for each process: ");
        int N = sc.nextInt(); //Getting max number of Resources needed for each process
        System.out.print("number of resources required: ");
        System.out.print(no_resources(P, N));
    }
    //CREATING A FUNCTION TO CALCULATE THE MINIMUM NO OF RESOURCES

    static int no_resources(int P, int N) {
        int num_of_resources = 0;

        // Required condition so that there will be no deadlock is
        // R >= P * (N - 1) + 1

        num_of_resources = P * (N - 1) + 1;
        return num_of_resources;
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Enter number processes: 8
Enter max number of resources needed for each process: 3
number of resources required: 17

Frequently Asked Questions

A system has 3 processes sharing 4 resources. If each process needs a maximum of 2 resources, what will happen?

  • There is a deadlock when each process holds one resource and waits for another.
  • A process must obtain two resources since there are three processes and four resources.
  • As soon as this process has completed its operations, it will release all the resources (2 resources) that it is holding.

The system contains three programs, and three tape units can operate each. What is the minimum number of tape units the system needs to ensure that deadlock is never encountered?

public class deadlocks {

    public static void main(String args[]) {
        int P = 3;
        int N = 3;
        System.out.print("Number of resources required: ");
        System.out.print(no_resources(P, N));
    }

    static int no_resources(int P, int N) {
        int num_of_resources = 0;
        num_of_resources = P * (N - 1) + 1;
        return num_of_resources;
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Number of resources required: 7

 

Conclusion

In this article, we discussed a program for Deadlock Free Condition along with its Implementation. So I hope now you will be clear about how to create a program that is deadlock-free.

And we hope that now you will be able to create a deadlock-free program.

So best of luck with your program.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass