具体原理可参考书本或者其他的文章,使用到的std::vector就是c++的可变数组,如果有问题可以询问GPT
源码
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include <vector>
int resorces_number;//资源个数
int process_number;//进程个数
int kill_dir=0;//终结指针
std::vector<std::vector<int>> proccess_max;//各个进程资源所需最大值
std::vector<std::vector<int>> proccess_allocation;//各个进程已占用资源
std::vector<std::vector<int>> proccess_need;//各个进程仍需资源
std::vector<int> add_mid;//中间变量
std::vector<int> sub_mid;//中间变量
std::vector<int> resources;//各个资源最大值
std::vector<int> available;//各个资源剩余量
std::vector<int> request;//请求分配的各个资源
std::vector<int> kill;//终结队列
int compare(std::vector<int> resource_available, std::vector<int> request)//前者为容积,后者为请求
{
for (int i = 0; i < resorces_number; i++) {
if (request[i] > resource_available[i])// 存在某种资源现无法满足请求
return -1;
}
return 1;//1现有资源可以满足要求
}
void add(std::vector<int> a, std::vector<int> b){//把相加得到的值放到add_mid中
for (int i = 0; i < resorces_number; i++)
add_mid[i] = a[i] + b[i];
}
void sub(std::vector<int> a, std::vector<int> b) {//把相减得到的值放到sub_mid中
for (int i = 0; i < resorces_number; i++)
sub_mid[i] = a[i] - b[i];
}
void copy(std::vector<int>& a, std::vector<int> b)//把b赋值给a
{
for (int i = 0; i < resorces_number; i++)
a[i] = b[i];
}
int finishfind(std::vector<int> finish, int j) {
for (int k = 0; k < process_number; k++)
{
if (finish[k] == j)//找到了
{
return -1;//返回-1
}
}
return 1;//没找到,返回1
}
void showsomething(std::vector<int> a) {//查错专用
printf_s("\n***查看***\n");
for (int i = 0; i < resorces_number; i++)
printf_s("%d ", a[i]);
printf_s("\n");
}
int safecheck() {
std::vector<int> finish;
finish.resize(process_number);//完成队列
std::vector<int> work;
work.resize(process_number);
copy(work, available);//把available给work
for (int i = 0; i < process_number;i++) {//全部初始化为-1
finish[i] = -1;
}
for (int i = 0; i < process_number;) {
int flag = 0;//标志
for (int j = 0; j < process_number; j++) {//检查
if (finishfind(finish, j) == 1) {
if (compare(work,proccess_need[j]) == 1) {//1代表现有资源可以满足进程j的需求
add(work, proccess_allocation[j]);//把j进程现在占有的资源和work加起来,然后放入work中
copy(work, add_mid);
//showsomething(work);
finish[i] = j;//j进程进入结束队列
i++;
flag = 1;
}
}
}
if (flag == 1)//flag改变了代表有进程可以结束,继续下一次查找
continue;
else if (flag == 0 && i != process_number) {//flag没有变代表没有进程可以结束,死锁
return -1;
}
}
std::cout << "拥有结束队列:";
for (int i = 0; i < process_number; i++)
std::cout << finish[i] << " ";
std::cout <<std:: endl;
return 1;//所有进程都能结束
}
void init()
{
std::cout << "输入资源个数" << std::endl;//初始化资源
std::cin >> resorces_number;
resources.resize(resorces_number);
available.resize(resorces_number);
add_mid.resize(resorces_number);
sub_mid.resize(resorces_number);
request.resize(resorces_number);
std::cout << "输入进程个数" << std::endl;//初始化进程
std::cin >> process_number;
kill.resize(process_number);
proccess_max.resize(process_number, std::vector<int>(resorces_number));
proccess_allocation.resize(process_number, std::vector<int>(resorces_number));
proccess_need.resize(process_number, std::vector<int>(resorces_number));
for (int i = 0; i < process_number; i++) {//全部初始化为-1
kill[i] = -1;
}
std::cout << "输入各个资源最大值" << std::endl;
for (int i = 0; i < resorces_number; i++) {
std::cout << "资源" << i << "最大值:";
std::cin >> resources[i];
}
std::cout << "输入各个进程所需资源最大值" << std::endl;
for (int i = 0; i < process_number; i++) {
std::cout << "进程P" << i << "所需资源最大值:" << std::endl;
for (int j = 0; j < resorces_number; j++) {
std::cout << "资源" << j << "所需资源最大值:";
std::cin >> proccess_max[i][j];
}
std::cout << "\n";
}
std::cout << "输入各个进程当前占用资源" << std::endl;
for (int i = 0; i < process_number; i++) {
std::cout << "进程P" << i << "当前占用资源:" << std::endl;
for (int j = 0; j < resorces_number; j++) {
std::cout << "资源" << j << "被占用:";
std::cin >> proccess_allocation[i][j];
}
std::cout << "\n";
}
std::cout << "正在计算剩余资源以及各个进程仍然需要的资源" << std::endl;
for (int i = 0; i < process_number; i++) {
add(add_mid, proccess_allocation[i]);
sub(proccess_max[i], proccess_allocation[i]);
copy(proccess_need[i],sub_mid);
}
sub(resources, add_mid);
copy(available,sub_mid);
std::cout << "计算剩余资源以及各个进程仍然需要的资源完成" << std::endl;
std::cout << "初始化完成" << std::endl;
//std::cout << "------------------------------------------" << std::endl;
std::cout << "初始状态检查中" << std::endl;
for (int i = 0; i < resorces_number; i++)
if (available[i] < 0)
{
std::cout << "资源分配不够用,请重试" << std::endl; exit(0);
}
else
std::cout << available[i] << " ";
std::cout << "初始状态分配正常" << std::endl;
std::cout << std::endl;
}
void show() {
std::cout << "显示当前状态:"<< std::endl;
std::cout << "------------------------------------------------------------------------------------" << std::endl;
std::cout << "id\tmax\t\tallocation\tneed\n" ;
for(int i=0;i<process_number;i++){
if (finishfind(kill, i) == -1)
std::cout << "P" << i << "(X)\t";
else
std::cout << "P"<< i<< " \t";
for (int j = 0; j < resorces_number; j++) {
std::cout << proccess_max[i][j] << " ";
}std::cout << "\t\t";
for (int j = 0; j < resorces_number; j++) {
std::cout << proccess_allocation[i][j] << " ";
}std::cout << "\t\t";
for (int j = 0; j < resorces_number; j++) {
std::cout << proccess_need[i][j] << " ";
}std::cout << "\t\t";
std::cout << std::endl;
}
std::cout << "------------------------------------------------------------------------------------" << std::endl;
std::cout << "剩余资源available:";
for (int i = 0; i < resorces_number; i++) {
std::cout << available[i] << " ";
}
std::cout << std::endl;
}
void run() {
int pro;
std::cout << "输入请求分配的进程id号(从0开始)" << std::endl;//初始化资源
std::cin >> pro;
std::cout << "输入请求分配各个资源" << std::endl;//初始化资源
for (int i = 0; i < resorces_number; i++) {
std::cout << "资源请求"<<i<<"分配:" << std::endl;
std::cin >> request[i];
}
if (compare(available, request) < 0)
{std::cout << "剩余资源无法满足分配需求,请重试" << std::endl; return;}//不满足分配则返回
else
std::cout << "剩余资源可以满足分配" << std::endl;
if (compare(proccess_need[pro], request) == -1)//请求大于进程max,及请求各个资源的数量有问题
{
std::cout << "本次资源请求后进程持有资源大于该进程最大资源上限,请重试" << std::endl;
return;
}
std::cout << "正在进行安全性检查" << std::endl;
sub(available, request);
copy(available, sub_mid);
sub(proccess_need[pro], request);
copy(proccess_need[pro], sub_mid);
add(proccess_allocation[pro], request);
copy(proccess_allocation[pro], add_mid);
if (safecheck() == 1)
{
std::cout << "本次资源请求安全,进程现在已经持有请求资源" << std::endl;
return;
}
else
{
add(available, request);
copy(available, add_mid);
add(proccess_need[pro], request);
copy(proccess_need[pro], add_mid);
sub(proccess_allocation[pro], request);
copy(proccess_allocation[pro], sub_mid);
std::cout << "本次资源请求不安全,请重试" << std::endl;
}
}
void end() {
int pro;
std::cout << "输入强制结束的进程id号(从0开始)" << std::endl;//初始化资源
std::cin >> pro;
add(available, proccess_allocation[pro]);
copy(available, add_mid);
sub(proccess_need[pro], proccess_need[pro]);
copy(proccess_need[pro], sub_mid);
if(kill_dir<process_number)
{
kill[kill_dir] = pro;
}else
{
std::cout << "所有进程全部结束" << std::endl;
}
}
int main()
{
init();
show();
while (1) {
int choose;
std::cout << "1:显示当前状态\n2:请求分配\n3:强制释放" << std::endl;
std::cin >> choose;
switch (choose) {
case 1:show(); break;
case 2:run(); break;
case 3:end(); break;
}
}
return 0;
}
运行测试(复制后直接黏贴回车即可)
3
5
10
5
7
7
5
3
3
2
2
9
0
2
2
2
2
4
3
3
0
1
0
2
0
0
3
0
2
2
1
1
0
0
2