Program for Deadlock free condition in Operating System

30 Sep, 2021
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. 
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++
Java
Python3
C#
// C++ implementation of above program.#include <bits/stdc++.h>using namespace std; // function that calculates// the minimum no. of resourcesint Resources(int process, int need){    int minResources = 0;     // Condition so that deadlock    // will not occur    minResources = process * (need - 1) + 1;     return minResources;} // Driver codeint main(){    int process = 3, need = 4;     cout << "R >= " << Resources(process, need);    return 0;}
// Java implementation of above program class GFG{ // function that calculates// the minimum no. of resourcesstatic int Resources(int process, int need){    int minResources = 0;     // Condition so that deadlock    // will not occur    minResources = process * (need - 1) + 1;     return minResources;} // Driver Codepublic static void main(String args[]){    int process = 3, need = 4;         System.out.print("R >= ");    System.out.print(Resources(process, need));}}
# Python 3 implementation of# above program  # function that calculates# the minimum no. of resourcesdef Resources(process, need):     minResources = 0     # Condition so that deadlock    # will not occur    minResources = process * (need - 1) + 1     return minResources # Driver Codeif __name__ == "__main__" :     process, need = 3, 4     print("R >=", Resources(process, need)) # This Code is Contributed# by Naman_Garg
// C# implementation of above programusing System; class GFG{ // function that calculates// the minimum no. of resourcesstatic int Resources(int process, int need){    int minResources = 0;     // Condition so that deadlock    // will not occur    minResources = process * (need - 1) + 1;     return minResources;} // Driver Codepublic static void Main(){    int process = 3, need = 4;         Console.Write("R >= ");    Console.Write(Resources(process, need));}} // This code is contributed// by Sanjit_Prasad
Output: 
R >= 10
 
Naman_Garg
Sanjit_Prasad
ankita_saini
simmytarika5
Operating Systems-Deadlock
C++ Programs
Computer Subject
Operating Systems
Operating Systems
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
How to find the minimum and maximum element of a Vector using STL in C++?
C++ Program for QuickSort
C Program to Swap two Numbers
SDE SHEET - A Complete Guide for SDE Preparation
What is Algorithm | Introduction to Algorithms
Software Engineering | Coupling and Cohesion
Type Checking in Compiler Design
Difference between NP hard and NP complete problem