C-Source Code for Bankers Algorithm
#include<stdio.h>
#include<string.h>
int n,m,i,j;
int all[10][10],max[10][10],need[10][10],work[10],work1[10];
struct proc
{
char name[10];
int flag;
}pro[20],temp;
void get()
{
printf("\nEnter the total no of process:");
scanf("%d",&n);
printf("\nEnter the total no of resourse:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("\nName=");
scanf("%s",&pro[i].name);
}
printf("\nEnter allocation matrix=");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&all[i][j]);
printf("\nEnter Max matrix=");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-all[i][j];
printf("\nNeed=");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(" %d",need[i][j]);
printf("\n");
}
printf("\nEnter work=");
for(i=0;i<m;i++)
{
scanf("%d",&work[i]);
work1[i]=work[i];
}
}
int safe()
{
int count=0;
for(i=0;i<n;i++)
pro[i].flag=0;
i=0;
while(1)
{
for(j=0;j<m;j++)
{
if(need[i][j]>work[j])
break;
}
if(pro[i].flag==0&&j==m)
{
printf(" %s ",pro[i].name);
for(j=0;j<m;j++)
work[j]=work[j]+all[i][j];
pro[i].flag=1;
}
else
i=++i%n;
for(j=0;j<n;j++)
{
if(!pro[j].flag)
break;
}
if(j==n)
{
printf("\nSafe");
return 1;
}
if(count>100)
{
printf("\nNot safe");
return 0;
}
}
}
void ra()
{
char name[10];
int rr[10];
printf("\nEnter name of the process=");
scanf("%s",&name);
for(i=0;i<n;i++)
{
if(!strcmp(name,pro[i].name))
break;
}
printf("\nEnter resourse request for process %s=",pro[i].name);
for(j=0;j<m;j++)
{
scanf("%d",&rr[j]);
work[j]=work1[j];
if(rr[j]>need[i][j])
{
printf("\nRequest is greater than need");
break;
}
if(rr[j]>work[j])
{
printf("\nRequest is greater than Available");
break;
}
}
int temp1[10],temp2[10],temp3[10];
if(j==m)
{
for(j=0;j<m;j++)
{
temp1[j]=all[i][j];
temp2[j]=work[j];
temp3[j]=need[i][j];
}
for(j=0;j<m;j++)
{
all[i][j]=all[i][j]+rr[j];
need[i][j]=need[i][j]-rr[j];
work[j]=work[j]-rr[j];
}
int flag=safe();
if(flag)
printf("\nResource Request is granted.");
else
{
for(j=0;j<m;j++)
{
all[i][j]=temp1[j];
work[j]=temp2[j];
need[i][j]=temp3[j];
}
printf("\nResource Request is not granted.");
}
}
}
int main()
{
get();
safe();
ra();
return(0);
}
#include<string.h>
int n,m,i,j;
int all[10][10],max[10][10],need[10][10],work[10],work1[10];
struct proc
{
char name[10];
int flag;
}pro[20],temp;
void get()
{
printf("\nEnter the total no of process:");
scanf("%d",&n);
printf("\nEnter the total no of resourse:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("\nName=");
scanf("%s",&pro[i].name);
}
printf("\nEnter allocation matrix=");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&all[i][j]);
printf("\nEnter Max matrix=");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-all[i][j];
printf("\nNeed=");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(" %d",need[i][j]);
printf("\n");
}
printf("\nEnter work=");
for(i=0;i<m;i++)
{
scanf("%d",&work[i]);
work1[i]=work[i];
}
}
int safe()
{
int count=0;
for(i=0;i<n;i++)
pro[i].flag=0;
i=0;
while(1)
{
for(j=0;j<m;j++)
{
if(need[i][j]>work[j])
break;
}
if(pro[i].flag==0&&j==m)
{
printf(" %s ",pro[i].name);
for(j=0;j<m;j++)
work[j]=work[j]+all[i][j];
pro[i].flag=1;
}
else
i=++i%n;
for(j=0;j<n;j++)
{
if(!pro[j].flag)
break;
}
if(j==n)
{
printf("\nSafe");
return 1;
}
if(count>100)
{
printf("\nNot safe");
return 0;
}
}
}
void ra()
{
char name[10];
int rr[10];
printf("\nEnter name of the process=");
scanf("%s",&name);
for(i=0;i<n;i++)
{
if(!strcmp(name,pro[i].name))
break;
}
printf("\nEnter resourse request for process %s=",pro[i].name);
for(j=0;j<m;j++)
{
scanf("%d",&rr[j]);
work[j]=work1[j];
if(rr[j]>need[i][j])
{
printf("\nRequest is greater than need");
break;
}
if(rr[j]>work[j])
{
printf("\nRequest is greater than Available");
break;
}
}
int temp1[10],temp2[10],temp3[10];
if(j==m)
{
for(j=0;j<m;j++)
{
temp1[j]=all[i][j];
temp2[j]=work[j];
temp3[j]=need[i][j];
}
for(j=0;j<m;j++)
{
all[i][j]=all[i][j]+rr[j];
need[i][j]=need[i][j]-rr[j];
work[j]=work[j]-rr[j];
}
int flag=safe();
if(flag)
printf("\nResource Request is granted.");
else
{
for(j=0;j<m;j++)
{
all[i][j]=temp1[j];
work[j]=temp2[j];
need[i][j]=temp3[j];
}
printf("\nResource Request is not granted.");
}
}
}
int main()
{
get();
safe();
ra();
return(0);
}
1 comments:
i want output for this coding....
Post a Comment