Space for google add

Banker's Algorithm Program.

     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 ... ");

}


        bankers.c
        
                #include <stdio.h>
#include "bankers.h" 
int main(void)
{
int ch,i;
printf("\nenter the number of process\n");
scanf("%d",&n);
printf("\nEnter the number of resources\n");
scanf("%d",&m);
do
{
printf("\n1.accept\n2.display\n3.Need\n4.Safety\n5.Resource\n6.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
//accept_maxM(n,m);
//accept_allocationA(n,m);
//accept_availAv(m);
break;
case 2:
printf("\n Max matrix is: \n");
display_maxM(n,m);
printf("\n Allocation matrix is: \n");
display_allocationA(n,m);
printf("\n Available matrix is: \n");

display_availAv(m);
break;
case 3:
printf("\n Need matrix is: \n");
find_need(n,m);
display();
break;
case 4:
if(safety(n,m)==-1)
printf("\nSafe sequence does not exists ... ");
break;
case 5:
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]);
}
}
resource_request(proc);
break;
case 6:
exit(0);
break;
}
}
while(ch<7);
}



Post a Comment

0 Comments