Banker’s Algorithm in C

Here you will get a program for investor’s calculation in C.

The financier’s calculation which is otherwise called evasion calculation is a gridlock identification calculation. It was created by Edsger Dijkstra. It is intended to check the protected state at whatever point an asset is mentioned.

It takes similarity of the bank, where client solicitation to pull back money. In light of certain information, the money is loaned to the client.

Banker’s Algorithm in C

The broker can’t give more money than what the client has mentioned for, and the absolute accessible money. As this calculation uses bank similarity so named as broker’s calculation

The fundamental information structures used to actualize this calculation are given beneath.

Give n a chance to be the all outnumber of procedures and m be the complete number of asset types in the framework.

Accessible: A vector of length m. It shows a number of accessible assets of each sort. On the off chance that Available[i] = k, at that point k occasions of asset Ri are accessible.

Max: A n×m grid that contains the most extreme interest of each procedure. On the off chance that Max[i,j] = k, at that point procedure Pi can demand most extreme k cases of asset type Rj.

Portion: A n×m network that contains a number of assets of each kind right now allotted to each procedure. On the off chance that Allocation[i,j] = k, at that point Pi is at present dispensed k occurrences of asset type Rj.

Need: A n×m framework that shows the rest of the asset need of each procedure. In the event that Need[i,j] = k, at that point procedure Pi may require k more examples of asset type Rj to finish the undertaking.

Program for Banker’s Algorithm in C

#include <stdio.h>
 
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
 
int main()
{
	printf("\nEnter number of processes: ");
    	scanf("%d", &processes);
 
    	for (i = 0; i < processes; i++) 
	{
        	running[i] = 1;
        	counter++;
    	}
 
    	printf("\nEnter number of resources: ");
    	scanf("%d", &resources);
 
    	printf("\nEnter Claim Vector:");
    	for (i = 0; i < resources; i++) 
	{ 
	        scanf("%d", &maxres[i]);
    	}
 
   	printf("\nEnter Allocated Resource Table:\n");
    	for (i = 0; i < processes; i++) 
	{
	        for(j = 0; j < resources; j++) 
		{
  			scanf("%d", &current[i][j]);
        	}
    	}
 
    	printf("\nEnter Maximum Claim Table:\n");
    	for (i = 0; i < processes; i++) 
	{
        	for(j = 0; j < resources; j++) 
		{
            		scanf("%d", &maximum_claim[i][j]);
        	}
    	}
 
	printf("\nThe Claim Vector is: ");
    	for (i = 0; i < resources; i++) 
	{
	        printf("\t%d", maxres[i]);
	}
 
    	printf("\nThe Allocated Resource Table:\n");
    	for (i = 0; i < processes; i++) 
	{
	        for (j = 0; j < resources; j++) 
		{
            		printf("\t%d", current[i][j]);
        	}
		printf("\n");
    	}
 
    	printf("\nThe Maximum Claim Table:\n");
    	for (i = 0; i < processes; i++) 
	{
        	for (j = 0; j < resources; j++) 
		{
		        printf("\t%d", maximum_claim[i][j]);
        	}
        	printf("\n");
    	}
 
    	for (i = 0; i < processes; i++) 
	{
        	for (j = 0; j < resources; j++) 
		{
            		allocation[j] += current[i][j];
        	}
    	}
 
    	printf("\nAllocated resources:");
    	for (i = 0; i < resources; i++) 
	{
        	printf("\t%d", allocation[i]);
    	}
 
    	for (i = 0; i < resources; i++) 
	{
	        available[i] = maxres[i] - allocation[i];
	}
 
    	printf("\nAvailable resources:");
    	for (i = 0; i < resources; i++) 
	{
        	printf("\t%d", available[i]);
    	}
    	printf("\n");
 
    	while (counter != 0) 
	{
        	safe = 0;
        	for (i = 0; i < processes; i++) 
		{
            		if (running[i]) 
			{
                		exec = 1;
                		for (j = 0; j < resources; j++) 
				{
                    			if (maximum_claim[i][j] - current[i][j] > available[j]) 
					{
                        			exec = 0;
                        			break;
                    			}
                		}
                		if (exec) 
				{
                    			printf("\nProcess%d is executing\n", i + 1);
                    			running[i] = 0;
                    			counter--;
                    			safe = 1;
 
                    			for (j = 0; j < resources; j++) 
					{
                        			available[j] += current[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 < resources; i++) 
			{
                		printf("\t%d", available[i]);
            		}
 
		        printf("\n");
        	}
    	}
    	return 0;
}

On the off chance that you discovered anything mistaken in the above program for financier’s calculation in C at that point remark beneath.

Recent Posts

error: Alert: Content is protected!!