C Program to Simulate Banker's Algorithm | Explicitly Commented(Easy to Understand)

 

Are you finding it difficult to understand the logic of Banker's algorithm?

Are you struggling to simulate Banker's algorithm using C programming language?

Then you are in the right place.

In this article, we are going to see the C program to simulate Banker's algorithm in the simplest way possible. We have checked out many other websites and articles related to this only to discover that none of those codes are explicitly commented and the code seems difficult to understand for someone who is new to coding.



So, we have decided to write our own version of the code to simulate the Banker's Algorithm using C.

We have tried our best to comment out each line possible in order to make it more and more understandable. The logic is very clear if you can read all those comments. So try to read them.

Also, feel free to let us know if you have any doubts regarding this. We are always happy to help.

You can click on the "Open in OnlineGDB" button to view and execute the code in onlineGDB. Try it out. 


Open in OnlineGDB

// C Program to Simulate Banker's Algorithm - CODE BY Nived Kannada
#include<stdio.h>
void main()
{   //Here we need 3 arrays namely Allocation, Max and Available
    // arrays alloc and max are 2D arrays. array 'available' is a 1D array.
    int n, m, i, j, k, alloc[20][20], max[20][20], available[20];
    int f[20],ans[20], ind=0, need[20][20]; //We need the Need matrix.
    
    //Reading the number of Processes from the input.
    printf("Enter number of processes: ");
    scanf("%d",&n);
    
    //Reading the number of Resources from the input.
    printf("Enter the number of Resources: ");
    scanf("%d",&m);
    
    //Reading the Allocation Values to the Matrix 'alloc[][]'
    printf("Enter the Values of Allocation Matrix: \n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf("Enter value at position (%d%d) :",i+1,j+1);
            scanf("%d",&alloc[i][j]);
        }
    }
    
    //Reading the Max values to the matrix 'max[][]'
        printf("Enter the Values of Max Matrix: \n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf("Enter value at position (%d%d) :",i+1,j+1);
            scanf("%d",&max[i][j]);
        }
    }
    
    //Reading the values of array available[]
    printf("Enter the values in available: \n");
    for(j=0;j<m;j++)
    {
        printf("Enter value at position (%d) :",j+1);
        scanf("%d",&available[j]);
    }
    
    //We are using an array f to represent the finished status of each process. 
    //Initially setting all processes as not finished. ie. Setting f[i]=0 for each process i.
    for(k=0;k<n;k++)
    {
        f[k]=0;
    }
    
    //Calculating values of the NEED MATRIX using its equation, for all processes
    //Equation is need[i][j] = max[i][j] - allocation[i][j]
    for(i=0;i<n;i++)    //For each process
    {
        for(j=0;j<m;j++)    //For each resource
        {
            need[i][j] = max[i][j] - alloc[i][j];
        }
    }
    
    //Finding safe sequence
    int y=0;
// ans[] array will be used to store the SAFE SEQUENCE in the end. 
    for(k=0;k<n;k++)    
    {
        for(i=0;i<n;i++)    //For each process
        {
            if(f[i]==0)     
            {
                int flag = 0;   //setting flag as 0 or false.
                for(j=0;j<m;j++)    //For each Resource
                {
                    if(need[i][j] > available[j])    //If Need greater than Available, then
                    {
                        flag=1; //Setting flag as true or 1.
                        //flag=1 means the Need is greater than what is Available for that particular resource.
                        break; //Breaking out of this loop if need > available
                    }
                }
                
                if(flag==0)
                {
                    ans[ind++] = i;
                    for(y=0;y<m;y++)    //For each Resource
                    {
                        available[y] = available[y] + alloc[i][y];  //Setting availability to current availability + allocation 
                    }
                    f[i]=1; //Declaring the current process as FINISHED.
                }
            }
        }
    }
    
   //Displaying the SAFE SEQUENCE.
    printf("The SAFE SEQUENCE is: \n");
    for(i=0;i<n-1;i++)  //Here loop ends at n-1 because we don't want to printf the arrowmark(->) at the end.printf(" P%d", answer[n-1)
    {
        printf(" P%d ->", ans[i]);
    }
    printf(" P%d", ans[n-1]);    //Printing the final state in safe sequence without printing the arrowmark.

}


/* CODE ENDS HERE */
/* Code written and explicitly commented by Nived Kannada */

Output




This code was written by Nived Kannada.

No comments:

Post a Comment

Pages