Программа для отсутствия тупиков в операционной системе

Опубликовано: 28 Декабря, 2021

Дано: система имеет R идентичных ресурсов, P процессов конкурируют за них, а N - максимальная потребность каждого процесса. Задача - найти минимальное количество требуемых ресурсов, чтобы никогда не возникла тупиковая ситуация.

Формула:

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

Примеры:

 Ввод: P = 3, N = 4
Выход: R> = 10

Ввод: P = 7, N = 2
Выход: R> = 8

Подход:

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 occuur
minResources = process * (need - 1) + 1;
return minResources;
}
// Driver code
int 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 resources
static int Resources( int process, int need)
{
int minResources = 0 ;
// Condition so that deadlock
// will not occuur
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 occuur
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 occuur
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

Выход:

 R> = 10

Вниманию читателя! Не прекращайте учиться сейчас. Получите все важные концепции теории CS для собеседований SDE с курсом теории CS по доступной для студентов цене и будьте готовы к отрасли.