操作系统课程设计-银行家算法源代码(二,c语言版)
#include
struct claim
{
int user;
int num[3];
}claims;
int input()
{
printf("please input your request:user(0~4):\n");
scanf("%d",&claims.user);
printf("input the number of resource a:\n");
scanf("%d",&claims.num[0]);
printf("input the number of resource b:\n");
scanf("%d",&claims.num[1]);
printf("input the number of resource c:\n");
scanf("%d",&claims.num[2]);
return 1;
}
int safety_chk(int alloc[][3],int need[][3],int avail[3])
{
int work[3],finish[5];
for(int p=0;p {
work[p]=avail[p];
finish[p]=0;
}
for(int i=0;i {
if(finish==0&&
need[0] need[1] need[2] {
for(int j=0;j work[j]=alloc[j]+work[j];
finish=1;
i=-1;//重头再来
}
}
for(i=0;i {
if(finish==0)
return 0;
}
return 1;
}
int process(int alloc[][3],int need[][3],int avail[3])
{
int ret;
input();
for(int i=0;i {
if(claims.num>need[claims.user]||claims.num>avail)
return 0;
}
for(i=0;i {
avail=avail-claims.num;
alloc[claims.user]=alloc[claims.user]+claims.num;
need[claims.user]=need[claims.user]-claims.num;
}
if((ret=safety_chk(alloc,need,avail)==0))
{
printf("safety_chk's result %d \n",0);
for(i=0;i {
avail=avail+claims.num;
alloc[claims.user]=alloc[claims.user]-claims.num;
need[claims.user]=need[claims.user]+claims.num;
}
return 0;
}
else
{
printf("safety_chk's result %d \n",1);
}
return 1;
}
//追查ZhuiCha.com网络技术 版权归原作者所有
//ZhuiCha.com追查网提示:
//本代码为网络转载
//不保证完全编译通过
//实际上 放置代码的目的主要参考里面的数据结构以及大体流程,代码还是自己写出来得好。
//http://www.zhuicha.com
//IP地址查询 手机号码归属 邮编区号 身份证验证查询 火车时刻表
//成语词典 运程测算 周公解梦 在线学习手册 专业查询网
void main()
{
int alloc[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int need[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int avail[3]={3,3,2};
if(process(alloc,need,avail)==0)
printf("sorry,we cannot help you!\n");
else printf("operation complete!\n");
return;
}