操作系统模拟之银行家算法。
文件共4份,其中1份cpp,3份.h,代码如下:
main.cpp
#include <iostream>
#include <stdlib.h>
#include "initialize.h"
#include "check.h"
#include "apply.h"
using namespace std;int main()
{Process_initialize();Check();Apply();cout << endl << "The program ended.Thanks for using." << endl;return 0;
}/*测试数据
5
3
0 1 0
7 5 3
2 0 0
3 2 2
3 0 2
9 0 2
2 1 1
2 2 2
0 0 2
4 3 3
3 3 2
*/
initialize.h
#include <vector>
using namespace std;
class process{private:int Resource_number;vector<int> Allocation;vector<int> Need;vector<int> Max;bool if_checked;public:process(int Resource_number);void Initialize();vector<int> get_Allocation();vector<int> get_Need();vector<int> get_Max();bool get_if_checked();void set_Allocation(vector<int> Allocation);void set_Need(vector<int> Need);void set_Max(vector<int> Max);void set_if_checked(bool if_checked);
};
vector<process> v;
vector<int> available;process::process(int Resource_number)
{this -> Resource_number = Resource_number;this -> if_checked = false;
}void process::Initialize()
{int temp_allocation, temp_need, temp_max;cout << "Please input the Allocation information:" << endl;for(int i = 0; i < Resource_number; i++){cin >> temp_allocation;Allocation.push_back(temp_allocation);}cout << "Please input the Max information:" << endl;for(int i = 0; i < Resource_number; i++){cin >> temp_max;Max.push_back(temp_max);}for(int i = 0; i < Resource_number; i++){Need.push_back(Max[i] - Allocation[i]);}
}void Process_initialize()
{int process_number, resource_number, temp_available;cout << "Please input the process number:" << endl;cin >> process_number;cout << "Please input the resource number:" << endl;cin >> resource_number;for(int i = 0; i < process_number; i++){cout << endl << "The process you initialize now is P" << i << endl;process temp_process(resource_number);temp_process.Initialize();v.push_back(temp_process);}cout << "Please input the available resources:" << endl;for(int i = 0; i < resource_number; i++){cin >> temp_available;available.push_back(temp_available);}cout << "Initialization successfully!" << endl;
}//设置成员变量的get函数
vector<int> process::get_Allocation()
{return Allocation;
}
vector<int> process::get_Need()
{return Need;
}
vector<int> process::get_Max()
{return Max;
}
bool process::get_if_checked()
{return if_checked;
}//设置成员变量的set函数
void process::set_Allocation(vector<int> Allocation)
{this -> Allocation = Allocation;
}
void process::set_Need(vector<int> Need)
{this -> Need = Need;
}
void process::set_Max(vector<int> Max)
{this -> Max = Max;
}
void process::set_if_checked(bool if_checked)
{this -> if_checked = if_checked;
}
check.h
#include <iostream>
#include <vector>
using namespace std;
class check{private:vector<int> temp_check;vector<process> check_process;vector<int> temp_available;public:check(vector<process> v, vector<int> available);void Check_security();
};check::check(vector<process> v, vector<int> available)
{check_process = v; temp_available = available;
}void check::Check_security()
{cout << "Now the program will check the security of processes." << endl;for(int i = 0; i < check_process.size(); i++){for(int j = 0; j < check_process.size(); j++){if(check_process[j].get_if_checked() == false){bool check_resource = true;vector<int> temp = check_process[j].get_Need();for(int k = 0; k < temp.size(); k++){if(temp[k] > temp_available[k])check_resource = false;}if(check_resource == true){check_process[j].set_if_checked(true);for(int k = 0; k < temp.size(); k++)temp_available[k] += temp[k];temp_check.push_back(j);}}}}if(temp_check.size() == check_process.size()){system("cls");cout << "There exists a secure sequence." << endl;cout << "The result may not be unique." << endl;cout << "The following sequence is the smallest in numerical ascending order:" << endl << endl;for(int i = 0; i < temp_check.size(); i++){if(i > 0)cout << " -> ";cout << "P" << temp_check[i];}cout << endl;}elsecout << "There dose not exist a secure sequence" << endl;
}void Check()
{check temp_check(v, available);temp_check.Check_security();
}
apply.h
#include <vector>
using namespace std;class apply{private:vector<int> temp_apply; //拟申请的资源 vector<process> apply_process; //已有的进程 vector<int> temp_available; //剩余的资源数量 bool if_true;public:apply(); //进程申请资源 void apply_resource();void update_data(); //更新进程数据 bool get_if_true();void set_if_true(bool if_true);
};apply::apply()
{apply_process = v;temp_available = available;if_true = false;
}bool apply::get_if_true()
{return if_true;
}
void apply::set_if_true(bool if_true)
{this -> if_true = if_true;
}void apply::apply_resource()
{ apply(); int id, temp_resource;cout << endl << "Please input the process id that you want to apply:" << endl;cin >> id;cout << endl << "Please input the resourse that you want to apply:" << endl;bool need_flag = true, avi_flag = true;vector<int> _temp_need, _temp_allocation;for(int i = 0; i < apply_process[id].get_Need().size(); i++){cin >> temp_resource;if(temp_resource > apply_process[id].get_Need()[i])need_flag = false;if(temp_resource > temp_available[i])avi_flag = false;vector<int> _temp_need, _temp_allocation;_temp_need.push_back(apply_process[id].get_Need()[i] - temp_resource);_temp_allocation.push_back(apply_process[id].get_Allocation()[i] + temp_resource);temp_available[i] -= temp_resource;}if(need_flag == false || avi_flag == false){if(need_flag == false)cout << endl << "ERROR! Exceed the resources it needs!" << endl;else if(avi_flag == false)cout << endl << "ERROR! Exceed the total available resources" << endl;cout << "Sorry, please input again." << endl;apply();return;}apply_process[id].set_Need(_temp_need);apply_process[id].set_Allocation(_temp_allocation);check temp_check(apply_process, temp_available);temp_check.Check_security();cout << endl << "Do you want to continue? Input yes or no." << endl;string choice;cin >> choice;if(choice == "no")if_true = true;
}
void Apply()
{apply a;while(a.get_if_true() == false)a.apply_resource();
}