源码
#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;
CountMax();
}
void CountMax()
{
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");
int Finish[Pcount] = { 0 };
int Work[Scount] = { 0 };
Add(Work, Available);
for (int i = 0; i < Pcount; i++)
{
if (Finish[i])
continue;
if (!Equals(Need[i], Work))
continue;
Add(Work, Allocation[i]);
Finish[i] = 1;
printf("p%d进程 Work=%d %d %d Finish=true\n", i, Work[0], Work[1], Work[2]);
i = -1;
}
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)
{
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])
{
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;
}