int i,j,Max[5][5],Allocation[5][5],Need[5][5],Available[5] ,Finish[5],p[5],r[5][5],Request[5][5],q;
void Init();
void Safe();
void Bank();
int main()
{
Init(); Safe();
if(q==1) {Bank();} else{printf("please input again!");Init();}
}
void Init()
{int m, n;
printf("please input the number of process:\n");
scanf("%d",&i);
printf("please input the number of resours:\n");
scanf("%d",&j);
printf("please input the number of Max:\n");
for(m=0;m<i;m++)
for(n=0;n<j;n++)
scanf("%d",&Max[m][n]);
printf("please input the number of Allocation:\n");
for(m=0;m<i;m++)
for(n=0;n<j;n++)
scanf("%d",&Allocation[m][n]);
printf("please input the number of Need:\n");
for(m=0;m<i;m++)
for(n=0;n<j;n++)
scanf("%d",&Need[m][n]);
printf("please input the number of Available:\n");
for(m=0;m<j;m++)
scanf("%d",&Available[m]);
}
void Bank()
{int m,n,k,p;
do{
printf("please input the name of request:\n ");
scanf("%d",&p);
if(p>i||p<0)
printf("input wrong again!") ;
}while(p>i||p<0);
printf("please input every process number of request:\n ");
for(k=0;k<j;k++)
scanf("%d",&Request[p][k]);
for(n=0;n<j;n++)
{if((Request[p][n]>Need[p][n])||(Request[p][n]>Available[n]))
{
break;
}
}
if(n==j)
{ for(k=0;k<j;k++)
{Available[k]=Available[k]-Request[p][k];
Allocation[p][k]=Allocation[p][k]+Request[p][k];
Need[p][k]=Need[p][k]-Request[p][k];
}
Safe(); /* for(k=0;k<j;k++)
printf("%d",Allocation[p][k]);
for(k=0;k<j;k++)
printf("%d",Need[p][k]);
for(k=0;k<j;k++)
printf("%d",Available[k]); */
if(q==1)printf("succee\n");
else
{ printf(" can not apply\n");
for(k=0;k<j;k++)
{Available[k]=Available[k]+Request[p][k];
Allocation[p][k]=Allocation[p][k]-Request[p][k];
Need[p][k]=Need[p][k]+Request[p][k];
/* for(k=0;k<j;k++)
printf("%d",Allocation[p][k]);
for(k=0;k<j;k++)
printf("%d",Need[p][k]);
for(k=0;k<j;k++)
printf("%d",Available[k]); */
}
}
}
else
{printf("da yu Need or A can not apply\n");}
getch();
}
void Safe()
{
int m,n,k,l=0;
int Work[5];
for(m=0;m<j;m++)
{Work[m]=Available[m];}
for(m=0;m<i;m++)
{Finish[m]=0;}
for(m=0;m<i;m++)
{
if(Finish[m]==1){ continue;}
else
{
for(n=0;n<j;n++)
{
if(Need[m][n]>Work[n])
{
break;
}
}
if(n==j)
{
Finish[m]=1;
for(k=0;k<j;k++)
{
Work[k]+=Allocation[m][k];
}
p[l++]=m;
m=-1;
}
}
if(l==i)
{
for(m=0;m<i;m++)
{Finish[m]=0;}
q=1;
printf("the safe list is:");
for(k=0;k<l;k++)
{
printf("%d",p[k]);
if(k!=l-1)
{
printf("-->");
}
}
}
}
if (l!=i)
{ q=0;
printf("it is not safe.\n"); }
}
yhj.rar_银行家算法
版权申诉
132 浏览量
2022-09-19
15:05:48
上传
评论
收藏 967B RAR 举报
邓凌佳
- 粉丝: 66
- 资源: 1万+
最新资源
- labelImg安装指导书.docx
- 2023AI自有光-她经济消费新图鉴(2023)-百度营销.pdf
- 2022中国新能源汽车内容生态趋势洞察(1).pdf
- Docker技术:Docker安装与配置教程+运维技术+超融合+虚拟技术+云计算
- ZeRO Memory Optimizations Toward Training LLM.pdf
- 高效SQL语句编写(how-to-write-efficient-sql)
- ZeroTermux-release_sign.apk
- 非弹性斜碰撞物理课件模拟-HTML网页制作
- 软件测试学习日志-测试基础-day02
- 制作一个简单的进销存(库存管理)页面.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈