📄 banker.c
字号:
#include<stdio.h>
#include<stdlib.h>
#define n 3
#define m 5
int compare1(int work[],int array[])
{ int l,a=0;
for(l=0;l<n;l++)
{
if(work[l]==array[l])
a++;
else
break;
}
if(a==3)
return 1;
else return 0;
}
int compare(int work[],int array[][n],int g)
{
int l,a=0;
for(l=0;l<n;l++)
{
if(work[l]>=array[g][l])
a++;
else
break;
}
if(a==3)
return 1;
else
return 0;
}
int security(int available[],int need[][n],int allocation[][n],int all[])
{
int work[n],i,j,w=0,r=0,l=0,finish[m]={0,0,0,0,0};
for(i=0;i<n;i++)
work[i]=available[i];
while(!w)
{
for(i=0;i<m;)
{
if(!finish[i]&&compare(work,need,i))
{
for(j=0;j<n;j++)
work[j]+=allocation[i][j];
finish[i]=1;
r++;
i++;
}
else
{
i++;
while(finish[i])
i++;
}
}
if(r==l)
break;
else
l=r;
}
if(compare1(work,all))
return 1;
else
return 0;
}
void main()
{
int max[m][n]={7,5,3,3,2,2,9,0,2,2,2,2,4,3,3},allocation[m][n],request[n],need[m][n],available[n],all[n]={10,5,7},i,d,j;
char quit;
int q=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ allocation[i][j]=0;
need[i][j]=max[i][j]-allocation[i][j];
}
for(i=0;i<n;i++)
available[i]=all[i];
printf("---------------------struction-------------------------------\n");
printf(" the alloction of Every process is 0 . \n now,you may input the source of some process needed source\n if you want to stop input c\n");
do
{
scanf("%d",&d);
for(j=0;j<n;j++)
{
scanf("%d",&request[j]);
if(request[j]>need[d][j]||request[j]>available[j])
{
printf("not enough source\n");
exit(0);
}
}
for(j=0;j<n;j++)
{
need[d][j]-=request[j];
allocation[d][j]+=request[j];
available[j]-=request[j];
}
if(security(available,need,allocation,all))
{
printf(" request seccussfully the source of neccesary,\n if you want to go on request sorce ,please input process number ,then the quantity of source \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d ",need[i][j]);
i=0;
while(i<n)
{ if(need[d][i]==0)
{ q++; i++;}
else break;
}
if(q==n)
{ for(i=0;i<n;i++)
available[i]+=allocation[d][i];
q=0;
}
}
else
{
printf("this need is not securious , should abandom\n if you want to go on request sorce ,please input process number ,then the quantity of source \n");
for(j=0;j<n;j++)
{
need[d][j]+=request[j];
allocation[d][j]-=request[j];
available[j]+=request[j];
}
}
scanf("%c",&quit);
}
while(quit!='c');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -