环境:
configure_bank.txt内容:
3 3 2
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
#include <stdlib.h>
#include <stdio.h>
//数据结构
int allocation[5][3];
int max[5][3];
int available[3];
int request[3];
int work[3];
int need[5][3];
int safeSequence[5];
int finish[5];
int i;//全局变量::
int m,n;
int count = 0;
int flag = 0;
int k = 0;
int s = 0;
//读取文件
int read()
{
// int m,n;
FILE* fp = fopen("configure_bank.txt","r");
if(fp == NULL)
{
printf("无法读取配置文件");
return -1;
}
for(m=0; m<3; m++)
{
fscanf(fp,"%d",&available[m]);
}
fscanf(fp,"\n");
for(m = 0; m<5; m++)
{
for(n=0; n<3; n++)
{
fscanf(fp,"%d",&max[m][n]);
}
fscanf(fp,"\n");
}
fscanf(fp,"\n");
for(m = 0; m<5; m++)
{
for(n=0; n<3; n++)
{
fscanf(fp,"%d",&allocation[m][n]);
}
fscanf(fp,"\n");
}
return 1;
}
int safe()
{
int i = ::i;
while(1)
{
if(finish[i]==1)
{
i=(i+1)%5;
}
else
{
for(n=0; n<3; n++)
{
flag = work[n]-need[i][n];
// printf("flag:%d\n",flag);
if(flag<0)
{
break;
}
}
//回收资源
if(flag>=0)
{
for(k =0; k<3; k++)
{
work[k]=work[k]+allocation[i][k];
}
finish[i]=1;
//printf("有进程可以分配%d\n",i );
safeSequence[s] = i;
// printf("safeSequence:::%d\n",safeSequence[s] );
s++;
}
flag = 1;
for(n = 0; n<5; n++)
{
if(finish[n]==0)
{
// printf("%d\n",finish[i]);
flag=0;
break;
}
}
// printf("%d\n",flag);
if(count<120&&flag==1)
{
printf("find the 安全序列:\n");
/*
for (m = 0; m <5 ; m++)
{
printf("%d\t\n",finish[m]);
}
*/
for (m = 0; m <5 ; m++)
{
printf("%d\t",safeSequence[m]);
}
printf("\n");
/*
for(m =0;m<5;m++)
{
finish[m]=0;
safeSequence[m]=0;
}
*/
break;
}
else if(count>=120)
{
break;
return 0;
}
count++;
i=(i+1)%5;
}
}
}
int main()
{
if(read()==-1)
{
printf("read error!");
return 0;
}
//已经分配的
printf("分配的资源的allocation矩阵(5*3)\n");
for(m=0; m<5; m++)
{
for(n=0; n<3; n++)
{
printf("%d\t",allocation[m][n]);
if(n ==2)
{
printf("\n");
}
}
}
printf("资源总数(1*3):\n");
int s;
for(s=0; s<3; s++)
{
printf("%d\t",available[s]);
}
//实际需要的
printf("\n4个进程每个进程对应的nmax矩阵(5*3)\n");
for(m=0; m<5; m++)
{
for(n=0; n<3; n++)
{
printf("%d\t",max[m][n]);
if(n ==2)
{
printf("\n");
}
}
}
// 首先先找出它的Need,Need即Max(最多需要多少资源)减去Allocation(原本已经分配出去的资源)
for(m=0; m<5; m++)
{
for(n=0; n<3; n++)
{
need[m][n]=max[m][n]-allocation[m][n];
}
}
printf("4个进程每个进程对应的need矩阵(5*3)\n");
for(m=0; m<5; m++)
{
for(n=0; n<3; n++)
{
printf("%d\t",need[m][n]);
if(n == 2)
{
printf("\n");
}
}
}
printf("\n");
printf("\n");
for(m=0; m<5; m++)
{
finish[m]=0;
}
printf("请输入进程申请的资源的进程号\n");
scanf("%d",&::i);
printf("请输入%d 号进程需要的资源情况\n",::i);
for(m = 0; m<3; m++)
{
scanf("%d",&request[m]);
}
int sign =1;
//i =1;
for(m =0; m<3; m++)
{
if(request[m]>need[::i][m]||request[m]>available[m])
{
sign = 0;
printf("%d号资源不够分配\n",m);
}
}
if(sign)
{
printf("可以分配\n");
for(m=0; m<3; m++)
{
available[m]= available[m]-request[m];
allocation[::i][m] = allocation[::i][m]+request[m];
need[::i][m]= need[i][m]-request[m];
}
for(m=0; m<3; m++)
{
work[m] = available[m];
}
safe();
}
else
{
printf("不可以分配\n");
return 0;
}
return 0;
}
/*
printf("第一个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[0][s]));
}
printf("第2个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[1][s]));
}
printf("第3个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[2][s]);
}
printf("第4个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[3][s]);
}
*/
/*
for(m=0;m<5;m++)
{
for(n=0;n<3;n++)
{
scanf("%d",&allocation[m][n]);
if(n ==2)
{
printf("\n");
}
}
}
5*3
/*
allocation[0][0] = 0;
allocation[0][1] = 1;
allocation[0][2] = 0;
allocation[1][0] = 2;
allocation[1][1] = 0;
allocation[1][2] = 0;
allocation[2][0] = 3;
allocation[2][1] = 0;
allocation[2][2] = 2;
allocation[3][0] = 2;
allocation[3][1] = 1;
allocation[3][2] = 1;
allocation[4][0] = 0;
allocation[4][1] = 0;
allocation[4][2] = 2;
*/
/* for(m=0;m<5;m++)
{
for(n=0;n<3;n++)
{
scanf("%d",&(max[m][n]));
if(n ==2)
{
printf("\n");
}
}
}
*/
/*
max[0][0] = 7;
max[0][1] = 5;
max[0][2] = 3;
max[1][0] = 3;
max[1][1] = 2;
max[1][2] = 2;
max[2][0] = 9;
max[2][1] = 0;
max[2][2] = 2;
max[3][0] = 2;
max[3][1] = 2;
max[3][2] = 2;
max[4][0] = 4;
max[4][1] = 3;
max[4][2] = 3;
*/
/* for(s=0;s<3;s++)
{
scanf("%d",&(available[s]));
}
*/
/*
available[0] = 3;
available[1] = 3;
available[2] = 2;
/*
request[0]=1;
request[1]=0;
request[2]=2;
*/
/* for(m=0;m<5;m++)
{
printf("%d\t",finish[m]);
}*/
/*
::i表示使用全局的i
* *
for(m=0; m<5; m++)
{
for(n=0; n<3; n++)
{
printf("%d\t",need[m][n]);
if(n == 2)
{
printf("\n");
}
}
}
printf("\n");
printf("\n");
for(m=0; m<5; m++)
{
finish[m]=0;
}
printf("请输入进程申请的资源的进程号\n");
scanf("%d",&::i);
printf("请输入%d 号进程需要的资源情况\n",::i);
for(m = 0; m<3; m++)
{
scanf("%d",&request[m]);
}
int sign =1;
//i =1;
for(m =0; m<3; m++)
{
if(request[m]>need[::i][m]||request[m]>available[m])
{
sign = 0;
printf("%d号资源不够分配\n",m);
}
}
if(sign)
{
printf("可以分配\n");
for(m=0; m<3; m++)
{
available[m]= available[m]-request[m];
allocation[::i][m] = allocation[::i][m]+request[m];
need[::i][m]= need[i][m]-request[m];
}
for(m=0; m<3; m++)
{
work[m] = available[m];
}
safe();
}
else
{
printf("不可以分配\n");
return 0;
}
return 0;
}
/*
printf("第一个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[0][s]));
}
printf("第2个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[1][s]));
}
printf("第3个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[2][s]);
}
printf("第4个资源的请求\n");
for(s=0;s<4;s++)
{
scanf("%d",&(available[3][s]);
}
*/
/*
for(m=0;m<5;m++)
{
for(n=0;n<3;n++)
{
scanf("%d",&allocation[m][n]);
if(n ==2)
{
printf("\n");
}
}
}
5*3
/*
allocation[0][0] = 0;
allocation[0][1] = 1;
allocation[0][2] = 0;
allocation[1][0] = 2;
allocation[1][1] = 0;
allocation[1][2] = 0;
allocation[2][0] = 3;
allocation[2][1] = 0;
allocation[2][2] = 2;
allocation[3][0] = 2;
allocation[3][1] = 1;
allocation[3][2] = 1;
allocation[4][0] = 0;
allocation[4][1] = 0;
allocation[4][2] = 2;
*/
/* for(m=0;m<5;m++)
{
for(n=0;n<3;n++)
{
scanf("%d",&(max[m][n]));
if(n ==2)
{
printf("\n");
}
}
}
*/
/*
max[0][0] = 7;
max[0][1] = 5;
max[0][2] = 3;
max[1][0] = 3;
max[1][1] = 2;
max[1][2] = 2;
max[2][0] = 9;
max[2][1] = 0;
max[2][2] = 2;
max[3][0] = 2;
max[3][1] = 2;
max[3][2] = 2;
max[4][0] = 4;
max[4][1] = 3;
max[4][2] = 3;
*/
/* for(s=0;s<3;s++)
{
scanf("%d",&(available[s]));
}
*/
/*
available[0] = 3;
available[1] = 3;
available[2] = 2;
/*
request[0]=1;
request[1]=0;
request[2]=2;
*/
/* for(m=0;m<5;m++)
{
printf("%d\t",finish[m]);
}*/
/*
::i表示使用全局的i
* */