用c++编程用银行家算法和随机算法分别设计一个资源分配程序运行这两个程序观察系统运行情况并对系统运行的每一步情况进行显示。
由于涉及到资源分配、进程管理等复杂问题,银行家算法和随机算法的实现需要一定的时间和技术。以下是一个简单的示例程序,仅供参考。
银行家算法示例程序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_PROCESS = 10;
const int MAX_RESOURCE = 5;
vector<int> available(MAX_RESOURCE);
vector<vector<int>> allocation(MAX_PROCESS, vector<int>(MAX_RESOURCE));
vector<vector<int>> need(MAX_PROCESS, vector<int>(MAX_RESOURCE));
vector<bool> finished(MAX_PROCESS, false);
void init()
{
available = {3, 3, 2, 1, 2};
allocation = {
{0, 1, 0, 0, 1},
{2, 0, 0, 1, 0},
{3, 0, 2, 1, 1},
{2, 1, 1, 0, 1},
{0, 0, 2, 0, 0},
};
need = {
{7, 4, 3, 1, 3},
{1, 2, 3, 1, 2},
{6, 0, 0, 0, 0},
{0, 1, 1, 0, 0},
{4, 3, 1, 1, 1},
};
}
bool is_safe_state()
{
vector<int> work = available;
vector<bool> finish(MAX_PROCESS, false);
int count = 0;
while (count < MAX_PROCESS) {
bool found = false;
for (int i = 0; i < MAX_PROCESS; i++) {
if (!finish[i]) {
bool ok = true;
for (int j = 0; j < MAX_RESOURCE; j++) {
if (need[i][j] > work[j]) {
ok = false;
break;
}
}
if (ok) {
found = true;
finish[i] = true;
count++;
for (int j = 0; j < MAX_RESOURCE; j++) {
work[j] += allocation[i][j];
}
cout << "执行进程 " << i << ",释放资源:" << allocation[i][0] << " " << allocation[i][1] << " " << allocation[i][2] << " " << allocation[i][3] << " " << allocation[i][4] << endl;
}
}
}
if (!found) {
return false;
}
}
return true;
}
void request_resource(int pid, const vector<int>& request)
{
bool ok = true;
for (int i = 0; i < MAX_RESOURCE; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
ok = false;
break;
}
}
if (ok) {
for (int i = 0; i < MAX_RESOURCE; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (is_safe_state()) {
cout << "允许进程 " << pid << " 请求资源:" << request[0] << " " << request[1] << " " << request[2] << " " << request[3] << " " << request[4] << endl;
} else {
cout << "拒绝进程 " << pid << " 请求资源:" << request[0] << " " << request[1] << " " << request[2] << " " << request[3] << " " << request[4] << endl;
for (int i = 0; i < MAX_RESOURCE; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
} else {
cout << "请求资源超过进程 " << pid << " 的需求或系统可用资源" << endl;
}
}
int main()
{
init();
if (is_safe_state()) {
cout << "初始状态是安全状态" << endl;
} else {
cout << "初始状态不是安全状态" << endl;
return 0;
}
request_resource(1, {1, 0, 2, 0, 1});
request_resource(2, {1, 2, 0, 1, 0});
request_resource(0, {0, 1, 0, 1, 0});
return 0;
}
随机算法示例程序:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX_PROCESS = 10;
const int MAX_RESOURCE = 5;
vector<int> available(MAX_RESOURCE);
vector<vector<int>> allocation(MAX_PROCESS, vector<int>(MAX_RESOURCE));
vector<vector<int>> need(MAX_PROCESS, vector<int>(MAX_RESOURCE));
vector<bool> finished(MAX_PROCESS, false);
void init()
{
available = {3, 3, 2, 1, 2};
allocation = {
{0, 1, 0, 0, 1},
{2, 0, 0, 1, 0},
{3, 0, 2, 1, 1},
{2, 1, 1, 0, 1},
{0, 0, 2, 0, 0},
};
need = {
{7, 4, 3, 1, 3},
{1, 2, 3, 1, 2},
{6, 0, 0, 0, 0},
{0, 1, 1, 0, 0},
{4, 3, 1, 1, 1},
};
}
bool is_safe_state()
{
vector<int> work = available;
vector<bool> finish(MAX_PROCESS, false);
int count = 0;
while (count < MAX_PROCESS) {
bool found = false;
for (int i = 0; i < MAX_PROCESS; i++) {
if (!finish[i]) {
bool ok = true;
for (int j = 0; j < MAX_RESOURCE; j++) {
if (need[i][j] > work[j]) {
ok = false;
break;
}
}
if (ok) {
found = true;
finish[i] = true;
count++;
for (int j = 0; j < MAX_RESOURCE; j++) {
work[j] += allocation[i][j];
}
cout << "执行进程 " << i << ",释放资源:" << allocation[i][0] << " " << allocation[i][1] << " " << allocation[i][2] << " " << allocation[i][3] << " " << allocation[i][4] << endl;
}
}
}
if (!found) {
return false;
}
}
return true;
}
void request_resource(int pid, const vector<int>& request)
{
bool ok = true;
for (int i = 0; i < MAX_RESOURCE; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
ok = false;
break;
}
}
if (ok) {
for (int i = 0; i < MAX_RESOURCE; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (is_safe_state()) {
cout << "允许进程 " << pid << " 请求资源:" << request[0] << " " << request[1] << " " << request[2] << " " << request[3] << " " << request[4] << endl;
} else {
cout << "拒绝进程 " << pid << " 请求资源:" << request[0] << " " << request[1] << " " << request[2] << " " << request[3] << " " << request[4] << endl;
for (int i = 0; i < MAX_RESOURCE; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
} else {
cout << "请求资源超过进程 " << pid << " 的需求或系统可用资源" << endl;
}
}
int main()
{
init();
if (is_safe_state()) {
cout << "初始状态是安全状态" << endl;
} else {
cout << "初始状态不是安全状态" << endl;
return 0;
}
srand(time(NULL));
for (int i = 0; i < 10; i++) {
int pid = rand() % MAX_PROCESS;
vector<int> request(MAX_RESOURCE);
for (int j = 0; j < MAX_RESOURCE; j++) {
request[j] = rand() % (need[pid][j] + 1);
}
request_resource(pid, request);
}
return 0;
}
注意,以上示例程序仅仅是一个简单的演示,并没有考虑到所有情况,实际应用中需要根据具体需求进行修改和完善
原文地址: https://www.cveoy.top/t/topic/eKlM 著作权归作者所有。请勿转载和采集!