一、实验目的要求
通过编写和调试一个系统动态分配资源的简单模拟程序,观察死锁产生的条件,掌握死锁产生的原因和必要条件以及银行家算法的实现。
二、实验材料和仪器设备
Windows操作系统环境下的个人微机。
三、实验内容
设计一个3进程共享10个资源的系统,进程可动态地申请和释放资源,系统利用银行家算法对进程运行过程中提出的资源请求进行检查,若分配后,系统处于安全状态则满足请求,否则拒绝申请。
程序要求:为了清楚地观察资源分配过程,程序中应有显示或打印各进程依次要求申请的资源号以及依次分配资源的情况,程序最终要打印输出银行家算法的资源申请情况和安全性状况。
四、实验要求
1、认真编写程序,编译环境不限;
2、独立调试程序,验证代码的正确性。
五、源代码
#include <iostream>
#include<iomanip>
using namespace std;
int resources = 5;//记录处理机当前可用资源数
int temp_resources = resources;//临时资源变量,进行预分配
int Notrun_processes = 3;//记录当前未执行的进程数
int stopped_processes = 0;//记录每一次遍历被阻塞的进程数
int Located_process[3];//定位每次选择执行的进程位置
int L = 0;
class Process
{
//进程类
public:
Process()
{
Max_resources = 0;
Allocation_resources = 0;
temp_Allocation_resources = Allocation_resources;
Need_resources = Max_resources - temp_Allocation_resources;
flag = false;
}
Process(int R1, int R2) :Max_resources(R1), Allocation_resources(R2)
{
//在构造函数初始化进程的最大资源需求量和已获得资源量
temp_Allocation_resources = Allocation_resources;
Need_resources = Max_resources - temp_Allocation_resources;
flag = false;
}
bool apply_resources(int R)
{
//申请资源
if (R <= temp_resources && R <= Need_resources)//申请的资源数小于临时资源变量和需要的资源数
{
Need_resources -= R;
temp_resources -= R;
temp_Allocation_resources += R;
return true;
}
else
{
cout << "Requesting resources illegally, and requests failed! " << endl;
return false;
}
}
void release_resources()
{
//释放资源
resources += temp_Allocation_resources;
//flag = true;
Max_resources = 0;
Need_resources = 0;
Allocation_resources = 0;
}
void setflag(bool f) {
flag = f; }
int getNeed_resources() {
return Need_resources; }
int gettemp_Allocation_resources() {
return temp_Allocation_resources; }
int getAllocation_resources() {
return Allocation_resources; }
int getMax_resources() {
return Max_resources; }
bool getflag() {
return flag; }
private:
int Max_resources;//进程需要的最大资源数
int temp_Allocation_resources;//进程已经获得的临时资源数,用于预分配
int Allocation_resources;//进程已经获得的资源数
int Need_resources;//进程仍需的资源数,其值等于 Max_resources - temp_Allocation_resources
bool flag;//判断进程是否已运行的状态标志
};
void Bankers_algorithm(Process* p)
{
//银行家算法
int location[3];//记录每次遍历进程序列中无法分配资源的进程的位置
int j = 0;
int temp = 0;//记录一次遍历进程序列后无法分配资源的数目
for (int i = 0; i < 3; i++)
{
if (!p[i].getflag() && p[i].getNeed_resources() <= temp_resources)
{
//遍历未完成的进程,判断其需求量是否小于当前可用量
//输出预分配表格
cout << "P" << i << " " << setw(12) << setfill(' ') << left << temp_resources << setw(12) << setfill(' ') << left << p[i].getNeed_resources() << setw(12) << setfill(' ') << left << p[i].gettemp_Allocation_resources() << setw(18) << setfill(' ') << left << temp_resources + p[i].gettemp_Allocation_resources() << setw(12) << setfill(' ') << left << !p[i].getflag() << endl;
temp_resources += p[i].gettemp_Allocation_resources();
p[i].setflag(true);
Notrun_processes--;
Located_process[L++] = i;
}
else if (!p[i].getflag() && p[i].getNeed_resources() > temp_resources)
location[j++] = i;//标记不可分配资源的进程的位置 Need_resources>resources
}
for (int i = 0; i < j; i++)
{
//确定一次遍历后无法分配资源的进程数
if (Notrun_processes != 0 && p[location[i]].getNeed_resources() > temp_resources)
{
temp++;
}
}
//判定是否再次遍历,若不可遍历则输出无安全序列
if (j != 0 && temp < j)
Bankers_algorithm(p);
else if (temp != 0 && temp == j)
{
temp_resources = resources;
cout << "这是不安全分配序列!" << endl;
}
else if (temp == 0 && j == 0)
{
cout << "这是安全分配序列! ";
for (int i = 0; i < L - 1; i++)
{
cout << "P" << Located_process[i] << " -> ";
p[Located_process[i]].release_resources();
}
cout << "P" << Located_process[L - 1] << endl;
p[Located_process[L - 1]].release_resources();
}
}
int main()
{
//定义四个进程并初始化每个进程的最大资源需求量和已分配的资源数
Process p[3] = {
Process(7, 0), Process(3, 2), Process(7, 3) };//可用资源数为5
if (p[0].apply_resources(1))
{
//如果申请资源数合法,则进行预分配
cout << setw(12) << setfill(' ') << left << "process" << setw(12) << setfill(' ') << left << "Work" << setw(12) << setfill(' ') << left << "Need" << setw(12) << setfill(' ') << left << "Allocation" << setw(18) << setfill(' ') << left << "Work+Allocation" << setw(12) << setfill(' ') << left << "Finish" << endl;
Bankers_algorithm(p);
}
//Bankers_algorithm(p);
cout << "-----------------------------------------------------" << endl << "资源分配过程: " << endl;
cout << setw(12) << setfill(' ') << left << "process" << setw(15) << setfill(' ') << left << "Max_resources" << setw(12) << setfill(' ') << left << "Allocation_resources" << endl;
//如果存在安全序列则输出分配后的各进程分配情况;如不存在,则输出初始分配情况
for (int i = 0; i < 3; i++)
cout << "p" << i << " " << setw(15) << setfill(' ') << left << p[i].getMax_resources() << setw(12) << setfill(' ') << left << p[i].getAllocation_resources() << endl;
return 0;
}
//默认分配给进程一0个资源(共需7个资源),进程二2个资源(共需3个资源),进程三3个资源(共需7个资源)