前言
关于进程模拟的c++问题,模拟银行家算法分配资源,分享一下题目和我的解答过程。
题目
设计3~4个并发进程,共享系统的10个同类不可抢占资源。各进程按银行家算法动态进行资源的申请和释放,编写程序模拟这一执行过程。
#include <iostream>
using namespace std;
struct PCB {
char state;
int request;
int max;
int allocation;
};
void random_allocation(PCB p[], int av);
bool safe_check(PCB p[], int av);
void bank_allocation(PCB p[], int av);
void show(PCB p[], int av);
void show_order(int order[]);
int main();
int main() {
PCB process[3];
int i;
int available = 10;
for (i = 0; i < 3; i++) {
cout << endl;
cout << "p" << i << "->max: ";
cin >> process[i].max;
if (process[i].max > 10) {
cout << endl;
cout << "错误,请重新输入!";
i--;
} else {
do {
cout << endl << "->allocation: ";
cin >> process[i].allocation;
} while ((process[i].allocation > process[i].max) ||
(available - process[i].allocation < 0));
available -= process[i].allocation;
process[i].request = 0;
process[i].state = 'R';
}
}
show(process, available);
cout << endl << "1--随机分配算法 2--银行家算法";
do {
cout << endl << "请选择: ";
cin >> i;
} while (!((i == 1) || (i == 2)));
if (i == 1)
random_allocation(process, available);
else {
do {
if (!safe_check(process, available)) {
cout << endl << "当前为不安全状态!";
cout << endl << " 1--退出 2--重置";
do {
cout << endl << "选择: ";
cin >> k;
} while ((k != 1) && (k != 2));
if (k == 2) {
available = 10;
for (j = 0; j < 3; j++) {
cout << endl << "p" << j << "->allocation: ";
cin >> process[j].allocation;
available -= process[j].allocation;
}
}
} else {
k = 0;
break;
}
} while (k == 2);
if (k == 1)
return -1;
else if (k == 0)
bank_allocation(process, available);
}
}
void show_order(int order[]) {
int i;
cout << endl << "安全序列为: ";
for (i = 0; i < 3; i++) {
cout << "p" << order[i] << " ";
}
}
void show(PCB p[], int av) {
int i;
cout << endl << "available=" << av;
cout << endl << "number max request allocation state";
for (i = 0; i < 3; i++) {
cout << endl << i;
cout << " ";
cout << p[i].max;
cout << " ";
cout << p[i].request;
cout << " ";
cout << p[i].allocation;
cout << " ";
cout << p[i].state;
}
}
void random_allocation(PCB p[], int av) {
int i = 0;
int j = 0;
int w;
int e;
while (1) {
if (i == 3)
i -= 3;
while (1) {
if (i == 3)
i -= 3;
else if (p[i].state == 'R') {
do {
cout << endl << "p" << i << "->request: ";
cin >> p[i].request;
} while (p[i].request > (p[i].max - p[i].allocation));
break;
} else
i++;
}
if (p[i].request <= av) {
av -= p[i].request;
p[i].allocation += p[i].request;
p[i].request = 0;
if (p[i].max == p[i].allocation) {
av += p[i].allocation;
p[i].state = 'E';
p[i].allocation = 0;
p[i].request = 0;
}
j = 0;
while (j < 3) {
if ((p[j].request <= av) && (p[j].state == 'W')) {
av += p[j].allocation;
p[j].request = 0;
p[j].state = 'E';
p[j].allocation = 0;
}
if ((p[j].max < av + p[j].allocation) && (p[j].state == 'W')) {
p[j].state = 'R';
}
j++;
}
show(p, av);
} else {
p[i].state = 'W';
show(p, av);
}
w = 0;
e = 0;
for (j = 0; j < 3; j++) {
if (p[j].state == 'W')
w++;
else if (p[j].state == 'E')
e++;
else
break;
}
if (((w + e) == 3) && (w != 0)) {
cout << endl << "发生死锁!";
return;
} else if (e == 3) {
cout << endl << "三个进程顺利执行完!";
return;
}
i++;
}
}
void bank_allocation(PCB p[], int av) {
int k;
int request;
int f;
int i;
show(p, av);
while (1) {
request = 0;
k = -1;
do {
if (k != -1)
cout << endl << "不是安全状态!";
av += request;
p[k].allocation -= request;
do {
cout << endl << "p" << "->NO.: ";
cin >> k;
} while (p[k].state != 'R');
do {
cout << endl << "p" << k << "->request: ";
cin >> request;
} while (request > (p[k].max - p[k].allocation));
if (request > av) {
p[k].request = request;
p[k].state = 'W';
break;
}
p[k].allocation += request;
av -= request;
} while (!safe_check(p, av));
if (p[k].allocation == p[k].max) {
p[k].state = 'E';
av += p[k].allocation;
p[k].allocation = 0;
}
for (i = 0; i < 3; i++) {
if ((p[i].state == 'W') && (p[i].request <= av)) {
if (safe_check(p, av)) {
p[i].allocation = p[i].request + p[i].allocation;
av -= p[i].request;
p[i].request = 0;
p[i].state = 'R';
if (p[i].max == p[i].allocation) {
p[i].state = 'E';
av += p[i].allocation;
p[i].allocation = 0;
}
} else {
cout << endl << "不是安全状态!原请求资源量无效。";
p[i].request = 0;
p[i].state = 'R';
}
}
}
show(p, av);
f = 0;
for (i = 0; i < 3; i++) {
if (p[i].state == 'E')
f++;
}
if (f == 3) {
cout << endl << "所有进程顺利执行完!";
break;
}
}
}
bool safe_check(PCB p[], int av) {
bool finish[3];
int order[3];
int i;
int j = 0;
int f = 0;
int k = 0;
int work;
int temp;
work = av;
for (i = 0; i < 3; i++) {
order[i] = 0;
if (p[i].state == 'E')
finish[i] = true;
else
finish[i] = false;
}
while (k < 3) {
for (i = 0; i < 3; i++) {
if ((p[i].state == 'W') && (p[i].request <= work) && (finish[i] == false)) {
temp = p[i].allocation;
p[i].allocation = p[i].request + p[i].allocation;
}
if (((p[i].allocation + work) >= p[i].max) && (finish[i] == false)) {
work = p[i].allocation + work;
finish[i] = true;
order[j] = i;
j++;
if (p[i].state == 'W')
p[i].allocation = temp;
}
}
k++;
}
for (i = 0; i < 3; i++)
if (finish[i])
f++;
if (f == 3) {
// show_order(order);
return true;
} else
return false;
}