#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//自定义bool类型
typedef int bool;
#define false 0
#define true !false
//系统中所有进程数量
#define num_of_processes 5
//有三类资源 a,b,c
typedef struct {
int a;
int b;
int c;
}RESOURCE;
//总资源向量
RESOURCE R = { 10,5,7 };
//可用资源向量
RESOURCE V = { 10,5,7 };
//最大需求矩阵
RESOURCE C[num_of_processes] =
{
{ 7,5,3 },
{ 3,2,2 },
{ 9,0,2 },
{ 2,2,2 },
{ 4,3,3 }
};
//已分配资源数矩阵
RESOURCE A[num_of_processes] =
{
{ 0,0,0 },
{ 0,0,0 },
{ 0,0,0 },
{ 0,0,0 },
{ 0,0,0 }
};
//需求矩阵
RESOURCE Need[num_of_processes] =
{
{ 7,5,3 },
{ 3,2,2 },
{ 9,0,2 },
{ 2,2,2 },
{ 4,3,3 }
};
int safe[num_of_processes];
//试探分配
void ProbeAlloc(int proId, RESOURCE *res)
{
V.a -= res->a;
V.b -= res->b;
V.c -= res->c;
A[proId].a += res->a;
A[proId].b += res->b;
A[proId].c += res->c;
Need[proId].a -= res->a;
Need[proId].b -= res->b;
Need[proId].c -= res->c;
}
//若试探分配后进入不安全状态,将分配回滚
void RollBack(int proId, RESOURCE *res)
{
V.a += res->a;
V.b += res->b;
V.c += res->c;
A[proId].a -= res->a;
A[proId].b -= res->b;
A[proId].c -= res->c;
Need[proId].a += res->a;
Need[proId].b += res->b;
Need[proId].c += res->c;
}
//安全性检查
bool SafeCheck()
{
RESOURCE Work = V;
bool Finish[num_of_processes] = { false,false,false,false,false };
int i;
int j = 0;
for (i = 0; i < num_of_processes; i += 1)
{
//是否已检查过
if (Finish[i] == false)
{
//是否有足够的资源分配给该进程
if (Need[i].a <= Work.a && Need[i].b <= Work.b && Need[i].c <= Work.c)
{
//有则使其执行完成,并将已分配给该进程的资源全部回收
Work.a += A[i].a;
Work.b += A[i].b;
Work.c += A[i].c;
Finish[i] = true;
safe[j++] = i;
i = -1; //重新进行遍历
}
}
}
//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态
for (i = 0; i < num_of_processes; i += 1)
{
if (Finish[i] == false)
{
return false;
}
}
return true;
}
//资源分配请求
bool request(int proId, RESOURCE *res)
{
//request向量需小于Need矩阵中对应的向量
if (res->a <= Need[proId].a && res->b <= Need[proId].b && res->c <= Need[proId].c)
{
//request向量需小于V向量
if (res->a <= V.a && res->b <= V.b && res->c <= V.c)
{
//试探分配
ProbeAlloc(proId, res);
//如果安全检查成立,则请求成功,否则将分配回滚并返回失败
if (SafeCheck())
{
return true;
}
else
{
printf("!!!!!!\n安全性检查失败:系统将进入不安全状态,有可能引起死锁。\n");
printf("正在回滚...\n");
RollBack(proId, res);
}
}
else
{
printf("!!!!!!\n安全性检查失败:请求向量大于可利用资源向量。\n");
}
}
else
{
printf("!!!!!!\n安全性检查失败:请求向量大于需求向量。\n");
}
return false;
}
//输出需求矩阵C
void printC()
{
printf("需求矩阵 C:\n");
printf("proId\ta\tb\tc\n");
int i;
for (i = 0; i<num_of_processes; i++)
{
printf("P%d\t%d\t%d\t%d\n", i, C[i].a, C[i].b, C[i].c);
}
printf("\n");
}
//输出总资源向量R
void printR()
{
printf("总资源向量 R:\n");
printf("\ta\tb\tc\n");
printf("\t%d\t%d\t%d\n", R.a, R.b, R.c);
printf("\n");
}
//输出可用资源向量 V
void printV()
{
printf("可用资源向量 V:\n");
printf("\ta\tb\tc\n");
printf("\t%d\t%d\t%d\n", V.a, V.b, V.c);
printf("\n");
}
//输出已分配资源矩阵 A
void printA()
{
printf("已分配资源矩阵 A:\n");
printf("proId\ta\tb\tc\n");
int i;
for (i = 0; i<num_of_processes; i++)
{
printf("P%d\t%d\t%d\t%d\n", i, A[i].a, A[i].b, A[i].c);
}
printf("\n");
}
int main()
{
int ch;
printR();
printC();
if (SafeCheck())
{
printf("系统处于安全状态。\n");
printf("安全序列是{P%d,P%d,P%d,P%d,P%d}。\n", safe[0], safe[1], safe[2], safe[3], safe[4]);
}
else
{
printf("系统处于不安全状态。程序将退出...\n");
exit(1);
}
do
{
int process;
RESOURCE res;
printf("\n请依次输入请求分配的进程和对三类资源的请求数量\n");
printf("进程:");
scanf("%d", &process);
printf("三类资源:");
scanf("%d%d%d", &res.a, &res.b, &res.c);
getchar();
if (request(process, &res))
{
printf("\n系统处于安全状态,分配成功。\n");
printf("安全序列是{P%d,P%d,P%d,P%d,P%d}。\n\n", safe[0], safe[1], safe[2], safe[3], safe[4]);
printA();
printV();
}
else
{
printf("\n系统处于不安全状态!\n\n");
}
printf("是否继续分配?(Y/N):");
ch = getchar();
getchar();
} while (ch == 'Y' || ch == 'y');
printf("执行完毕。");
return 0;
}
说明:这个代码有借鉴网上的一份代码,但是出处已经不记得了。如侵权请留言删除