⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 44.c

📁 银行家算法的实例
💻 C
字号:
#include <stdio.h>
struct YH
{
	int Allocation[5][3];
	int Need[5][3];
	int Available[3];
	int G[3][3];
};

int u,B=0,A=0;
int Work[3];
int Request[3];
struct YH st;

void W(int i) //系统安全检测函数
{int j;
for(j=0;j<=2;j++)
{
	if(Request[j]<=st.Need[i][j])
		A++;
}
if(A==3)
for(j=0;j<=2;j++)
{
	if(Request[j]<=st.Available[j])
	B++;
}
else {printf("不安全");}
if(B==3)
for(j=0;j<=2;j++)
{
	st.Available[j]=st.Available[j]-Request[j];
	st.Allocation[i][j]=st.Allocation[i][j]+Request[j];
	st.Need[i][j]=st.Need[i][j]-Request[j];
	

}
else {printf("不安全");}
}


void F() //分配后安全检测函数
{
	int j,t,C=0,D=0,P[5];

	for(j=0;j<=2;j++)
     Work[j]=st.Available[j];

	for(t=0;t<=4;t++)
	P[t]=0;


Loop:
	for(t=0;t<=4;t++)
	{
		for(j=0;j<=2;j++)
		{
			if(Work[j]>=st.Need[t][j]&&P[t]==0)
			C++;
		}

	if(C==3)
	{
		P[t]=1;
		D++;
		for(j=0;j<=2;j++)
			Work[j]=Work[j]+st.Allocation[t][j];	
		
		printf("%d ",t);
		if(P[t]==1)
		{
			C=0;
			goto Loop;
		}
			
		
	}
				C=0;
			
			
}
	 if(D==5)
	 
	 {	 printf("安全");u=1;}
	     
	 
	 else
	 {printf("不安全");u=0;}
	 
	 for(j=0;j<=2;j++)
	 	Work[j]=st.Available[j];
				
	 for(t=0;t<=4;t++)
               	P[t]=0;
             D=0;
			 	t=0;
}

void Q(int a[],int b[]) 
{
	int k;
	printf("输入Request的值\n");

	for(k=0;k<=2;k++)
    scanf("%d",&a[k]);

    printf("i=?\n");

    scanf("%d",&b[0]);
}

main()
{
int i, j,c[1];

printf("输入Allocation的值\n");
for(i=0;i<=4;i++)
for(j=0;j<=2;j++)
scanf("%d",&st.Allocation[i][j]);

printf("输入Need的值\n");

for(i=0;i<=4;i++)
for(j=0;j<=2;j++)
scanf("%d",&st.Need[i][j]);



printf("输入Available的值\n");
for(j=0;j<=2;j++)
scanf("%d",&st.Available[j]);

F();
Loop1:
{
Q(Request,c);

if (Request[0]==0&&Request[1]==0&&Request[2]==0)
printf("测试结束");
else 
{
switch(c[0])
{case 0:
      W(0);
	  break;
 case 1:
      W(1);
	  break;
 case 2:
      W(2);
	  break;
 case 3:
      W(3);
	  break;
 case 4:
      W(4);
	  break;
}
if(B==3)
{F();}
A=0;
B=0;
if(u==1)
{

	for(j=0;j<=2;j++)
	{
st.G[0][j]=st.Allocation[c[0]][j] ;
st.G[1][j]=st.Need[c[0]][j] ;	
st.G[2][j]=st.Available[j];


//printf("%d ",st.Available[j]);



	} 
//	printf("%d",B);
	//printf("%d",t);
goto Loop1;}
if(u==0)
{
	for(j=0;j<=2;j++)
{
	st.Allocation[c[0]][j]=st.G[0][j];
    st.Need[c[0]][j]=st.G[1][j];
    st.Available[j]=st.G[2][j];
}
	goto Loop1;}

}}

scanf("%d",&i);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -