You are on page 1of 6

BANKERS AND SAFETY ALGORITM:

AIM: Simulation of Bankers and Safety algoritms for given n-process, m-resources of
multiple instance environments (Generate processes, resources, instances randomly)

ALGORITHM:
1. Read number of processes(n) and number of resources(m) and generate resourcfe
instances randomly.
The data structures needed to implement the algorithm

Available: A vector of length m indicates the number of available resources of each


type.

Max: An nxm matrix defines the number of resources of eac type currently allocates
to each process

Allocation: An nxm matrix defines the number of resources of each type currently
allocated to each process

Need: An nxm matrix indicates the remaining resources of each process.

Need[I,j]=max[I,j]-allocation[I,j].
2. Read the matrix and allocation matrix, compute the available matrix and need
matrix from those.

SAFETY ALGORITHM:

1. Let work and finish be vectors of length m and n respectively. Initialize


work:=available and finish[I}=false for I=1,2,n
2. Find an I such that both finish[I]=flase and need[I]<=work if no such I, exists goto
step
3. Work:=work+allocation : finish[I}=true goto step 3
4. I finish[I]=true for all I, then the system is in a safe state.

Let request j be the request vector for process pi. If request I[j]=k then process pi wants k
instancesof resource type Rj.

BANKERS ALGORITHM:

1. If request I<=need I, goto step2. Otherwise, raise an error condition , since the
process has exceeded its maximum claim.
2. If request I<=available, goto step3, otherwise pi must wait since the resources are
not avialble
3. Have the system presented to have allocated the requested resources to process i by
modifying the state as follows;

Available:=available-request i
Allocation i:=allocation i+request i
Need :=needi-request i

If the resulting resource allocation stae is safe, the transaction is competed and process pi
is allocated its resources. Howevr if the new state is u nsafe, then pi must wait for request I
and the oldallocation state is restored.
/*BANKERS ALGOTITM FOR DEADLOCK AVOIDANCE */

#include <stdio.h>
main()
{
int p,r,m[10][10],a[10][10],n[10][10],i,j,ri

printf ("enter the number of processes"); // entering the number of process and
resources//
scanf ("%d",&p);
printf ("enter the number of resources");
scanf ("%d",&r);
printf ("enter the max matrix"); // entering the max matrix//
for (i=0;i<p;i++)
{
for (j=0;j<r;j++)
{
scanf ("%d",&m[i][j]);
}
}
printf ("enter the instances of the resource);
for (i=0;i<r;i++)
{
scanf("%d",&ri[i]);
}
printf ("enter the allocation matrix"); // entering the allocation matrix //
for (i=0;i<p;i++)
{
for (j=0;j<r;j++)
{
scanf ("%d",&a[i][j]);
}
}
for (i=0;i<p;i++)
{
for (j=0;j<r;j++)
{
n[i][j]=m[i][j]-a[i][j];
}
}

for (i=0;i<r;i++)
av[i]=0;
for (j=0;j<r;j++)
{
for (i=0; i<p;i++)
{
av[j]=av[j]+a[i][j];
}
}
for (i=0;i<r;i++)
{
av[i]=ri[i]-av[i];
}
for (i=0;i<p;i++)
{
f[i]=0;
}
c=0;
for (k=0;k<p;k++)
{
for (i=0;i<p;i++)
{
if (f[i]==0)
{
for (j=0;j<r;j++)
{

if (av[j] >= n[i][j])


{
c=c+1;
}
}

if (c==r)
{
f[i]=1;
for (j=0;j<r;j++)
av[j]=av[j]+a[i][j];
}
c=0;
}
}
}
for (i=0;i<p;i++)
{
printf ("%4d",f[i]);
}
c=0;
for (i=0;i<p;i++)
{
if (f[i]==1)
c=c+1;
}
if(c==p)
printf ("system is safe");
else
printf ("system is unsafe");
}

Output: enter the number of processes 5


enter the number of resources 3
enter the max matrix
864
431
923
333
544
enter the instances of the resources 10 5 7
enter the allocation matrix
010
200
302
211
002
0 1 1 1 1
system is unsafe

You might also like