#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> // bool 类型 int N = 0; // 进程数目
int M = 0; // 资源数目
int* Available; // 可利用资源向量 M
int** Max; // 最大需求矩阵 M * N
int** Allocation; // 分配矩阵 M * N
int** Need; // 需求矩阵 M * N // 初始化数据结构
void init(); // 销毁数据结构
void destory(); // 安全性检查算法
void security_checks(); int main(int argc, char const *argv[]) {
init();
security_checks();
destory();
return 0;
} /**
* 初始化数据结构
*/
void init() {
printf("依次输入进程数目和资源类型数目:");
scanf("%d%d", &N, &M); // 分配内存
Available = malloc(sizeof(int*) * N);
Max = malloc(sizeof(int*) * N);
Allocation = malloc(sizeof(int*) * N);
Need = malloc(sizeof(int*) * N);
for (int i = 0; i < N; i++) {
Max[i] = malloc(sizeof(int) * M);
Allocation[i] = malloc(sizeof(int) * M);
Need[i] = malloc(sizeof(int) * M);
} // 赋值
for (int i = 0; i < M; i++) {
printf("输入资源类型%d的数目:", i);
scanf("%d", &(Available[i]));
} for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("依次输入进程%d对资源类型%d的最大需求及已获得的数量:", i, j);
scanf("%d%d", &(Max[i][j]), &(Allocation[i][j]));
Available[j] -= Allocation[i][j];
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
} /**
* 销毁数据结构
*/
void destory() {
free(Available);
for (int i = 0; i < N; i++) {
free(Max[i]);
free(Need[i]);
free(Allocation[i]);
}
} /**
* 安全性检查算法
*/
void security_checks() {
int Work[M];
bool Finish[N];
for (int i = 0; i < N; i++) {
Finish[i] = false;
}
for (int i = 0; i < M; i++) {
Work[i] = Available[i];
} int finish_count = 0; // 结束任务的进程数目
char security_seq[N][5]; // 安全序列
while (true) {
bool had_allocated = false; // 判断本轮是否分配
for (int i = 0; i < N; i++) {
if (Finish[i] == false) { // 找一个没有结束的进程
bool all_valid = true;
for (int j = 0; j < M; j++) { // 如果对所有资源的Need[i][j]都小于Work[j],则分配
if (Need[i][j] > Work[j]) { // 但凡有一个不满足,则不分配
all_valid = false;
break;
}
}
if (all_valid == true) { // 分配并回收资源
for (int j = 0; j < M; j++) {
Work[j] += Allocation[i][j];
}
Finish[i] = true;
had_allocated = true;
sprintf(security_seq[finish_count++], "P%d\0", i);
}
}
}
if (finish_count == N) { // 结束,安全
puts("系统处于安全状态");
printf("进程安全序列为:");
for (int i = 0; i < N; i++) { // 输出安全序列
printf("[%s]", security_seq[i]);
if (i != N - 1) printf(" => ");
else puts("");
}
return ;
} else if (had_allocated == false) { // 结束,不安全
puts("系统处于不安全状态");
return ;
}
}
}

  

最新文章

  1. EditText获取和失去焦点,软键盘的关闭,和软键盘的显示和隐藏的监听
  2. How.To.Process.Image.Infomation.Of.Rotate.And.Flip.From.Server
  3. ORACLE 9i 数据库体系结构图
  4. Hadoop在eclipse中的配置
  5. ncurses-devel
  6. mysql 5.6.33发布
  7. c#局域网文件搬移
  8. Chocolatey:Windows软件包管理器
  9. bootstrap 下的 validation插件
  10. 在wdcp环境下架设VSFTPD虚拟用户只上传功能服务器
  11. centos一些命令
  12. [刷题]Codeforces 794C - Naming Company
  13. sql操作一般函数
  14. Zabbix实战-简易教程(1)--总流程
  15. Java之split方法
  16. 剑指offer-删除链表中重复的节点
  17. 【一天一道LeetCode】#72. Edit Distance
  18. 最新鲜最详细的Android SDK下载安装及配置教程
  19. leetcode — largest-rectangle-in-histogram
  20. .NET CORE学习笔记系列(6)——KestrelServer

热门文章

  1. UI自动化和selenium相关以及八大定位
  2. SpringBoot-2.1.1系列二:使用websocket
  3. 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
  4. C# Post发送 接受Xml
  5. npm全局模块卸载及默认安装目录修改
  6. CentOS6.5源码安装mysql-5.5.21
  7. 【转】DB2数据库编目的概念以及对其的正确解析
  8. YOLOv3 K-means获取anchors大小
  9. cogs 1963. [HAOI 2015] 树上操作 树链剖分+线段树
  10. Webpack实战(四):教教你如何轻松搞定-预处理器(loader)