×

Banker’s Algorithm in C

Introduction

Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests resource allocation for predetermined maximum quantities of all resources.

Why is it called the banker's algorithm?

It was created by Edsger Dijkstra. The avoidance algorithm is another name for the Banker's Algorithm. Whenever a resource request is made, it is intended to check the safe state.

The reason the banker's algorithm is so titled is that it uses a bank as an analogy, where customers can request cash withdrawals. Cash is lent to the customer based on some data. More cash than what the customer has asked, and the whole amount of cash on hand cannot be given by the banker. Because it uses a bank analogy, this method is known as the banker's algorithm.

The following list contains the fundamental data structures necessary to implement this algorithm.

Let n represent the overall system process count and m represent the overall system resource type count.

  • Available: There is an m-length vector available. It displays the quantity of each sort of resource that is available. If Available[i] = k, then there are k instances of resource Ri available.
  • Max: Max is an n-by-m matrix that represents the highest demand for each process. Process If Max I j] = k, Pi can request a maximum of k instances of resource type Rj.
  • Allocation: The number of resources of each type that are currently assigned to each process is listed in the allocation matrix, which is a n x m matrix. Pi k instances of the resource type are allocated by allocation I j] = k. Rj.
  • Need: Each process' remaining resource requirements are shown in an n-by-m matrix. Process Pi may need k additional instances of resource type Rj to finish the task if Need I j] = k.

Algorithm for Safety

An explanation of the process for figuring out whether a system is in a safe condition is provided below:

1. Assume that Work and Finish are represented by the two vectors m and n, respectively.
Beginning with "Work = Available,"
Finish[i], where I ranges from 1 to n, is false.


2. Find an I so that 
a) Finish[i] = false.
b) If there is no such I, proceed to step (4). Needi = Work 


3. Allocation[i] is equal to Work + Work Finish[i] as the actual next step (2)


4. If Finish I = true for each and every I the system is in a secure state.

Algorithm for Resource Requests

Let Request be a list of requests to process Pi. Request [j] = k indicates that Process Pi has asked for k instances of resources of type Rj. When process Pi makes a request for resources, the following things take place:

1) If Requesti >= Needi, move on to step 2, otherwise indicate an error condition because there are too many claims for the process to handle.


2) If Requesti = Accessible, if you don't move on to step 3, Pi will have to wait since the resources aren't available.
3) Alter the state so that it appears as though the system has provided Pi with the necessary resources.
Requested minus Available equals Available
Allocationi = Allocationi - Requesti Needi = Needi - Requesti

Program for Banker’s Algorithm in C

#include <stdio.h>
 
int curr [ 5 ][ 5 ], max_claim [ 5 ][ 5 ] , avlble [ 5 ]  ;
int allocation [ 5 ] = { 0, 0, 0, 0, 0 }  ;
int maxres [ 5 ], running [ 5 ], safe = 0  ;
int counter = 0, i, j, exec, rsrces, prcesses, z = 1  ;
 
int main ( )
{
     printf ( "\nEnter number of processes: " ) ;
     scanf ( "%d", &prcesses ) ;
 
     for ( i = 0 ; i < prcesses ; i++ )
{
         running[i] = 1 ;
         counter++ ;
     }
 
     printf ( "\nEnter number of resources: " ) ;
     scanf ( "%d", &rsrces ) ;
 
     printf ( "\nEnter Claim Vector:" ) ;
     for ( i = 0 ; i < rsrces ; i++ )
{
        scanf ( "%d", &maxres[i] ) ;
     }
 
   printf ( "\nEnter Allocated Resource Table:\n" ) ;
     for ( i = 0 ; i < prcesses ; i++ )
{
        for ( j = 0 ; j < rsrces ; j++ )
{
   scanf ( "%d", &curr[i][j] ) ;
         }
     }
 
     printf ( "\nEnter Maximum Claim Table:\n" ) ;
     for ( i = 0 ; i < prcesses ; i++ )
{
         for ( j = 0 ; j < rsrces ; j++ )
{
             scanf ( "%d", &max_claim[i][j] ) ;
         }
     }
 
printf ( "\nThe Claim Vector is: " ) ;
     for ( i = 0 ; i < rsrces ; i++ )
{
        printf ( "\t%d", maxres[i] ) ;
}
 
     printf ( "\nThe Allocated Resource Table:\n" ) ;
     for ( i = 0 ; i < prcesses ; i++ )
{
        for  ( j = 0 ; j < rsrces ; j++ )
{
             printf ( "\t%d", curr[i][j] ) ;
         }
printf ( "\n" ) ;
     }
 
     printf ( "\nThe Maximum Claim Table:\n" ) ;
     for  ( i = 0 ; i < prcesses ; i++ )
{
         for  ( j = 0 ; j < rsrces ; j++ )
{
        printf ( "\t%d", max_claim[i][j] ) ;
         }
         printf ( "\n" ) ;
     }
 
     for  ( i = 0 ; i < prcesses ; i++ )
{
         for  ( j = 0 ; j < rsrces ; j++ )
{
             allocation[j] += curr[i][j] ;
         }
     }
 
     printf ( "\nAllocated resources:" ) ;
     for  ( i = 0 ; i < rsrces ; i++ )
{
         printf ( "\t%d", allocation[i] ) ;
     }
 
     for ( i = 0 ; i < rsrces ; i++ )
{
        avlble[i] = maxres[i] - allocation[i] ;
}
 
     printf ( "\nAvailable resources:" ) ;
     for ( i = 0 ; i < rsrces ; i++ )
{
         printf ( "\t%d", avlble[i] ) ;
     }
     printf ( "\n" ) ;
 
     while ( counter != 0 )
{
         safe = 0 ;
         for ( i = 0 ; i < prcesses ; i++ )
{
             if ( running[i] )
{
                 exec = 1 ;
                 for ( j = 0 ; j < rsrces ; j++ )
{
                     if ( max_claim[i][j] - curr[i][j] > avlble[j] )
{
                         exec = 0 ;
                         break ;
                     }
                 }
                 if ( exec )
{
                     printf ( "\nProcess%d is executing\n", i + 1 ) ;
                     running[i] = 0 ;
                     counter-- ;
                     safe = 1 ;
 
                     for ( j = 0 ; j < rsrces ; j++ )
{
                         avlble[j] += curr[i][j] ;
                     }
                break ;
                 }
             }
         }
         if ( !safe )
{
             printf ( "\nThe processes are in unsafe state.\n" ) ;
             break ;
         }
else
{
             printf ( "\nThe process is in safe state" ) ;
             printf ( "\nAvailable vector:" ) ;
 
             for ( i = 0 ; i < rsrces ; i++ )
{
                 printf ( "\t%d", avlble[i] ) ;
             }
 
        printf ( "\n" ) ;
         }
     }
     return 0 ;
}

Input:

Enter number of processes: 2


Enter number of resources: 2


Enter Claim Vector:1

Output:

Enter Allocated Resource Table:


Enter Maximum Claim Table:


The Claim Vector is:    1       0
The Allocated Resource Table:
        0       0
        0       0


The Maximum Claim Table:
        0       0
        0       0


Allocated resources:    0       0
Available resources:    1       0


Process1 is executing


The process is in safe state
Available vector:       1       0


Process2 is executing


The process is in safe state
Available vector:       1       0

Explanation:

  • The processes must be able to predict the maximum quantity of resources needed as they come into the system, which can be done in a timely manner.
  • This technique maintains a fixed number of processes, unlike interactive systems.
  • This strategy only works if a certain amount of resources are accessible to disperse. In the event that a device malfunctioned and unexpectedly went down, the algorithm would not work.
  • When there are multiple processes and resources, the method will incur overhead costs because it needs to be called for each process.

Related Topics

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.