ads

Bankers Algorithm

 Bankers Algorithm


Banker's algorithm is used for deadlock avoidance.




The Banker's algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It is designed to ensure that a system can safely allocate resources to processes without causing deadlocks or other system failures.

The Banker's algorithm works by keeping track of the available resources in the system and the resource requests made by each process. It then simulates the allocation of resources to each process to determine whether the system is in a safe state, meaning that it can allocate resources to each process without causing a deadlock.


The algorithm uses a number of data structures to keep track of the resource requests and the resources available. These data structures include the following:

  • Available: a vector that indicates the number of available resources of each type.
  • Allocation: a matrix that indicates the number of resources of each type currently allocated to each process.
  • Need: a matrix that indicates the number of resources of each type that each process still needs to complete its task.


The Banker's algorithm works by iterating through the processes and checking whether their resource requests can be granted without causing a deadlock. If a process can be granted the resources it needs, the system allocates the resources and marks them as unavailable. If not, the process is blocked and added to a waitlist.

The algorithm continues to iterate through the processes until all the processes have been allocated the resources they need or until it determines that the system is in an unsafe state. If the system is in an unsafe state, it rolls back the allocation of resources and blocks the processes until the necessary resources become available.

Overall, the Banker's algorithm is a useful tool for ensuring that a system can allocate resources to processes safely and avoid deadlocks. However, it does have some limitations, such as the assumption of fixed resource requirements and the need for processes to declare their maximum resource needs upfront.


// Bakers Algorithm
#include<stdio.h>
#include<stdlib.h>


 int i,j,n=5,m=3;
 int max[5][3];
 int allo[5][3];
 int avail[3];
 int need[5][3];
 
void accept_allo(){
    printf("\nEnter the  5 rows and 3 cols for Allocation matrix ");
    for (i=0;i<5;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&allo[i][j]);    
        }
    }
}
void accept_max(){
    printf("\nEnter the 5 rows and 3 cols max matrix\n");
    for (i=0;i<5;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&max[i][j]);
        }
    }
}
   
   
void accept_avail(){
    printf("\nEnter 3 availble resources: ");
    for (i=0;i<3;i++)
        scanf("%d",&avail[i]);  
    }
void print_avail(){
    printf("[");
    for (i=0;i<3;i++)
    {
        printf(" %d ",avail[i]);    
    }
    printf("]");
}
   
void display_matrix(int t[][3],int n ,int m){

    for (i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf(" %d ",t[i][j]);
        }
        printf("\n");
    }
}

void find_need(){

    int f[10],k;    
   
    for (i = 0; i < 5; i++)
        f[i] = 0;
       
     for (i=0;i<5;i++)
        for(j=0;j<3;j++)
            need[i][j]=max[i][j]-allo[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) {
                    for (j = 0; j < m; j++)
                        avail[j] += allo[i][j];
                    f[i] = 1;
                }
            }
        }
    }
}
   



void main()
{
    accept_avail();
    print_avail();
    accept_allo(5,3);
    printf("\nGiven Allocation matrix\n ");
    display_matrix(allo,5,3);
    accept_max();
    printf("\nGiven Max matrix\n ");
    display_matrix(max,5,3);
    printf("Need matric\n");
    find_need();
    display_matrix(need,5,3);
    print_avail();
}




// Need And Available
#include<stdio.h>
#include<stdlib.h>
 int i,j,n,m;
 int max[5][3];
 int allo[5][3];
 int avail[3];
 int need[5][3];
 
void accept_allo(){
    printf("\nEnter the  5 rows and 3 cols for Allocation matrix ");
    for (i=0;i<5;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&allo[i][j]);    
        }
    }
}
void accept_max(){
    printf("\nEnter the 5 rows and 3 cols max matrix\n");
    for (i=0;i<5;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&max[i][j]);
        }
    }
}
   
void accept_avail(){
    printf("\nEnter 3 availble resources: ");
    for (i=0;i<3;i++)
        scanf("%d",&avail[i]);  
    }
void print_avail(){
    printf("[");
    for (i=0;i<3;i++)
    {
        printf(" %d ",avail[i]);    
    }
    printf("]");
}
   
void display_matrix(int t[][3],int n ,int m){

    for (i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf(" %d ",t[i][j]);
        }
        printf("\n");
    }
}

void find_need(){
     for (i=0;i<5;i++)
        for(j=0;j<3;j++)
            need[i][j]=max[i][j]-allo[i][j];
}

void main()
{
    accept_avail();
    print_avail();
    accept_allo(5,3);
    printf("\nGiven Allocation matrix\n ");
    display_matrix(allo,5,3);
    accept_max();
    printf("\nGiven Max matrix\n ");
    display_matrix(max,5,3);
    printf("Need matric\n");
    find_need();
    display_matrix(need,5,3);

}




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!