一、实验目的
掌握银行家算法思想,并能编程实现。
二、实验内容和要求
1、在Linux环境下编译运行程序;
2、按照教材的算法编写;
3、(*)输入数据从文本文件中读出,不从键盘录入,数据文件格式见以下说明;
4、主要数据结构的变量名和教材中的一致,包括Available、Max、Allocation、Need、Request、Work、Finish。
5、程序可支持不同个数的进程和不同个数的资源;
6、验证教材中的“银行家算法示例”中的例子(包括可成功分配、不可分配)。
三、代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// When the function => init ,the data shoule come from flie and there is just for test!!!
int Allocation[5][3];
int Max[100][100];
int Need[100][100];
int Request[100][100];
int Available[100];
int Work[100];
int Finish[100];
int Resource[100];
int PNumber;
int ResourceNumber;
int Line[5];
void initLine()
{
for (int i = 0; i < 5; i++)
{
Line[i] = 0;
}
}
void AddLine(int number)
{
for (int i = 0; i < number; i++)
printf("\n");
}
int wheatherAllTureFinsih()
{
for (int i = 0; i < PNumber; i++)
{
if (Finish[i] == 0)
{
return 0;
}
}
return 1;
}
void recyleAllocation(int i)
{
// i => pid
for (int m = 0; m < ResourceNumber; m++)
{
Work[m] += Allocation[i][m];
}
}
void initFinish()
{
for (int i = 0; i < 5; i++)
{
Finish[i] = 0;
}
}
void init(char file[])
{
printf("\t ===> 正在读入文件,初始化资源参数\n");
// process => 5 , resoure => 3
FILE *fp;
char strLine[100];
if ((fp = fopen(file, "r")) == NULL)
{
printf("\t ==> 读取文件错误,请检查文件路径!\n");
exit(1);
}
int next = 0;
while (!feof(fp))
{
int where = 0;
int req = 0