重复申请资源缺少跳出 *** 作,但是懒得改了
#include#define MAX 10 using namespace std; int num; // 进程数 int kind; // 资源种类 int Max[MAX][MAX]; // 最大需求矩阵 int Allocation[MAX][MAX]; // 分配矩阵 int Need[MAX][MAX]; // 需求矩阵 int Allocation_final[MAX][MAX]; // 安全分配矩阵 int Need_final[MAX][MAX]; // 安全需求矩阵 int Available[MAX]; // 可利用资源向量 int work[MAX]; // 工作向量 int work_matrix[MAX][MAX]; // 工作矩阵 int work_allocation[MAX][MAX]; // work + allocation int job_id[MAX]; // 存放进程的编号 bool Finish[MAX] = { false }; // 安全性检测标志 int request[MAX]; // 进程申请向量 int id; // 申请资源的进程的编号 int safe = 1; // 是否存在安全序列 int flag = 0; // 输入函数 void input() { cout << "请输入进程数:"; cin >> num; cout << "请输入资源种类数:"; cin >> kind; cout << "请输入最大需求矩阵Max:" << endl; for (int i = 0; i < num; i++) { for (int j = 0; j < kind; j++) { cin >> Max[i][j]; } } cout << "请输入分配矩阵Allocation:" << endl; for (int i = 0; i < num; i++) { for (int j = 0; j < kind; j++) { cin >> Allocation[i][j]; } } cout << "请输入需求矩阵Need:" << endl; for (int i = 0; i < num; i++) { for (int j = 0; j < kind; j++) { cin >> Need[i][j]; } } cout << "请输入可利用资源向量Available:" << endl; for (int i = 0; i < kind; i++) { cin >> Available[i]; } } // 展示函数 void display() { cout << "n分配序列为:" << endl; cout << "t " << "Max" << "tt" << "Allocation" << "t" << "Need" << "tt" << "Available" << endl; for (int i = 0; i < num; i++) { cout << "进程" << i; cout << "t "; for (int j = 0; j < kind; j++) { cout << Max[i][j] << " "; } cout << "t "; for (int j = 0; j < kind; j++) { cout << Allocation[i][j] << " "; } cout << "t"; for (int j = 0; j < kind; j++) { cout << Need[i][j] << " "; } if (i == 0) { cout << "tt "; for (int j = 0; j < kind; j++) { cout << Available[j] << " "; } } cout << "n"; } } // 安全性检测 int check_security() { int flag1 = 0; // 如果need <= available ,flag将等于资源种类数目 memcpy(work, Available, sizeof(int) * kind);// work = available // 遍历work_matrix矩阵,找出安全序列中的第一个进程 for (int k = 0; k < num; k++) { // 遍历Need矩阵,找出第一个need<=available for (int i = 0; i < num; i++) { // need <= available for (int j = 0; j < kind; j++) { if (Need[i][j] < Available[j] || Need[i][j] == Available[j]) { flag1 = flag1 + 1; } } if (flag1 == kind) { Finish[i] = true; memcpy(work_matrix[k], work, sizeof(int) * kind); memcpy(Need_final[k], Need[i], sizeof(int) * kind); memcpy(Allocation_final[k], Allocation[i], sizeof(int) * kind); job_id[k] = i; // work_allocation = work + allocation for (int p = 0; p < kind; p++) { work_allocation[k][p] = work_matrix[k][p] + Allocation[i][p]; } // work = work + allocation memcpy(work, work_allocation[k], sizeof(int) * kind); break; } } if (flag1 == kind) { break; } } // 再次遍历work_matrix和Need for (int k = 1; k < num; k++) { for (int i = 0; i < num; i++) { flag1 = 0; if (Finish[i] != true) { // need <= available for (int j = 0; j < kind; j++) { if (Need[i][j] < work[j] || Need[i][j] == work[j]) { flag1 = flag1 + 1; } } if (flag1 == kind) { Finish[i] = true; memcpy(work_matrix[k], work, sizeof(int) * kind); memcpy(Need_final[k], Need[i], sizeof(int) * kind); memcpy(Allocation_final[k], Allocation[i], sizeof(int) * kind); job_id[k] = i; // work_allocation = work + allocation for (int p = 0; p < kind; p++) { work_allocation[k][p] = work_matrix[k][p] + Allocation[i][p]; } // work = work + allocation memcpy(work, work_allocation[k], sizeof(int) * kind); break; } } else { continue; } } } // 检查是否安全 for (int i = 0; i < num; i++) { if (Finish[i] != true) { cout << "n不存在安全序列" << endl; safe = 0; return 0; } } } // 输出安全性检测后的序列信息 void output_security() { cout << "n存在安全序列:" << endl; cout << "t " << "Work" << "tt " << "Need" << "tt " << "Allocation" << "tt" << "Work+Allocation" << "tt" << "Finish" << endl; for (int i = 0; i < num; i++) { cout << "进程" << job_id[i]; cout << "t "; for (int j = 0; j < kind; j++) { cout << work_matrix[i][j] << " "; } cout << "t "; for (int j = 0; j < kind; j++) { cout << Need[job_id[i]][j] << " "; } cout << "t "; for (int j = 0; j < kind; j++) { cout << Allocation[job_id[i]][j] << " "; } cout << "tt "; for (int j = 0; j < kind; j++) { cout << work_allocation[i][j] << " "; } cout << "tt truen"; } } // 申请资源 void Request() { int flag = 1; cout << "n请输入要申请的进程的编号:"; cin >> id; cout << "请输入进程" << id << "的请求向量request:" << endl; // 输入request for (int i = 0; i < kind; i++) { cin >> request[i]; } // 判断申请是否成功 for (int j = 0; j < kind; j++) { if (request[j] > Need[id][j] || request[j] > Available[j]) { cout << "n进程" << id << "的申请不能得到满足" << endl; flag = 0; break; } } // 请求成功后 for (int j = 0; j < kind; j++) { Available[j] = Available[j] - request[j]; Allocation[id][j] = Allocation[id][j] + request[j]; Need[id][j] = Need[id][j] - request[j]; } check_security(); // 检测是否安全 if (safe == 0 && flag == 1) { cout << "n进程" << id << "的申请不能得到满足" << endl; } // 为存在安全序列服务 if (safe == 0) { flag = 0; } if (safe == 1 && flag == 1) { output_security(); cout << "n进程" << id << "申请资源成功" << endl; } if (flag == 0 && safe == 1) { // 恢复到原始数据 for (int j = 0; j < kind; j++) { Available[j] = Available[j] + request[j]; Allocation[id][j] = Allocation[id][j] - request[j]; Need[id][j] = Need[id][j] + request[j]; } Request(); } } int main() { input(); display(); check_security(); if (safe == 1) { output_security(); } while (true) { Request(); // 重置 safe = 1; flag = 0; for (int i = 0; i < num; i++) { Finish[i] = false; } } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)