×

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

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.