c语言——银行家算法

	学习完操作系统的课程后,我迫不及待地进行了银行家算法的编写。其中银行家算法的编写只需3步;话不多说,直接进入代码分析。完整代码如下:
#include
#includeusing namespace std;#define num_of_resource 3
#define num_of_binary 5
class OS {
private://banker algorithm's datasint Request[num_of_resource] = { 0, 0,0 };int binary;//inherent datasint Max[num_of_binary][num_of_resource];int Available[num_of_resource];int Need[num_of_binary][num_of_resource];int Allocation[num_of_binary][num_of_resource];//security check process's datasbool Finish[num_of_binary] = { false,false,false,false };int Work[num_of_binary];bool bl(int* a,int* b,int n) {int i;for (i = 0; i < n; i++) {if (a[i] <= b[i]) continue;else return false;}return true;}bool banker_calc() {assert((binary < num_of_binary) && (binary > 0));if (!bl(Request, Need[binary], num_of_resource)) return false;if (bl(Request, Available, num_of_resource)) return false;for (int i = 0; i < num_of_resource; i++) {Available[i] -= Request[i];Allocation[binary][i] += Request[i];Need[binary][i] -= Request[i];}return security_check();}bool security_check() {for (int a = 0; a < num_of_resource; a++) {Work[a] = Available[a];}for (int i = 0; i < num_of_binary; i++) {if ((Finish[i] == false) && bl(Need[i], Work, num_of_resource)) {Finish[i] = true;for (int j = 0; j < num_of_resource; j++) {Work[j] += Allocation[i][j];}i = 0;}else return false;}return true;}
public:OS() {binary = 0;cout<<"OS_bin has been created!"<> Max[i][j];}}cout << "Please iutput the Allocation Matrix: "<> Allocation[i][j];}}cout << "Please inout the Available array: "<> Available[i];}for (int i = 0; i < num_of_binary; i++) {for (int j = 0; j < num_of_resource; j++) {Need[i][j] = Max[i][j] - Allocation[i][j];}}}void ResourcesApplication() {cout << "Plesase input the identifier of the binary: "<> binary;cout << endl;cout << "Please input the number of resources applicated one by one: "<> Request[i];}}bool processApplication() {if (!security_check()) {cout << "Try another assignmeng of resources!"<


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部