|
|
|
@ -0,0 +1,297 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
|
|
#define M 5 // 进程数
|
|
|
|
|
#define N 3 // 资源数
|
|
|
|
|
#define TRUE 1
|
|
|
|
|
#define FALSE 0
|
|
|
|
|
|
|
|
|
|
int MAX[M][N] = {{5, 5, 9}, {5, 3, 6}, {4, 0, 11}, {4, 2, 5}, {4, 2, 4}}; // 最大需求矩阵
|
|
|
|
|
int AVAILABLE[N] = {17, 5, 13}; // 系统可用资源数
|
|
|
|
|
int ALLOCATION[M][N] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; // 已分配矩阵
|
|
|
|
|
int NEED[M][N] = {{5, 5, 9}, {5, 3, 6}, {4, 0, 11}, {4, 2, 5}, {4, 2, 4}}; // 需求矩阵
|
|
|
|
|
int Request[N] = {0, 0, 0}; // 请求向量
|
|
|
|
|
|
|
|
|
|
void showdata(); // 显示系统状态
|
|
|
|
|
void changdata(int); // 更改资源分配
|
|
|
|
|
void rschangedata(int); // 回滚更改
|
|
|
|
|
int safecheck(); // 检查系统安全性
|
|
|
|
|
void inize(); // 初始化数据
|
|
|
|
|
int isbigneed(int); // 检查需求
|
|
|
|
|
int isbigavailable(int); // 检查可用资源
|
|
|
|
|
|
|
|
|
|
// 主程序
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int i = 0, j = 0;
|
|
|
|
|
char flag;
|
|
|
|
|
inize();
|
|
|
|
|
showdata();
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
printf("请输入申请资源的进程号(从0到%d): ", M - 1);
|
|
|
|
|
scanf("%d", &i);
|
|
|
|
|
}
|
|
|
|
|
if (i < 0 || i >= M)
|
|
|
|
|
{
|
|
|
|
|
printf("输入的进程号不存在,请重新输入!\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// printf("请输入进程号");
|
|
|
|
|
// printf("%d",i);
|
|
|
|
|
printf("请输入进程 %d 申请的资源数量\n", i);
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
if (j == 0)
|
|
|
|
|
printf("请输入申请A类资源的数量:");
|
|
|
|
|
if (j == 1)
|
|
|
|
|
printf("请输入申请B类资源的数量:");
|
|
|
|
|
if (j == 2)
|
|
|
|
|
printf("请输入申请C类资源的数量:");
|
|
|
|
|
scanf("%d", &Request[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isbigneed(i) && isbigavailable(i))
|
|
|
|
|
{
|
|
|
|
|
changdata(i);
|
|
|
|
|
if (safecheck())
|
|
|
|
|
{
|
|
|
|
|
rschangedata(i);
|
|
|
|
|
showdata();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
showdata();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("按“Y”或‘y’继续操作,否则退出:");
|
|
|
|
|
getchar();
|
|
|
|
|
scanf("%c", &flag);
|
|
|
|
|
if (flag != 'y' && flag != 'Y')
|
|
|
|
|
{
|
|
|
|
|
printf("\n谢谢使用!\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否超过需求
|
|
|
|
|
int isbigneed(int i)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
if (Request[j] > NEED[i][j])
|
|
|
|
|
{
|
|
|
|
|
printf("%d", i);
|
|
|
|
|
printf("号进程");
|
|
|
|
|
printf("申请的资源数>进程");
|
|
|
|
|
printf("%d", i);
|
|
|
|
|
printf("还需要");
|
|
|
|
|
if (j == 0)
|
|
|
|
|
printf("A");
|
|
|
|
|
if (j == 1)
|
|
|
|
|
printf("B");
|
|
|
|
|
if (j == 2)
|
|
|
|
|
printf("C");
|
|
|
|
|
printf("类资源的资源量!申请不合理,出错!\n");
|
|
|
|
|
printf("\n\n请输入任意键继续\n");
|
|
|
|
|
getch();
|
|
|
|
|
return 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否超过可用资源
|
|
|
|
|
int isbigavailable(int i)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
if (Request[j] > AVAILABLE[j])
|
|
|
|
|
{
|
|
|
|
|
printf("进程");
|
|
|
|
|
printf("%d", i);
|
|
|
|
|
printf("申请的资源数大于系统可用");
|
|
|
|
|
if (j == 0)
|
|
|
|
|
printf("A");
|
|
|
|
|
if (j == 1)
|
|
|
|
|
printf("B");
|
|
|
|
|
if (j == 2)
|
|
|
|
|
printf("C");
|
|
|
|
|
printf("类资源的资源量!申请不合理,出错!\n");
|
|
|
|
|
printf("\n\n请输入任意键继续\n");
|
|
|
|
|
getch();
|
|
|
|
|
return 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化数据
|
|
|
|
|
void inize()
|
|
|
|
|
{
|
|
|
|
|
printf("\n\n\t\t**************************************************************************************************\t\t\n");
|
|
|
|
|
printf("\t\t\t\t实验二 银行家算法实验\n");
|
|
|
|
|
printf("\n\n\t\t**************************************************************************************************\t\t\n");
|
|
|
|
|
printf("\t\t\t\t计算机科学与技术2402班\n");
|
|
|
|
|
printf("\t\t\t\t姓名:黄明艳\n");
|
|
|
|
|
printf("\t\t\t\t学号:202404130209\n");
|
|
|
|
|
printf("\t\t\t\t完成时间:2024年11月12日\n");
|
|
|
|
|
printf("\t\t\t\t请输入任意键进入演示过程\n");
|
|
|
|
|
_getch();
|
|
|
|
|
system("cls");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示系统状态
|
|
|
|
|
void showdata()
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("当前可用资源:\n");
|
|
|
|
|
printf("* * *Available* * *:\n");
|
|
|
|
|
printf("资源类别:A\tB\tC\n");
|
|
|
|
|
printf("资源数目:\n");
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d\t", AVAILABLE[j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("\n\n各进程还需要的资源:\n");
|
|
|
|
|
printf("* * * * * * * * Nedd * * * * * * * * \n");
|
|
|
|
|
printf("资源类别:A\tB\tC\n");
|
|
|
|
|
for (i = 0; i < M; i++)
|
|
|
|
|
{
|
|
|
|
|
// printf("");
|
|
|
|
|
// printf("%d", i);
|
|
|
|
|
// printf("号进程:");
|
|
|
|
|
printf("%d号进程:", i);
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d\t", NEED[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("\n\n各进程已分配的资源:\n");
|
|
|
|
|
printf("* * * * * * Allocation * * * * * * \n");
|
|
|
|
|
printf("资源类别:A\tB\tC\n");
|
|
|
|
|
for (i = 0; i < M; i++)
|
|
|
|
|
{
|
|
|
|
|
// printf("");
|
|
|
|
|
// printf("号进程:");
|
|
|
|
|
printf("%d号进程:", i);
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%d\t", ALLOCATION[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 系统对进程请求响应,资源的暂时改变
|
|
|
|
|
void changdata(int k)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
AVAILABLE[j] = AVAILABLE[j] - Request[j];
|
|
|
|
|
ALLOCATION[k][j] = ALLOCATION[k][j] + Request[j];
|
|
|
|
|
NEED[k][j] = NEED[k][j] - Request[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 系统资源状态回滚
|
|
|
|
|
void rschangedata(int k)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
AVAILABLE[j] = AVAILABLE[j] + Request[j];
|
|
|
|
|
ALLOCATION[k][j] = ALLOCATION[k][j] - Request[j];
|
|
|
|
|
NEED[k][j] = NEED[k][j] + Request[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 安全性检查
|
|
|
|
|
int safecheck()
|
|
|
|
|
{
|
|
|
|
|
int WORK[N], FINISH[M], temp[M], WORK1[N]; // 工作向量
|
|
|
|
|
int i, j, n, m = 0, k = 0, p, l;
|
|
|
|
|
|
|
|
|
|
// 初始化工作向量和完成向量
|
|
|
|
|
for (i = 0; i < M; i++)
|
|
|
|
|
FINISH[i] = FALSE;
|
|
|
|
|
for (i = 0; i < N; i++)
|
|
|
|
|
{
|
|
|
|
|
WORK[i] = AVAILABLE[i];
|
|
|
|
|
WORK1[i] = AVAILABLE[i];
|
|
|
|
|
}
|
|
|
|
|
// 查找系统是否有安全序列
|
|
|
|
|
for (n = 0; n < M; n++)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < M; i++)
|
|
|
|
|
{
|
|
|
|
|
m = 0;
|
|
|
|
|
for (j = 0; j < N; j++)
|
|
|
|
|
{
|
|
|
|
|
if (FINISH[i] == FALSE && NEED[i][j] <= WORK[j])
|
|
|
|
|
{
|
|
|
|
|
WORK[j] = WORK[j] + ALLOCATION[i][j];
|
|
|
|
|
m++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (m == 3)
|
|
|
|
|
{
|
|
|
|
|
FINISH[i] = TRUE;
|
|
|
|
|
temp[k] = i;
|
|
|
|
|
k++;
|
|
|
|
|
for (l = 0; l < N; l++)
|
|
|
|
|
WORK1[l] = WORK[l];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (p = 0; p < N; p++)
|
|
|
|
|
WORK[p] = WORK1[p];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < M; i++)
|
|
|
|
|
{
|
|
|
|
|
if (FINISH[i] == FALSE)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("系统不安全!本次资源申请不成功!\n");
|
|
|
|
|
printf("\n\n请输入任意键继续\n");
|
|
|
|
|
getch();
|
|
|
|
|
printf("\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("经安全性检查,系统安全,本次分配成功!\n");
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("本次安全序列:\n");
|
|
|
|
|
printf("进程依次为");
|
|
|
|
|
for (i = 0; i < M; i++) // 输出安全序列
|
|
|
|
|
{
|
|
|
|
|
printf("%d", temp[i]);
|
|
|
|
|
printf("->");
|
|
|
|
|
}
|
|
|
|
|
printf("\n\n请输入任意键继续! \n");
|
|
|
|
|
getch();
|
|
|
|
|
printf("\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|