Bankers Algorithm (Implementation in C++)
Bankers Algorithm (Implementation in C++)
The Bankers Algorithm is a deadlock avoidance algorithm developed by Edsger Dijkstra that
tests for safety by simulating the allocation of predetermined maximum possible amounts of
all resources, and then makes an "s-state" check to test for possible deadlock conditions for
all other pending activities, before deciding whether allocation should be allowed to continue.
The algorithm was developed in the design process for the THE operating system When a
new process enters a system, it must declare the maximum number of instances of each
resource type that it may ever claim; clearly, that number may not exceed the total number of
resources in the system. Also, when a process gets all its requested resources it must return
them in a finite amount of time.
For the Banker's algorithm to work, it needs to know three things:
int sum=0;
for(j=0; j<5; ++j)
{
sum += allocated[j][i];
}
available[i]=resource[i]-sum;
}
i=0;
while(!p[0] || !p[1] || !p[2] || !p[3] || !p[4])
{
int cond=1;
if(!p[i%5])
{
for(j=0; j<4; ++j)
{
if(available[j] >= need[i%5][j])
{
cond=1;
}
else
{
cond=0;
break;
}
}
if(cond)
{
p[i%5]=true;
cout << \nProcess << (i%5)+1 << has completed\n;
for(int k=0; k<4; ++k)
{
available[k] += allocated[i%5][k];
}
}
}
++i;
}
return 0;
}
OUTPUT