----------------------------------------------------------------------------
Title - Write a program to implement Banker’s Algorithm
----------------------------------------------------------------------------
------------------------
MAIN PROGRAM a9.c
------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int max[20][20],allo[20][20],need[20][20],i,j,k,np,ava[10],ava1[20],sum,nr;
int m,rem,flag,sequence[20],m1,flag1=1;
clrscr();
printf("\\n Enter the No. of Processor : ");
scanf("%d",&np);
printf("\\n Enter the No. of Resoucres : ");
scanf("%d",&nr);
/*printf("\\nEnter the process name: ");
for(i=0;i<np;i++)
{
scanf("%s",&p[i]);
}*/
/* printf("\\nEnter the resources name: ");
for(j=0;j<nr;j++)
{
scanf("%s",&r[j]);
}*/
printf("\\n Enter the No. of instance for each Resources : ");
for(i=0;i<nr;i++)
{
printf("\\n Resource r%d : ",i);
scanf("%d",&ava[i]);
}
/*printf("\\nInstance for each resources: ");
for(j=0;j<nr;j++)
{
printf("\\nResource r%d: ",j);
printf("%d\\t",ava[j]);
}*/
for(i=0;i<np;i++)
{
printf("\\n\\n Enter the Maximum Resources for Process p%d : ",i);
for(j=0;j<nr;j++)
{
printf("\\n Resource r%d : ",j);
scanf("%d",&max[i][j]);
}
}
printf("\\n\\n Max. Matrix : \\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
printf(" %d \\t",max[i][j]);
}
printf("\\n");
}
for(i=0;i<np;i++)
{
printf("\\n\\n Enter the Allocation Resources for Process p%d : ",i);
for(j=0;j<nr;j++)
{
flag1=1;
while(flag1==1)
{
printf("\\nResource r%d: ",j);
scanf("%d",&m1);
if(m1<=max[i][j])
{
allo[i][j]=m1;
flag1=0;
}
else
{
printf("\\n Enter Value less than the Max value..");
flag1=1;
}
}
// allo[i][j]=m1;
}
}
printf("\\n\\n Allocation Matrix : \\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
printf(" %d \\t",allo[i][j]);
}
printf("\\n");
}
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
need[i][j]=max[i][j]-allo[i][j];
}
}
printf("\\n\\n Need Matrix : \\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
printf(" %d \\t",need[i][j]);
}
printf("\\n");
}
printf("\\n\\n Available Resources : ");
for(j=0;j<nr;j++)
{
sum=0;
for(i=0;i<np;i++)
{
sum=sum+allo[i][j];
}
ava1[j]=ava[j]-sum;
printf("\\t%d",ava1[j]);
}
for(k=0;k<np;k++)
{
for(i=0;i<np;i++)
{
m=0;
for(j=0;j<nr;j++)
{
if(need[i][j]!=-1 && need[i][j]<=ava1[m])
{
flag=1;
rem=i;
}
else
{
flag=0;
break;
}
m++;
}
if(flag==1)
break;
}
printf("\\n\\n p%d Executed ",rem);
sequence[k]=rem;
if(flag==1)
{
for(j=0;j<nr;j++)
{
need[rem][j]=-1;
}
printf("\\n\\n Need matrix : \\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
printf("%d\\t",need[i][j]);
}
printf("\\n");
}
printf("\\n\\nAvailable resources: ");
for(j=0;j<nr;j++)
{
ava1[j]=ava1[j]+allo[rem][j];
printf("\\t %d ",ava1[j]);
}
}
}
if(flag==1)
{
printf("\\n\\n\\nSafe sequence is:\\n");
for(j=0;j<np;j++)
printf("p%d\\t ",sequence[j]);
}
printf("\\n");
if(flag==0)
printf("\\nresource not available unsafe state\\n");
getch();
}
------------------------
ENND OF THE PROGRAM
------------------------
/*
------------------------
OUTPUT
------------------------
mca@mca-VirtualBox:~/Desktop$ gcc a9a.c
mca@mca-VirtualBox:~/Desktop$ ./a.out
Enter the No. of Processor : 4
Enter the No. of Resoucres : 3
Enter the No. of instance for each Resources :
Resource r0 : 9
Resource r1 : 3
Resource r2 : 6
Enter the Maximum Resources for Process p0 :
Resource r0 : 3
Resource r1 : 2
Resource r2 : 2
Enter the Maximum Resources for Process p1 :
Resource r0 : 6
Resource r1 : 1
Resource r2 : 3
Enter the Maximum Resources for Process p2 :
Resource r0 : 3
Resource r1 : 1
Resource r2 : 4
Enter the Maximum Resources for Process p3 :
Resource r0 : 4
Resource r1 : 2
Resource r2 : 2
Max. Matrix :
3 2 2
6 1 3
3 1 4
4 2 2
Enter the Allocation Resources for Process p0 :
Resource r0: 1
Resource r1: 0
Resource r2: 0
Enter the Allocation Resources for Process p1 :
Resource r0: 6
Resource r1: 1
Resource r2: 2
Enter the Allocation Resources for Process p2 :
Resource r0: 2
Resource r1: 1
Resource r2: 1
Enter the Allocation Resources for Process p3 :
Resource r0: 0
Resource r1: 0
Resource r2: 2
Allocation matrix :
1 0 0
6 1 2
2 1 1
0 0 2
Need matrix :
2 2 2
0 0 1
1 0 3
4 2 0
Available Resources : 0 1 1
p1 Executed
Need matrix :
2 2 2
-1 -1 -1
1 0 3
4 2 0
Available resources: 6 2 3
p0 Executed
Need matrix :
-1 -1 -1
-1 -1 -1
1 0 3
4 2 0
Available resources: 7 2 3
p2 Executed
Need matrix :
-1 -1 -1
-1 -1 -1
-1 -1 -1
4 2 0
Available resources: 9 3 4
p3 Executed
Need matrix :
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
Available resources: 9 3 6
Safe sequence is:
p1 p0 p2 p3
------------------------
EXIT
------------------------
*/