Assignment 3:
Banker's Algorithm Program.
bankers.h
#include <stdio.h>
#include <stdlib.h>
int max[5][3]={7,5,3,3,2,2,9,0,2,2,2,2,4,3,3},avail[3]={3,3,2},allocation[5][3]={0,1,0,2,0,0,3,0,2,2,1,1,0,0,2},need[5][3],finish[5],work[3],safe[5],request[3];
int n,m,proc;
/*void accept_requestR(int m)
{
int i,proc;
printf("\nenter the process number\n");
scanf("%d",&proc);
if(proc>=n)
printf("\nInvalid process");
else
{
for(i=0;i<m;i++)
{
printf("\nenter the request matrix for R[%d]",i);
scanf("%d",&request[i]);
}
}
}
*/
void accept_maxM(int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("\nenter the max matrix for M[%d][%d]",i,j);
scanf("%d",&max[i][j]);
}
}
}
void display_maxM(int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n P%d \t",i);
for(j=0;j<m;j++)
{
printf("%d \t",max[i][j]);
}
}
}
void accept_allocationA(int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("\nenter the allocation matrix for A[%d][%d]",i,j);
scanf("%d",&allocation[i][j]);
}
}
}
void display_allocationA(int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n P%d \t",i);
for(j=0;j<m;j++)
{
printf("%d \t",allocation[i][j]);
}
}
}
void accept_availAv(int m)
{
int i;
for(i=0;i<m;i++)
{
printf("\nenter the available matrix Av[%d][%d]",i);
scanf("%d",&avail[i]);
}
}
void display_availAv(int m)
{
int i;
for(i=0;i<m;i++)
{
printf("%d \t",avail[i]);
}
}
void display()
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n P%d \t",i);
for(j=0;j<m;j++)
{
printf("%d \t",need[i][j]);
}
}
}
void find_need(int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
}
int compare_need(int p,int m)
{
int i,flag=0;
for(i=0;i<m;i++)
{
if(need[p][i]>work[i])
{
flag=1;
break;
}
}
if(flag==0)
return p;
else
return -1;
}
int safety(int n,int m)
{
int i,k,flag,over=0,pno,l=0,cnt=0;
for(i=0;i<n;i++)
{
safe[i]=0;
finish[i]=0;
}
for(i=0;i<m;i++)
{
work[i]=avail[i];
}
while(!over)
{
for(i=0;i<n;i=(i+1)%n)
{
if(cnt==n)
{
return -1;
}
if(finish[i]==0)
{
flag=0;
pno=compare_need(i,m);
if(pno==-1)
{
if(i==m-1)
cnt++;
continue;
}
else
{
if(i<n && pno>=0)
{
for(k=0;k<m;k++)
{
work[k]+=allocation[pno][k];
printf("\nW %d\n",work[k]);
}
finish[pno]=1;
safe[l++]=pno;
// printf("\n p %d",pno);
if(l==n)
{
printf("\nSafe sequence is:\n");
for(i=0;i<n;i++)
printf("\n P%d\t",safe[i]);
over=1;return 1;
}
}
}
}
}
}
}
void resource_request(int proc)
{
//accept_requestR(m);
int i;
for(i=0;i<m;i++)
{
if(request[i]>need[proc][i])
{
printf("error..process exceed its max demand");
return;
}
}
for(i=0;i<m;i++)
{
if(request[i]>avail[i])
{
printf("Process must wait,resources not available");
return;
}
}
for(i=0;i<m;i++)
{
avail[i]=avail[i]-request[i];
allocation[proc][i]=allocation[proc][i]+request[i];
need[proc][i]=need[proc][i]-request[i];
}
printf("\nAllocation Matrix is :\n");
display_allocationA(n,m);
printf("\nAvailable Matrix is :\n");
display_availAv(m);
printf("\nNeed Matrix is :\n");
display();
if(safety(n,m)==-1)
printf("\nSafe sequence does not exists ... ");
}
0 Comments
If anyone has Doubts or suggestions please let me know