C Program to implements Banker's Algorithm

Banker'S Algorithm The Bankers Algorithm is a resource allocation and standstill algorithm developed by Edsger Dijkstra to test and simulate the use of a bank of bankers for a pre-defined maximum amount of resources. The algorithm performs an s - state check in which all other pending activities are checked for possible stoppages and conditions before deciding whether to continue the assignment. Once the algorithm has determined that the process is in a safe state, it allocates resources to it. If a process is terminated before it reaches its maximum resources, this makes it easier for the system to allocate resources from the resources it currently has. Given these assumptions, the algorithm determines which state is safe by trying to find a process that allows it to reach its maximum resources and then terminate them and return them to the system. If it is possible for the process to be terminated before it is terminated, then the state can be considered safe. In this example, we show that the states listed in the previous example are safe states by showing that it is possible to acquire the maximum resource. This shows that they are a safe state, because we have shown that in a state that is' safe ', it would theoretically not be possible to conduct a trial even under the right conditions. Since the system cannot know when a process ends and how many resources it then requests, it can only assume that the processes will ultimately try to obtain the maximum resources of their state. A reasonable assumption is that the system is unaware of how long a process is running or how many resources it needs. The bankers "algorithm can be described as a dead end - an algorithm to avoid resource allocation that ensures the safety of execution by simulating the maximum already determined, but also establishing a system (s) of the state by checking the number of pending processes and the amount of resources available to them. The number of resources may not increase while it is being executed, and no additional processes are started while they are being executed. We need to know how many resources a process might be able to retrieve, but for most system processes this varies dynamically. In this algorithm, the number of processes is static, so we do not allow them to "go up" while we are running. When the system receives a request for a resource, it executes the banker algorithm to determine whether it is safe to comply with the request. The algorithm is relatively simple in understanding the distinction between safe and unsafe states. The ability of the system to reject or postpone impossible or insecure requests is an operating system-specific decision. The security algorithm is used to determine the state of a system; that is, it can be either in a safe or unsafe state. When process P makes a request for a resource, the following measures are taken: If the resulting resource allocation (stale) is secure, it assigns its resources and completes the transaction. If the new state is unsafe, P waits for the request and the old resource allocation state has been restored. When the system receives a request for a resource, it executes the banker algorithm to determine whether it is safe to comply with the request. The algorithm is pretty straightforward - forward if you understand the distinction between safe and unsafe states. An example of an uncertain state is the question of what would happen if process 2 initially contained 1 or more units of resource B. It is an operating system - a specific decision for the system to reject an impossible or insecure request and make it wait for its next available resource. Given this assumption, the algorithm determines which state is safe by trying to find a process that allows it to acquire its maximum resource and then terminate the resource and return it to the system. The state specified in the previous example is presented as a safe state by showing that it is possible for the process to obtain the maximum resources. If the "safe state" is processed in a prefabricated queue, it can be considered a decision-maker. Since the system cannot know when the process will be terminated or how many resources it will request, it assumes that the processes will eventually try to reach their specified maximum resources. If it is possible for a process to be completed and then terminated, then the state is considered safe. For example, in an uncertain state, consider what happens if Process 2 initially contains 2 units of resource B. This is a reasonable assumption, as the system is not aware of how long the process will take or how many units of resource B are present. In addition, information about the region of maximum resource usage can be extracted before the process is executed. If a process is terminated before it reaches all its maximum resources, this makes the systems easier

    
/* objective : To implements Banker's Algorithm
  Author: Rahul kumar */
#include <stdio.h>
#define max 100
int main()
{
    int NoP,NoR,i,j,flag,count;
    int SS[max],PC[max],tot_res[max],rem_tot_res[max],alloc_res[max][max],rem_need[max][max],max_need[max][max];
    
    printf("Please enter no. of processes: \n ");
    scanf("%d",&NoP);
    printf("Please enter no. of resources: \n");
    scanf("%d",&NoR);
    printf("\n");
    if(NoP>max || NoR>max)
    {
        printf("Limit exceeded");
    }
    else if(NoP<1 || NoR<1){
        printf("Not sufficient process or resource");
    }
    else
    {
        for(i=0;i< NoR; i++)
        {
            printf("Please enter the max no. of instances of resources %d: ",i+1);
            scanf("%d",&tot_res[i]);
            rem_tot_res[i]=tot_res[i];
        }
        for(i=0;i< NoP ; i++)
        {
            printf("Please enter the deatils for process %d: \n",i+1);
            PC[i]=0;
            for(j=0;j< NoR; j++){
                printf("\nInstances of Resources %d allocated: ",j+1);
                scanf("%d",&alloc_res[i][j]);
                printf("\n max need of instances of resources %d required: ",j+1);
                scanf("%d",&max_need[i][j]);
                rem_tot_res[j]-=alloc_res[i][j];
                rem_need[i][j]=max_need[i][j]-alloc_res[i][j];
            }
        }
    
        printf("\n");
        count=0;
        while(count< NoP)
        {
            for(i=0;i< NoP; i++)
            {
                flag=0;
                if(PC[i]==0)
                {
                    for(j=0;j< NoR; j++)
                    {
                        if(rem_tot_res[j]< rem_need[i][j])
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0)
                    {
                        SS[count]=i;
                        count++;
                        PC[i]=1;
                        for(j=0;j< NoR; j++)
                        {
                            rem_need[i][j]=0;
                            rem_tot_res[j]+=alloc_res[i][j];
                            alloc_res[i][j]=0;
                        }
                    }
                }
            }
        }
        printf("Safe Sequence: ");
        for(i=0;i< NoP; i++)
            {
                printf("P%d\t",SS[i]);
        }
    }
    return 0;
}
    


Output:

Please enter no. of processes:
 4
Please enter no. of resources:
3

Please enter the max no. of instances of resources 1: 10
Please enter the max no. of instances of resources 2: 5
Please enter the max no. of instances of resources 3: 7
Please enter the deatils for process 1:

Instances of Resources 1 allocated: 0

 max need of instances of resources 1 required: 7

Instances of Resources 2 allocated: 1

 max need of instances of resources 2 required: 5

Instances of Resources 3 allocated: 0

 max need of instances of resources 3 required: 3
Please enter the deatils for process 2:

Instances of Resources 1 allocated: 2

 max need of instances of resources 1 required: 3

Instances of Resources 2 allocated: 0

 max need of instances of resources 2 required: 2

Instances of Resources 3 allocated: 0

 max need of instances of resources 3 required: 2
Please enter the deatils for process 3:

Instances of Resources 1 allocated: 3

 max need of instances of resources 1 required: 9

Instances of Resources 2 allocated: 0

 max need of instances of resources 2 required: 0

Instances of Resources 3 allocated: 2

 max need of instances of resources 3 required: 2
Please enter the deatils for process 4:

Instances of Resources 1 allocated: 2

 max need of instances of resources 1 required: 2

Instances of Resources 2 allocated: 1

 max need of instances of resources 2 required: 2

Instances of Resources 3 allocated: 1

 max need of instances of resources 3 required: 2

Safe Sequence: P1       P3      P0      P2