标签:
1 #include <iostream> 2 #include<string> 3 #define False 0 4 #define True 1 5 6 using namespace std; 7 8 9 int Avaliable[100] = { 0 }; //系统->拥有资源 10 int Max[100][100] = { 0 }; //进程->共需要 11 int Allocation[100][100] = { 0 }; //进程->已得到 12 int Need[100][100] = { 0 }; //进程->还需要 13 14 int Work[100] = { 0 }; //模拟->系统->拥有资源 15 //int Request[100] = { 0 }; //进程->请求资源 0.0貌似后来没用到 16 17 18 char name[100] = { 0 }; //资源名称。界面使用 19 20 int temp[100] = { 0 }; //存放安全序列 21 22 23 int M = 100; //进程的最大数为 24 int N = 100; //资源的最大数为 25 26 27 28 void showdata(); //显示资源矩阵 29 int safe(); //安全性算法 30 31 32 33 34 35 int main()//主函数 36 { 37 int i, j, number, choice = -1, m, n, flag; 38 char ming; 39 40 //------------------------------------------------------------->>>>>>>> 【界面】 >>> 41 cout << "\t*-----------------------------------------------------*" << endl; 42 cout << "\t|| ||" << endl; 43 cout << "\t|| 银行家算法实现 ||" << endl; 44 cout << "\t|| ||" << endl; 45 cout << "\t|| 张金棒 ||" << endl; 46 cout << "\t|| ||" << endl; 47 cout << "\t|| 2016.06.29 ||" << endl; 48 cout << "\t|| ||" << endl; 49 cout << "\t*-----------------------------------------------------*" << endl; 50 51 52 53 //------------------------------------------------------------->>>>>>>> 【初始化】 >>> 54 cout << "请先输入系统可供资源种类的数量:"; 55 cin >> n; 56 N = n; 57 58 for (i = 0; i < n; i++) 59 { 60 cout << "资源" << i + 1 << "的名称:"; 61 cin >> ming; 62 name[i] = ming; 63 cout << "资源的数量:"; 64 cin >> number; 65 Avaliable[i] = number; 66 } 67 cout << endl; 68 cout << "请输入作业的数量"; 69 cin >> m; 70 M = m; 71 cout << "请输入各进程的最大需求量(" << m << "*" << n << "矩阵)[Max]:" << endl; 72 for (i = 0; i < m; i++) 73 for (j = 0; j < n; j++) 74 cin >> Max[i][j]; 75 do 76 { 77 flag = 0; 78 cout << "请输入各进程已经申请的资源量(" << m << "*" << n << "矩阵)[Allocation]:" << endl; 79 for (i = 0; i < m; i++) 80 for (j = 0; j < n; j++) 81 { 82 cin >> Allocation[i][j]; 83 if (Allocation[i][j]>Max[i][j]) flag = 1; 84 Need[i][j] = Max[i][j] - Allocation[i][j]; 85 Avaliable[j] = Avaliable[j] - Allocation[i][j]; 86 } 87 if (flag) 88 cout << "申请的资源大于最大需求值,请重新输入!\n"; 89 } while (flag); 90 91 92 93 94 95 showdata();//显示各种资源 96 97 98 99 safe();//用银行家算法判定系统是否安全 100 system("pause"); 101 return 0; 102 } 103 104 105 //------------------------------------------------------------->>>>>>>> 【界面·显示资源矩阵】 >>> 106 void showdata() 107 { 108 int i, j; 109 cout << "系统当前可用资源[Avaliable]:" << endl; 110 for (i = 0; i < N; i++) 111 cout << name[i] << " "; 112 cout << endl; 113 for (j = 0; j < N; j++) 114 cout << Avaliable[j]<<" "; 115 cout << endl; 116 cout << " Max Allocation Need" << endl; 117 cout << "process "; 118 for (j = 0; j < 3; j++) 119 { 120 for (i = 0; i < N; i++) 121 cout << name[i] << " "; 122 cout << " "; 123 } 124 cout << endl; 125 for (i = 0; i < M; i++) 126 { 127 cout << " " << i << " "; 128 for (j = 0; j < N; j++) 129 cout << Max[i][j] << " "; 130 cout << " "; 131 for (j = 0; j < N; j++) 132 cout << Allocation[i][j] << " "; 133 cout << " "; 134 for (j = 0; j < N; j++) 135 cout << Need[i][j] << " "; 136 cout << endl; 137 } 138 } 139 //------------------------------------------------------------->>>>>>>> 【银行家算法】 >>> 140 int safe() 141 { 142 int i, k = 0, m, apply, Finish[100] = { 0 }; //apply [应用,使用] 143 int j; 144 for (i = 0; i < N; i++) 145 { 146 Work[i] = Avaliable[i]; 147 } 148 for (i = 0; i < M; i++)//------------------------------------->>>>>>>>>>选定进程 i 149 { 150 apply = 0; 151 for (j = 0; j < N; j++)//--------------------------------->>>>>>>>>>选定资源 j 152 { 153 if (Finish[i] == False&&Need[i][j] <= Work[j])//------>>>>>>>>>>第i个进程j类所需资源小于系统拥有的资源 154 { 155 apply++; 156 if (apply == N)//--------------------------------->>>>>>>>>>当i进程各类资源都满足后 157 { 158 for (m = 0; m < N; m++) 159 Work[m] = Work[m] + Allocation[i][m];//--->>>>>>>>>>分配其资源使其运行结束然后回收资源 160 Finish[i] = True;//--------------------------->>>>>>>>>>i进程执行完毕 161 temp[k] = i;//-------------------------------->>>>>>>>>>写入进程号到序列中 162 i = -1;//------------------------------------->>>>>>>>>>检查前面的是否有可满足的 163 k++;//---------------------------------------->>>>>>>>>>准备好写下一个安全序列项 164 } 165 } 166 } 167 } 168 for (i = 0; i < M; i++) 169 { 170 if (Finish[i] == False) 171 { 172 cout << "系统不安全" << endl;//不成功 系统不安全 173 return -1; 174 } 175 } 176 cout << "系统是安全的!" << endl;//如果安全,输出成功 177 cout << "分配的序列:"; 178 for (i = 0; i < M; i++) //输出运行进程的数组 179 { 180 cout << temp[i]; 181 if (i < M - 1) cout << "->"; 182 } 183 cout << endl; 184 return 0; 185 }
标签:
原文地址:http://www.cnblogs.com/jin521/p/5628714.html