代码如下:(环境VS2019)
#include<stdio.h>
#define Pcount 5
#define Scount 3
//可用资源,最大资源,已分配资源,仍需资源设置为全局变量
int Available[Scount];
int Max[Pcount][Scount];
int Allocation[Pcount][Scount];
int Need[Pcount][Scount];
void InitData();
void CountMax();
void PrintMssge();
int CheckSafe();
void ApplyResulate(int P, int Request[Scount]);
int Apply(int P, int Request[Scount]);
void Add(int* a, int b[Scount]);
void Minus(int* a, int b[Scount]);
int Equals(int a[Scount], int b[Scount]);
int CheckFinish(int Finish[]);
int main()
{
printf("作者信息\n");
//初始化数据
InitData();
//打印出数据
PrintMssge();
//检查系统的安全性
CheckSafe();
//第一次申请资源
int apply1[Scount] = { 1,0,2 };
ApplyResulate(1,apply1);
//第二次申请资源
int apply2[Scount] = { 3,3,0 };
ApplyResulate(4, apply2);
//第三次申请资源
int apply3[Scount] = { 0,2,0 };
ApplyResulate(0, apply3);
return 0;
}
void InitData()
{
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;
Need[0][0] = 7;
Need[0][1] = 4;
Need[0][2] = 3;
Need[1][0] = 1;
Need[1][1] = 2;
Need[1][2] = 2;
Need[2][0] = 6;
Need[2][1] = 0;
Need[2][2] = 0;
Need[3][0] = 0;
Need[3][1] = 1;
Need[3][2] = 1;
Need[4][0] = 4;
Need[4][1] = 3;
Need[4][2] = 1;
Available[0] = 3;
Available[1] = 3;
Available[2] = 2;
//计算MAX
CountMax();
}
void CountMax()
{//计算MAX资源,Max=Allocation+Need
for(int i=0;i<Pcount;i++)
for (int j = 0;j < Scount;j++)
{
Max[i][j] = Allocation[i][j] + Need[i][j];
}
}
void PrintMssge()
{//打印出当前资源分配情况
printf(" Max Allocation Need\n");
for (int i = 0;i < Pcount;i++)
{
printf("P%d ", i);
for (int j = 0;j < Scount;j++)
{
printf(" %d ", Max[i][j]);
}
for (int j = 0;j < Scount;j++)
{
printf(" %d ", Allocation[i][j]);
}
for (int j = 0;j < Scount;j++)
{
printf(" %d ", Need[i][j]);
}
if (i == 0)
{
printf("\t");
for (int j = 0;j < Scount;j++)
{
printf("%d", Available[j]);
}
}
printf("\n");
}
}
int CheckSafe()
{
printf("安全性检查\n");
//初始化Finish 未检查的全为0
int Finish[Pcount] = { 0 };
int Work[Scount] = { 0 };
//使Work=Available
Add(Work, Available);
for (int i = 0;i < Pcount;i++)
{//从第一个进程开始检查
//先检查Finish标志
if (Finish[i])
continue;
//检查Need是否小于Work
if (!Equals(Need[i], Work))
continue;
//若符合分配条件,则成功分配,完成任务,Work+Allocation
Add(Work, Allocation[i]);
//改变Finish标志
Finish[i]=1;
printf("p%d进程 Work=%d %d %d Finish=true\n", i, Work[0], Work[1], Work[2]);
//完成之后从头检查,i归零
i = -1;
}
//检查所有Finish标志,输出结果
if (CheckFinish(Finish))
{
printf("安全状态检查完毕,Finish全为True,系统为安全状态\n");
return 1;
}
else
{
printf("安全状态检查完毕,Finish存在False,系统处在不安全状态\n");
return 0;
}
}
void ApplyResulate(int P,int Request[Scount])
{//资源申请输出结果
printf("\n模拟P%d申请资源 %d %d %d \n",P, Request[0], Request[1], Request[2]);
int state = Apply(P, Request);
if (state)
{
printf("资源分配成功!\n");
PrintMssge();
}
else
{
printf("分配资源失败,进程P%d需要等待\n", P);
}
}
int Apply(int P, int Request[Scount])
{//资源申请操作
if (!Equals(Request, Need[P]))
{//判断申请的资源是否小于所需
printf("进程P%d申请的资源大于需求,申请失败\n",P);
return 0;
}
if (!Equals(Request, Available))
{//判断申请的资源是否小于可用
printf("进程P%d申请的资源大于可用资源,申请失败\n",P);
return 0;
}
//进行分配资源
Minus(Available, Request);
Add(Allocation[P], Request);
Minus(Need[P], Request);
//分配后进行安全性检查
int Safestate = CheckSafe();
if (Safestate)
{
//若安全则返回1
return Safestate;
}
//若不安全则恢复分配之前的数值
Add(Available, Request);
Minus(Allocation[P], Request);
Add(Need[P], Request);
return Safestate;
}
void Add(int* a, int b[Scount])
{
for (int i = 0;i < Scount;i++)
{
a[i] = a[i] + b[i];
}
}
void Minus(int* a, int b[Scount])
{
for (int i = 0;i < Scount;i++)
{
a[i] = a[i] - b[i];
}
}
int Equals(int a[Scount], int b[Scount])
{//比较资源 a<=b返回1 a>b返回0
for (int i = 0;i < Scount;i++)
{
if (a[i] > b[i])
return 0;
}
return 1;
}
int CheckFinish(int Finish[])
{
for (int i = 0; i < Scount;i++)
{
if (Finish[i] == 0)
{
return 0;
}
}
return 1;
}