Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

banker Algorithm (deadlock avoidence), Summaries of Operating Systems

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Typology: Summaries

2022/2023

Uploaded on 11/27/2023

aryan-verma-10
aryan-verma-10 🇮🇳

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Banker’s Algorithm
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that
tests for safety by simulating the allocation for the predetermined maximum possible
amounts of all resources, then makes an “s-state” check to test for possible activities,
before deciding whether allocation should be allowed to continue.
Why Banker’s algorithm named so?
Banker’s algorithm is named so because it is used in the banking system to check
whether a loan can be sanctioned to a person or not. Suppose there are n number of
account holders in a bank and the total sum of their money is S. If a person applies for
a loan then the bank first subtracts the loan amount from the total money that the bank
has and if the remaining amount is greater than S then only the loan is sanctioned. It
is done because if all the account holders come to withdraw their money then the bank
can easily do it.
In other words, the bank would never allocate its money in such a way that it can no
longer satisfy the needs of all its customers. The bank would try to be in a safe state
always.
Advantages
Following are the essential characteristics of the Banker's algorithm:
1. It contains various resources that meet the requirements of each process.
2. Each process should provide information to the operating system for upcoming
resource requests, the number of resources, and how long the resources will be
held.
3. It helps the operating system manage and control process requests for each type
of resource in the computer system.
4. The algorithm has a Max resource attribute that represents indicates each process
can hold the maximum number of resources in a system.
Disadvantages
1. It requires a fixed number of processes, and no additional processes can be
started in the system while executing the process.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download banker Algorithm (deadlock avoidence) and more Summaries Operating Systems in PDF only on Docsity!

Banker’s Algorithm

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Why Banker’s algorithm named so?

Banker’s algorithm is named so because it is used in the banking system to check whether a loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money that the bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the account holders come to withdraw their money then the bank can easily do it. In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. The bank would try to be in a safe state always.

Advantages

Following are the essential characteristics of the Banker's algorithm:

  1. It contains various resources that meet the requirements of each process.
  2. Each process should provide information to the operating system for upcoming resource requests, the number of resources, and how long the resources will be held.
  3. It helps the operating system manage and control process requests for each type of resource in the computer system.
  4. The algorithm has a Max resource attribute that represents indicates each process can hold the maximum number of resources in a system.

Disadvantages

  1. It requires a fixed number of processes, and no additional processes can be started in the system while executing the process.
  1. The algorithm does no longer allows the processes to exchange its maximum needs while processing its tasks.
  2. Each process has to know and state their maximum resource requirement in advance for the system.
  3. The number of resource requests can be granted in a finite time, but the time limit for allocating the resources is one year.

Working on Bankers' algorithm

When working with a banker's algorithm, it requests to know about three things:

  1. How much each process can request for each resource in the system. It is denoted by the [ MAX ] request.
  2. How much each process is currently holding each resource in a system. It is denoted by the [ ALLOCATED ] resource.
  3. It represents the number of each resource currently available in the system. It is denoted by the [ AVAILABLE ] resource. The Banker's Algorithm is the combination of the safety algorithm and the resource request algorithm to control the processes and avoid deadlock in a system: The following Data structures are used to implement the Banker’s Algorithm: Let ‘n’ be the number of processes in the system and ‘m’ be the number of resource types. Allocation:
  • Specifies the number of resources currently allocated to a process.
  • 2 - D array of size n*m.
  • Allocation[i,j]=k, i.e, 'k' instances of a resource Rj is allocated to process Pi. Max need:
  • Specifies the number of resources needed by a process.
  • 2 - D array of size n*m.
  • Max need [i,j]=Max[i,j]-Allocation[i,j]. Available:
  • Specifies the number of available resources for a process.
  • 1 - D array of size m.
  • Available[j]=k, ie, resource type Rj has k instances. Remaining need(Max):
  • Specifies the maximum demand of each process in a system.
  • 2 - D array of size n*m.

Goto step (2) ; else raise error condition as the process exceeds its maximum claim.

  1. If Request[i] <= Available Goto step (3); else wait as the resources are not available. 3)Pretend to have allocated the resources that are requested to process Pi by modifying the state as Available = Available – Request[i] Allocation[i] = Allocation[i] + Request[i] Need[i] = Need[i]– Request[i]

Example:

Considering a system with five processes P 0 through P 4 and three resources of type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t 0 following snapshot of the system has been taken: Steps of deadlock detection algorithm: 1.Deadlock detection algorithm will check that process requesting resources are available Request <= Available

  1. If the above condition gets satisfied then the algorithm will allocate the resources demanded by the process. Also, after fulfilling the request, availability gets added with the allocation of that process. available = available + allocation

Else, the algorithm will keep the process till its next turn. Also, detection algorithm create safe sequence i.e. sequence of resource allocated processes without any deadlock. Here, safe sequence is P1 - > P3 - > P4 - > P0 - > P

Code for Banker’s Algorithm

#include <stdio.h>

int main()

// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k,alloc[10][10],max[10][10],avail[10];

// n = 5; // Number of processes

printf("Enter NO. Of Processes");

scanf("%d",&n);

// m = 3; // Number of resources

printf("Enter Number of resources");

scanf("%d",&m);

printf("Enter allocation matrics");

for (i=0;i<n;i++){

for(j=0;j<m;j++){

scanf("%d",&alloc[i][j]);

printf("Enter Max matrics");

int f[n], ans[n], ind = 0;

for (k = 0; k < n; k++) {

f[k] = 0;

int need[n][m];

for (i = 0; i < n; i++) {

for (j = 0; j < m; j++)

need[i][j] = max[i][j] - alloc[i][j];

int y = 0;

for (k = 0; k < 5; k++) {

for (i = 0; i < n; i++) {

if (f[i] == 0) {

int flag = 0;

for (j = 0; j < m; j++) {

if (need[i][j] > avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;

for (y = 0; y < m; y++)

avail[y] += alloc[i][y];

f[i] = 1;

int flag = 1;

for(int i=0;i<n;i++)

if(f[i]==0)

flag=0;

printf("The following system is not safe");

break;

if(flag==1)

printf("Following is the SAFE Sequence\n");

for (i = 0; i < n - 1; i++)

printf(" P%d - >", ans[i]);

printf(" P%d", ans[n - 1]);

return (0);