Вы находитесь на странице: 1из 5

BANKER`S ALGORITHM

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:

How much of each resource each process could possibly request[MAX]

How much of each resource each process is currently holding[ALLOCATED]

How much of each resource the system currently has available[AVAILABLE]

Resources may be allocated to a process only if it satisfies the following conditions:


1. request max, else set error condition as process has crossed maximum claim made
by it.
2. request available, else process waits until resources are available.

PROGRAM TO IMPLEMENT BANKERS ALGORITHM


#include<iostream.h>
#include<conio.h>
int main()
{
bool p[5];
p[0]=p[1]=p[2]=p[3]=p[4]=false;
int resource[4];
resource[0]=resource[1]=resource[2]=resource[3]=0;
int allocated[5][4]={0}, need[5][4]={0}, available[4]={0};
int i=0, j=0;
for(; i<4; ++i)
{
cout << Enter the instances of Resource << i+1 << ;
cin >> resource[i];
}
cout << \nEnter the allocation matrix : \n;
for(i=0; i<5; ++i)
{
for(j=0; j<4; ++j)
{
cin >> allocated[i][j];
}
}
cout << \nEnter the need matrix : \n;
for(i=0; i<5; ++i)
{
for(j=0; j<4; ++j)
{
cin >> need[i][j];
}
}
for(i=0; i<4; ++i)
{

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

Вам также может понравиться