0% found this document useful (0 votes)
2K views

Bankers Algorithm in C

This C program implements the Banker's algorithm to check if a system is in a safe state. It takes input for the number of processes and resources, allocation and maximum claim matrices, and resource vector. It then calculates the need matrix and available resources. It checks if all needs of a process can be met by available resources to determine the safe sequence of completing processes. The program outputs the initial inputs and checks if the system is in a safe or unsafe state.

Uploaded by

Chaman Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Bankers Algorithm in C

This C program implements the Banker's algorithm to check if a system is in a safe state. It takes input for the number of processes and resources, allocation and maximum claim matrices, and resource vector. It then calculates the need matrix and available resources. It checks if all needs of a process can be met by available resources to determine the safe sequence of completing processes. The program outputs the initial inputs and checks if the system is in a safe or unsafe state.

Uploaded by

Chaman Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<conio.h>
void main()
{
int aloc[10][10],need[10][10],max[10][10],av[10],rv[10];
int nr=0,np=0,complete[10];
int i=0,j=0,sum=0,ctr=0;
clrscr();
//GETTING REQUIRED DATA....
printf("\nEnter num of process: ");
scanf("%d",&np);
printf("\nEnetr num of resources: ");
scanf("%d",&nr);
if(np==0 || nr==0)
{
printf("\nwrong input...\nplease enter correct value");
getch();
return;
}
printf("\nEnter resource vector: ");
for(i=0;i<nr;i++)
{
scanf("%d",&rv[i]);
}
printf("\nEnter max claim matrix: ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter allocation matrix: ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&aloc[i][j]);
}
}
//find need matrix...
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
need[i][j] = 0;
need[i][j] = max[i][j] - aloc[i][j] ;
}
}
//find avilable resources..
for(i=0;i<nr;i++)
{
av[i]=0;
for(j=0;j<np;j++)
{
sum = sum + aloc[j][i] ;
}
av[i] = rv[i] - sum ;
sum=0;
}
//initialising complete vector...
for(i=0;i<np;i++)
{
complete[i]=0;
}

//DISPLAY THE INPUT WITH INITIAL SITUATION...


printf("\nALOC_MATRIX\tMAX_CLAIM\tNEED");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
printf("%d ",aloc[i][j]);
}
printf("\t\t ");
for(j=0;j<nr;j++)
{
printf("%d ",max[i][j]);
}
printf("\t ");
for(j=0;j<nr;j++)
{
printf("%d ",need[i][j]);
}
}
printf("\nResource vector: ");
for(i=0;i<nr;i++)
{
printf("%d ",rv[i]);
}
printf("\nAvailable resources: ");
for(i=0;i<nr;i++)
{
printf("%d ",av[i]);
}

//FINDING SAFE SEQUENCE...


again:
for(i=0;i<np;i++)
{
if(complete[i]==1)
{
continue;
}
else
{
for(j=0;j<nr;j++)
{
if(need[i][j] > av[j])
{
ctr++;
if(ctr==np)
{
printf("\nSystem is in unsafe state...");
getch();
return;
}
goto next;
}
}
for(j=0;j<nr;j++)
{
av[j] = av[j] + aloc[i][j];
aloc[i][j] = 0;
}
complete[i] = 1;
printf("\nProcess %d is complete...",i);
goto again;
}
next :
}
//checking if any process left...
for(i=0;i<nr;i++)
{
if(complete[i]==0)
{
goto again;
}
}
printf("\n\nSYSTEM IS IN SAFE STATE...");
getch();
}

You might also like