Open In App

Relation Between Number of Processes and Number of Resources to Prevent Deadlock

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A set of blocked processes where each process is holding some resource and waiting to acquire some other resource held by another process in the set. This situation is called deadlock in the operating system.

Deadlock can be prevented if the number of resource instances in a system is significantly larger than the number of processes asking for a resource. In this article, we are going to understand the relation between a number of processes and a number of resources to prevent deadlock based on the two following problem statements:

  • Given the maximum number of resources asked by each process, calculate the minimum number of resources required to permanently avoid deadlock.
  • Given the number of resources and number of processes available in the system, calculate the maximum number of resources one process can ask without leading to any deadlock combination.

Problem 1

Calculate the minimum number of resources required.

Given the maximum number of resources asked by each process, calculate the minimum number of resources required to permanently avoid deadlock. Let us take the following example,

A given system has n processes and m instances of a certain resource type. The iáµ—Ê° process can ask at most Sáµ¢ resources. What is the minimum value of m, so that the system never enters into deadlock?

Approach

  • To solve this problem we can just calculate the maximum number of resources that can lead to a deadlock and add one to that.
  • The system can only be in a deadlock when all the resources are already occupied and all the processes are requesting at least one resource.
  • As all the resources are occupied, that means the total number of resources = Σ(Number of resources occupied by each process)
  • Let us assume that the iáµ—Ê° process is occupying some resources and it is currently waiting for at least one resource to be free.
  • That means out of Sáµ¢ resources every process is currently occupying (Sáµ¢ – 1) resources and waiting for one resource.
  • The maximum number of resources that can lead to a deadlock = Σ(Sáµ¢ – 1) for each i=1 to n.

Now we just have add one to the The maximum number of resources that can lead to a deadlock to get the desired result. The calculation is as follows: m \ge\ \sum^{n}_{i=1}(S_{i}-1)\ +1

So the minimum number of resources required to avoid a deadlock is: \sum^{n}_{i=1}(S_{i}-1)\ +1

Problem 2

Calculate the maximum number of resources one process can ask?

Given the number of resources and number of processes available in the system, calculate the maximum number of resources one process can ask without leading to any deadlock combination.

A given system has n processes and m instances of a certain resource type. Each process can request maximum of k instances at a certain point of time. What is the largest value of k that will always avoid deadlock?

Approach

  • From problem 1 we know that in order to avoid deadlock, m \ge\ \sum^{n}_{i=1}(S_{i}-1)\ +1   .
  • Now replacing the value of Sáµ¢ with given value k we get, m \ge\ \sum^{n}_{i=1}(k-1)\ +1
  • Simplifying the above equation we will get, m \ge\ nk-n\ +1
  • So k is less than or equal to (m + n – 1)/n

So the maximum number of resources one process can ask is (m + n – 1)/n.

Relevant GATE Questions

GATE CS 2018 | Question 30

GATE-CS-2006 | Question 85

GATE-CS-2005 | Question 90

GATE CS 1998 | Question 32

GATE CS 1997 | Question 47

ISRO CS 2009 | Question 16


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads