
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ] = { }; //系统->拥有资源 ][] = { }; //进程->共需要 ][] = { }; //进程->已得到 ][] = { }; //进程->还需要 ] = { }; //模拟->系统->拥有资源 //int Request[100] = { 0 }; //进程->请求资源 0.0貌似后来没用到 ] = { }; //资源名称。界面使用 ] = { }; //存放安全序列 ; //进程的最大数为 ; //资源的最大数为 void showdata(); //显示资源矩阵 int safe(); //安全性算法 int main()//主函数 { , m, n, flag; char ming; //------------------------------------------------------------->>>>>>>> 【界面】 >>> cout << "\t*-----------------------------------------------------*" << endl; cout << "\t|| ||" << endl; cout << "\t|| 银行家算法实现 ||" << endl; cout << "\t|| ||" << endl; cout << "\t|| 张金棒 ||" << endl; cout << "\t|| ||" << endl; cout << "\t|| 2016.06.29 ||" << endl; cout << "\t|| ||" << endl; cout << "\t*-----------------------------------------------------*" << endl; //------------------------------------------------------------->>>>>>>> 【初始化】 >>> cout << "请先输入系统可供资源种类的数量:"; cin >> n; N = n; ; i < n; i++) { cout << << "的名称:"; cin >> ming; name[i] = ming; cout << "资源的数量:"; cin >> number; Avaliable[i] = number; } cout << endl; cout << "请输入作业的数量"; cin >> m; M = m; cout << "请输入各进程的最大需求量(" << m << "*" << n << "矩阵)[Max]:" << endl; ; i < m; i++) ; j < n; j++) cin >> Max[i][j]; do { flag = ; cout << "请输入各进程已经申请的资源量(" << m << "*" << n << "矩阵)[Allocation]:" << endl; ; i < m; i++) ; j < n; j++) { cin >> Allocation[i][j]; ; Need[i][j] = Max[i][j] - Allocation[i][j]; Avaliable[j] = Avaliable[j] - Allocation[i][j]; } if (flag) cout << "申请的资源大于最大需求值,请重新输入!\n"; } while (flag); showdata();//显示各种资源 safe();//用银行家算法判定系统是否安全 system("pause"); ; } //------------------------------------------------------------->>>>>>>> 【界面·显示资源矩阵】 >>> void showdata() { int i, j; cout << "系统当前可用资源[Avaliable]:" << endl; ; i < N; i++) cout << name[i] << " "; cout << endl; ; j < N; j++) cout << Avaliable[j]<<" "; cout << endl; cout << " Max Allocation Need" << endl; cout << "process "; ; j < ; j++) { ; i < N; i++) cout << name[i] << " "; cout << " "; } cout << endl; ; i < M; i++) { cout << " " << i << " "; ; j < N; j++) cout << Max[i][j] << " "; cout << " "; ; j < N; j++) cout << Allocation[i][j] << " "; cout << " "; ; j < N; j++) cout << Need[i][j] << " "; cout << endl; } } //------------------------------------------------------------->>>>>>>> 【银行家算法】 >>> int safe() { , m, apply, Finish[] = { }; //apply [应用,使用] int j; ; i < N; i++) { Work[i] = Avaliable[i]; } ; i < M; i++)//------------------------------------->>>>>>>>>>选定进程 i { apply = ; ; j < N; j++)//--------------------------------->>>>>>>>>>选定资源 j { if (Finish[i] == False&&Need[i][j] <= Work[j])//------>>>>>>>>>>第i个进程j类所需资源小于系统拥有的资源 { apply++; if (apply == N)//--------------------------------->>>>>>>>>>当i进程各类资源都满足后 { ; m < N; m++) Work[m] = Work[m] + Allocation[i][m];//--->>>>>>>>>>分配其资源使其运行结束然后回收资源 Finish[i] = True;//--------------------------->>>>>>>>>>i进程执行完毕 temp[k] = i;//-------------------------------->>>>>>>>>>写入进程号到序列中 i = -;//------------------------------------->>>>>>>>>>检查前面的是否有可满足的 k++;//---------------------------------------->>>>>>>>>>准备好写下一个安全序列项 } } } } ; i < M; i++) { if (Finish[i] == False) { cout << "系统不安全" << endl;//不成功 系统不安全 ; } } cout << "系统是安全的!" << endl;//如果安全,输出成功 cout << "分配的序列:"; ; i < M; i++) //输出运行进程的数组 { cout << temp[i]; ) cout << "->"; } cout << endl; ; }