Bankers algorithm is Deadlock avoidance algorithm which capable of dealing with more than two or n process resource allocation.Everytime there is demand by the process for allocation algorithm is invoked by OS.
DATA STRUCTURES USED-:
Available-Vector to store resources available with the system
Max[i][j]-Max demand made by the ith process for jth resource
Allocated[i][j]-Number of instances of jth resources has been allocated to ith process
Need[][]-Max-Allocated
ALGO-:
#include<conio.h>
#include<stdlib.h>
int available[1][5];
int max[5][5];
int allocated[5][5];
int need[5][5];
int p[5]={0};
int n=5;//number of process
int m=3;
void input()
{
int i,j;
puts("Enter the Allocation Matrix");
for(i=0;i<n;i++)
{
printf("P%d ",i);
for(j=0;j<m;j++)
{
scanf("%d",&allocated[i][j]);
}//inner for
printf("\n");
}//outer for
puts("Enter the MAX Matrix");
for(i=0;i<n;i++)
{
printf("P%d ",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}//inner for
printf("\n");
}//outer for
puts("Enter the Available Matrix");
for(i=0;i<m;i++)
{
scanf("%d",&available[0][i]);
}
}//function end
void calc_need()
{
int i,j;
puts("NEED matrix");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
printf("%d ",need[i][j]);
}//inner for
printf("\n");
}//outer for
}//end of calc_need
int less_than_or_equal(int i)//to check if for process i need <=Available
{
int j;
for(j=0;j<m;j++)
{
if(need[i][j]<=available[0][j])
continue;
else
return 0;
}//end of for
return 1;
}
void increase_available(i)
{
int j;
for(j=0;j<m;j++)
{
available[0][j]=available[0][j]+allocated[i][j];
}
}
void check()
{
int j;
for(j=0;j<n;j++)
{
printf("%d",p[j]);
}
}
main()
{
int i;
int f=0;
input();//to take inputs for Allocated Max and Available
calc_need();
while(1)
{
if(f==n)
break;
f=0;
for(i=0;i<n;i++)
{
if((p[i]==0)&&(less_than_or_equal(i)))
{
increase_available(i);
p[i]=1;
printf("P%d ",i);
continue;
}
else
{
f=f+1;
continue;
}
}//end of for
}//end of while
getch();
check();
getch();
}//end of main
OUTPUT
Safe Sequence
<P1,P3,P4,P0,P2>
DATA STRUCTURES USED-:
Available-Vector to store resources available with the system
Max[i][j]-Max demand made by the ith process for jth resource
Allocated[i][j]-Number of instances of jth resources has been allocated to ith process
Need[][]-Max-Allocated
ALGO-:
- The process works on lines of bank management.Whenever there is the demand of resources allocation which cannot be completed by OS, process is asked to wait until some finished process release its resources.
- Released resources are added to Available list
- This Available list is compared with Need list of next process
- If Need is greater than Available, process is put on hold and same condition is checked for next process
- If Need is less than or Equal to Available then the Allocated resources are added to Available(since if condition is true process can successfully finish itself and can release its resources)
- All above process are repeated until all process have been completed or there is no process left for which Need <=Available. In the latter case system is not in SAFE state and in former safe sequence can be generated
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int available[1][5];
int max[5][5];
int allocated[5][5];
int need[5][5];
int p[5]={0};
int n=5;//number of process
int m=3;
void input()
{
int i,j;
puts("Enter the Allocation Matrix");
for(i=0;i<n;i++)
{
printf("P%d ",i);
for(j=0;j<m;j++)
{
scanf("%d",&allocated[i][j]);
}//inner for
printf("\n");
}//outer for
puts("Enter the MAX Matrix");
for(i=0;i<n;i++)
{
printf("P%d ",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}//inner for
printf("\n");
}//outer for
puts("Enter the Available Matrix");
for(i=0;i<m;i++)
{
scanf("%d",&available[0][i]);
}
}//function end
void calc_need()
{
int i,j;
puts("NEED matrix");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
printf("%d ",need[i][j]);
}//inner for
printf("\n");
}//outer for
}//end of calc_need
int less_than_or_equal(int i)//to check if for process i need <=Available
{
int j;
for(j=0;j<m;j++)
{
if(need[i][j]<=available[0][j])
continue;
else
return 0;
}//end of for
return 1;
}
void increase_available(i)
{
int j;
for(j=0;j<m;j++)
{
available[0][j]=available[0][j]+allocated[i][j];
}
}
void check()
{
int j;
for(j=0;j<n;j++)
{
printf("%d",p[j]);
}
}
main()
{
int i;
int f=0;
input();//to take inputs for Allocated Max and Available
calc_need();
while(1)
{
if(f==n)
break;
f=0;
for(i=0;i<n;i++)
{
if((p[i]==0)&&(less_than_or_equal(i)))
{
increase_available(i);
p[i]=1;
printf("P%d ",i);
continue;
}
else
{
f=f+1;
continue;
}
}//end of for
}//end of while
getch();
check();
getch();
}//end of main
OUTPUT
Safe Sequence
<P1,P3,P4,P0,P2>
No comments:
Post a Comment