操作系统课程实验2-死锁
一、实验介绍
1.1 实验目的
通过对银行家算法的模拟加深对避免死锁的理解,掌握银行家算法和安全性测试算法;
1.2 实验内容
系统中有m个同类资源,被n个进程共享,每个进程对资源的最大需求数分别为S1、S2、…、Sn,且Max(Si)<=m(i=1,2,…,n)。进程可以动态地申请资源和释放资源。编写一个程序,实现银行家算法模拟分配资源以及进行安全性检测。当系统将资源分配给某一进程而不会死锁时,就分配之。否则,推迟分配,并显示适当信息。
1.3 实验要求
- 将本实验分成两个阶段,第一阶段实现系统安全性检测算法(在系统安全的情况下要求输出安全序列), 第二阶段实现银行家算法。
- 要求用户能自主地输入不同的向量矩阵。
- 程序能正确输出不同的运算结果。
- 程序应具备良好的容错能力。
1.4 参考测试数据
假设系统中有5个进程{P0,P1,P2,P3,P4},3类资源{A,B,C},资源总数为10、5、7。
- P1请求资源Request1 (1,0,2);
- P4请求资源Request4 (3,3,0);
- P0请求资源Request0 (0,2,0)。
二、实现代码
#include <iostream> // 引入输入输出流库
#include <queue> // 引入队列库
#define N 5 // 定义进程数量
#define M 3 // 定义资源类型数量
using namespace std; // 使用标准命名空间
// 定义银行家算法使用的结构体
typedef struct BANKER {
int MAX[M]; // 定义每个进程的最大需求量
int Allocation[M]; // 定义每个进程已分配的资源量
int Need[M]; // 定义每个进程还需要的资源量
bool finish_flag; // 定义进程是否已完成的标志
} BK;
BK P[N]; // 定义进程数组
int Available[M]; // 定义可用资源数组
int Work[M]; // 定义工作数组
int finishcount; // 定义已完成进程计数
int NumOfSafeQueue; // 定义安全序列计数
int Safe_Queue[N]; // 定义安全序列数组
void Input(); // 输入函数声明
bool Safety_Check(); // 安全性检查函数声明
bool compare(int a[M], int b[M]); // 比较函数声明
void BankerA(); // 银行家算法函数声明
int main(void) { // 主函数
Input(); // 调用输入函数
BankerA(); // 调用银行家算法函数
return 0; // 返回0,表示程序执行成功
}
void Input() {
printf("MAX(0->m) Allocation(0->m) Need(0->m)\n"); // 输出提示信息
printf("Please input the initial data to the algorithm of banker,separate by space and the aforementioned tips is data formate:\n"); // 输出提示信息
for (int i = 0; i < N; i++) { // 循环输入每个进程的最大需求量、已分配资源量和还需要的资源量
printf("Process%d:", i); // 输出提示信息
cin >> P[i].MAX[0] >> P[i].MAX[1] >> P[i].MAX[2]; // 输入最大需求量
cin >> P[i].Allocation[0] >> P[i].Allocation[1] >> P[i].Allocation[2]; // 输入已分配资源量
cin >> P[i].Need[0] >> P[i].Need[1] >> P[i].Need[2]; // 输入还需要的资源量
}
cout << "Please input the Initial data about Available:"; // 输出提示信息
cin >> Available[0] >> Available[1] >> Available[2]; // 输入可用资源量
}
bool Safety_Check() { // 安全性检查函数
for (int i = 0; i < N; i++) { // 循环检查每个进程
if (P[i].finish_flag == false) // 如果进程未完成
if (compare(P[i].Need, Work)) { // 如果进程需要的资源量小于等于工作数组
P[i].finish_flag = true; // 设置进程已完成标志
for (int j = 0; j < M; j++) // 更新工作数组
Work[j] += P[i].Allocation[j];
finishcount++; // 完成进程计数加1
Safe_Queue[finishcount - 1] = i; // 将进程加入安全序列
if (finishcount == N) { // 如果所有进程都已加入安全序列
NumOfSafeQueue++; // 安全序列计数加1
cout << "安全序列:"; // 输出安全序列
for (int j = 0; j < N; j++)
cout << Safe_Queue[j] << ' ';
cout << endl;
} else
Safety_Check(); // 递归调用安全性检查函数
P[i].finish_flag = false; // 重置进程已完成标志
for (int j = 0; j < M; j++) // 重置工作数组
Work[j] -= P[i].Allocation[j];
Safe_Queue[finishcount - 1] = -1;; // 重置安全序列
finishcount--; // 完成进程计数减1
} else {
printf("条件不满足:%d %d %d 与%d %d %d\n", P[i].Need[0], P[i].Need[1], P[i].Need[2], Work[0], Work[1], Work[2]); // 输出条件不满足的信息
}
}
if (NumOfSafeQueue > 0) // 如果存在安全序列
return true; // 返回真
else
return false;
}
bool compare(int a[M], int b[M]) { // 比较函数
for (int i = 0; i < M; i++) // 循环比较每个资源类型
if (a[i] > b[i]) // 如果第一个进程的资源需求大于第二个进程的资源量
return false; // 返回假
return true; // 如果所有资源类型都满足条件,返回真
}
void BankerA() { // 银行家算法函数
int NumR, APObj, Res[M]; // 定义请求次数、请求进程编号和请求资源数组
cout << endl << "please input the num of Request:"; // 输出提示信息
cin >> NumR; // 输入请求次数
for (int i = 0; i < NumR; i++) { // 循环处理每个请求
printf("request%d:", i + 1); // 输出请求编号
cin >> APObj ; // 输入请求进程编号
for (int j = 0; j < M; j++) { // 输入请求的资源量
cin >> Res[j];
}
// 银行家首要条件
if (compare(Res, P[APObj].Need)) {
if ( compare(Res, Available)) {
// 如果请求的资源量小于等于进程的需求数量和可用资源量
// 更新可用资源、已分配资源和还需资源
for (int j = 0; j < M; j++) {
Available[j] -= Res[j];
P[APObj].Allocation[j] += Res[j];
P[APObj].Need[j] -= Res[j];
}
// 初始化所有进程的可完成状态为false
for (int i = 0; i < N; i++) {
P[i].finish_flag = false;
}
// 初始化work为available
for (int j = 0; j < M; j++) {
Work[j] = Available[j];
}
// 清空安全队列
for (int i = 0; i < N; i++) {
Safe_Queue[i] = -1;
}
// 初始化计数
finishcount = 0;
NumOfSafeQueue = 0;
// 安全性算法
if (Safety_Check()) { // 如果安全性检查通过
// 打印资源矩阵,可视化状态
for (int j = 0; j < N; j++) {
printf("Process%d:", j);
cout << P[j].MAX[0] << P[j].MAX[1] << P[j].MAX[2] << ' ' << P[j].Allocation[0] << P[j].Allocation[1] << P[j].Allocation[2] << ' ' << P[j].Need[0] << P[j].Need[1] << P[j].Need[2];
cout << endl;
}
cout << "available:" << Available[0] << ' ' << Available[1] << ' ' << Available[2] << endl;
cout << "It is successful to allocate the resource,next request" << endl << endl;
} else { // 如果安全性检查失败
cout << "the condition have not been quilified,So failing to allocation resource!" << endl << endl;
for (int j = 0; j < M; j++) { // 恢复资源状态
Available[j] += Res[j];
P[APObj].Allocation[j] -= Res[j];
P[APObj].Need[j] += Res[j];
}
}
} else // 如果请求的资源量不满足条件
cout << "可用资源不足,进程阻塞" << endl; // 输出换行
} else
cout << "申请不合理,进程终止";
}
return; // 结束函数
}
三、心灵的救赎
- 那些不经意想起的,总是记忆力最深刻的。