算法思想:
代码:
#include<iostream>
#include<string>
using namespace std;
#define numberOfProcess 5
#define numberOfReSource 3
int *available=new int[numberOfReSource];
int *work=new int[numberOfReSource];
string *securitySequence=new string[numberOfProcess];
typedef struct pcb {
string pName;//进程名
string *nameOfReSource = new string[numberOfReSource];//依次存放各类资源的名称
int *max = new int[numberOfReSource];//max[j]=K表示进程需要Rj类资源的最大数目为K
int *allocation = new int[numberOfReSource];//allocation[j]=K表示进程已分得Rj类资源的数目为K
int *need = new int[numberOfReSource];//need[j]=K表示进程还需要Rj类资源K个方能完成任务
bool finish;
struct pcb *next;//链接指针指向下一个进程
}PCB;
void printProcess(PCB* p) {//输出进程的相关信息
PCB *q = p->next;//q指向第一个进程
cout << endl << "所有进程的相关信息" << endl;
cout << " Need Allocation Finish" << endl;
cout << "进程 A B C A B C " << endl;
while (q) {//遍历进程
cout << q->pName << " ";
cout << q->need[0] << " " << q->need[1] << " " << q->need[2] << " ";
cout << q->allocation[0] << " " << q->allocation[1] << " " << q->allocation[2] << " ";
cout << (q->finish ? "true" : "false") << endl;
q = q->next;//q指向下一个进程
}
cout << endl;
}
void initProcess(PCB *p) {//初始化进程
cout << "初始化进程" << endl;
cout << "请依次输入资源的名称:";
string *nameOfReSource = new string[numberOfReSource];//该变量里依次存放资源的名称
for (int i = 0; i<numberOfReSource; i++)//依次存放各类资源的名称
cin >> nameOfReSource[i];
cout << "请依次输入当前可利用资源量:";
for (int i = 0; i<numberOfReSource; i++)
cin >> available[i];
PCB *q = p;
PCB *r = new PCB;//当前工作指针
for (int i = 0; i<numberOfProcess; i++) {
cout << "请输入进程的名字:";
cin >> r->pName;
cout << "请依次输入该进程的各类资源的最大需求量(例如:2 3 4 ...):";
for (int j = 0; j<numberOfReSource; j++)
cin >> r->max[j];
cout << "请依次输入该进程的各类资源的已经分配量(例如:2 3 4 ...):";
for (int j = 0; j<numberOfReSource; j++) {
cin >> r->allocation[j];
r->need[j] = r->max[j] - r->allocation[j];
r->finish = false;
}
q->next = r;
q = r;
r->next = new PCB;
r = r->next;//指针后移
}
r->next = NULL;
q->next = NULL;
q = NULL;
delete q;//释放结点
delete r;
printProcess(p);//输出此时进程的相关信息
cout << endl;
}
bool securityAlgorithm(PCB *p) {//安全性检查算法
PCB *m = p->next;//m指向第一个结点
PCB *s = p->next;//s指向第一个结点
int count = 0;
bool flag = true;
for (int i = 0; i<numberOfReSource; i++)//初始化work变量
work[i] = available[i];
cout << "初始时刻work变量的值:";
for (int j = 0; j<numberOfReSource; j++)
cout << work[j] << " ";
cout << endl;
while (s) {//将所有进程的finish初始化为false
s->finish = false;
s = s->next;
}
while (true) {//安全性检测
flag = true;
for (int j = 0; j<numberOfReSource; j++)
if (m->need[j]>work[j] && !m->finish) {
flag = false;
break;
}
if (flag && !m->finish) {//找到符合条件的进程
securitySequence[count++] = m->pName;//将该进程的名称加到安全序列中
for (int j = 0; j<numberOfReSource; j++)//修改work变量的值
work[j] = work[j] + m->allocation[j];
m->finish = true;//修改finish
cout << endl << "当前选中的进程:" << m->pName << endl;
cout << "该进程预分配后work变量的值:";//输出此刻work变量的值
for (int j = 0; j<numberOfReSource; j++)
cout << work[j] << " ";
cout << endl;
}
m = m->next;//m指向下一个进程
if (m == NULL)//m指向第一个进程
m = p->next;
s = p->next;
flag = false;
for (int i = 0; i<numberOfProcess; i++) {
if (s->finish) {
s = s->next;
continue;
}
flag = true;
for (int j = 0; j<numberOfReSource; j++)
if (s->need[j]>work[j]) {
flag = false;
break;
}
if (flag)//当前进程符合条件
break;
s = s->next;
}
if (!flag)//退出整个循环
break;
}
flag = true;
s = p->next;
for (int i = 0; i<numberOfProcess; i++) {
if (!s->finish) {
flag = false;
break;
}
s = s->next;
}
if (flag) {//全为true
cout << endl << "当前系统是安全的" << endl;
cout << "安全序列:";
for (int i = 0; i<numberOfProcess; i++)
cout << securitySequence[i] << " ";
cout << endl;
printProcess(p);
return true;
}
else {//不全为false
cout << "当前系统是不安全的" << endl;
printProcess(p);
return false;
}
}
int main() {
PCB *p = new PCB;
PCB *q;
initProcess(p);//初始化操作
securityAlgorithm(p);//安全性检测
string nameOfRequestResource;//请求资源的进程名
int *requestResource = new int[numberOfReSource];
bool flag = true;
while (true) {
cout << endl << "请输入发出请求资源的进程的名字:";
cin >> nameOfRequestResource;
cout << "请依次输入请求向量:";
for (int i = 0; i<numberOfReSource; i++) {
cin >> requestResource[i];
}
q = p->next;
while (q) {//找到当前进程
if (q->pName == nameOfRequestResource)
break;
q = q->next;
}
cout << endl << "银行家算法开始进行检查" << endl << endl;
flag = true;
for (int i = 0; i<numberOfReSource; i++) {
if (requestResource[i]>q->need[i] || requestResource[i]>available[i]) {
flag = false;
break;
}
}
if (flag) {//符合条件
cout << "银行家算法检查过后发现符合要求" << endl << endl;
for (int i = 0; i<numberOfReSource; i++) {
available[i] -= requestResource[i];
q->allocation[i] += requestResource[i];
q->need[i] -= requestResource[i];
}
cout << "下面进行安全性算法检查" << endl << endl;
if (!securityAlgorithm(p))//当前系统处于不安全状态
for (int i = 0; i<numberOfReSource; i++) {
available[i] += requestResource[i];
q->allocation[i] -= requestResource[i];
q->need[i] += requestResource[i];
}
}
else//不符合条件
cout << "银行家算法检查之后发现不符合要求" << endl << endl;
cout << endl;
string choose;
cout << endl << "是否还有进程要申请资源,若是则输入YES,否则输入NO:";
cin >> choose;
if (choose == "NO")
break;
}
q = NULL;
delete q;
return 0;
}
结果: