Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include < iostream >
- #include < cstdlib >
- #include < iomanip >
- #include < fstream >
- #include < vector >
- struct resource {
- int A, B, C;
- resource(): A(0), B(0), C(0) {};
- };
- class Banker {
- private: int num;
- std: : vector < struct resource > Allocation, Max, Need;
- public: Banker() {};
- Banker(int n): num(n) {};
- void AddData(int data[][6]);
- void SafeTest(struct resource Available);
- void SafeTest(struct resource Available, int p, struct resource Request);
- bool Safe(struct resource Available, struct resource Need);
- };
- void Banker::AddData(int data[][6]) {
- Allocation.resize(num);
- Max.resize(num);
- Need.resize(num);
- int j = 0;
- for (int i = 0; i <= num; i++) {
- Allocation[i].A = data[i][j];
- Allocation[i].B = data[i][++j];
- Allocation[i].C = data[i][++j];
- Max[i].A = data[i][++j];
- Max[i].B = data[i][++j];
- Max[i].C = data[i][++j];
- j = 0;
- }
- for (int i = 0; i < num; i++) {
- Need[i].A = Max[i].A - Allocation[i].A;
- Need[i].B = Max[i].B - Allocation[i].B;
- Need[i].C = Max[i].C - Allocation[i].C;
- }
- }
- bool Banker::Safe(struct resource Available, struct resource Need) {
- if (Need.A <= Available.A && Need.B <= Available.B && Need.C <= Available.C) return true;
- else return false;
- }
- void Banker::SafeTest(struct resource Available) {
- bool test = true; // To test whether jump the loop or not
- bool Finish[num];
- int count = 0; // record the number of the safe process
- for (int i = 0; i < num; i++) Finish[i] = false;
- std: : vector < int > SafeSequence;
- while (test) {
- test = false;
- for (int i = 0; i < num; i++) {
- // at
- std: : vector < int > SafeSequence;
- if (Safe(Available, Need[p]) == true) {
- Available.A -= Request.A;
- Available.B -= Request.B;
- Available.C -= Request.C;
- Need[p].A -= Request.A;
- Need[p].B -= Request.B;
- Need[p].C -= Request.C;
- Allocation[p].A += Request.A;
- Allocation[p].B += Request.B;
- Allocation[p].C += Request.C;
- if (Need[p].A == 0 && Need[p].B == 0 && Need[p].C == 0) {
- Finish[p] = true;
- count++;
- SafeSequence.push_back(p);
- }
- } else {
- std: : cout << "Rejected " << std::endl;
- return;
- }
- while (test) {
- test = false;
- for (int i = 0; i < num; i++) {
- // at most do the numbers of
- Process times
- if (Finish[i] == false && Safe(Available, Need[i]) == true) {
- Available.A += Allocation[i].A;
- Available.B += Allocation[i].B;
- Available.C += Allocation[i].C;
- Finish[i] = true;
- test = true;
- count++;
- SafeSequence.push_back(i);
- }
- }
- }
- if (count == num) {
- std: : cout << " Granted " << std::endl << " SafeSequence : ";
- for (int i = 0; i < num; i++) std: : cout << "P" << SafeSequence[i] << std::setw(3);
- }
- else std::cout << "Rejected " << std::endl;
- }
- main() {
- std: : fstream file("resource.txt", std::ios:: in );
- int data[10][6],
- i = 0,
- j = 0;
- if (!file) {
- std: : cout << "File not found . " << std::endl;
- exit(EXIT_FAILURE);
- }
- else {
- int a, b, c;
- while (file >> a >> b >> c) {
- data[i][j] = a;
- data[i][++j] = b;
- data[i][++j] = c;
- ++j %= 6;
- if (j == 0) i++;
- }
- file.close();
- }
- Banker Process(i);
- Process.AddData(data);
- struct resource Available;
- std::cout << " Input the Available resources : ";
- std::cin >> Available.A >> Available.B >> Available.C;
- std::cout << std::endl;
- char ch;
- std::cout << "Assign the process to request for resources? (y/n) ";
- std::cin >> ch;
- struct resource Request;
- switch (ch) {
- case 'y':
- std::cout << std::endl << "Input the assigned process : ";
- int p;
- std: : cin >> p;
- std: : cout << std::endl << "Input the resources to be
- requested: ";
- std::cin >> Request.A >> Request.B >> Request.C;
- std: : cout << std::endl;
- Process.SafeTest(Available, p, Request);
- break;
- case 'n':
- Process.SafeTest(Available);
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement