Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include < iostream >
  2. #include < cstdlib >
  3. #include < iomanip >
  4. #include < fstream >
  5. #include < vector >
  6.  
  7. struct resource {
  8. int A, B, C;
  9. resource(): A(0), B(0), C(0) {};
  10. };
  11. class Banker {
  12. private: int num;
  13. std: : vector < struct resource > Allocation, Max, Need;
  14. public: Banker() {};
  15. Banker(int n): num(n) {};
  16. void AddData(int data[][6]);
  17. void SafeTest(struct resource Available);
  18. void SafeTest(struct resource Available, int p, struct resource Request);
  19. bool Safe(struct resource Available, struct resource Need);
  20. };
  21. void Banker::AddData(int data[][6]) {
  22. Allocation.resize(num);
  23. Max.resize(num);
  24. Need.resize(num);
  25. int j = 0;
  26. for (int i = 0; i <= num; i++) {
  27. Allocation[i].A = data[i][j];
  28. Allocation[i].B = data[i][++j];
  29. Allocation[i].C = data[i][++j];
  30. Max[i].A = data[i][++j];
  31. Max[i].B = data[i][++j];
  32. Max[i].C = data[i][++j];
  33. j = 0;
  34. }
  35. for (int i = 0; i < num; i++) {
  36. Need[i].A = Max[i].A - Allocation[i].A;
  37. Need[i].B = Max[i].B - Allocation[i].B;
  38. Need[i].C = Max[i].C - Allocation[i].C;
  39. }
  40. }
  41. bool Banker::Safe(struct resource Available, struct resource Need) {
  42. if (Need.A <= Available.A && Need.B <= Available.B && Need.C <= Available.C) return true;
  43. else return false;
  44. }
  45. void Banker::SafeTest(struct resource Available) {
  46. bool test = true; // To test whether jump the loop or not
  47. bool Finish[num];
  48. int count = 0; // record the number of the safe process
  49. for (int i = 0; i < num; i++) Finish[i] = false;
  50. std: : vector < int > SafeSequence;
  51. while (test) {
  52. test = false;
  53. for (int i = 0; i < num; i++) {
  54. // at
  55. std: : vector < int > SafeSequence;
  56. if (Safe(Available, Need[p]) == true) {
  57. Available.A -= Request.A;
  58. Available.B -= Request.B;
  59. Available.C -= Request.C;
  60. Need[p].A -= Request.A;
  61. Need[p].B -= Request.B;
  62. Need[p].C -= Request.C;
  63. Allocation[p].A += Request.A;
  64. Allocation[p].B += Request.B;
  65. Allocation[p].C += Request.C;
  66. if (Need[p].A == 0 && Need[p].B == 0 && Need[p].C == 0) {
  67. Finish[p] = true;
  68. count++;
  69. SafeSequence.push_back(p);
  70. }
  71. } else {
  72. std: : cout << "Rejected " << std::endl;
  73. return;
  74. }
  75. while (test) {
  76. test = false;
  77. for (int i = 0; i < num; i++) {
  78. // at most do the numbers of
  79. Process times
  80. if (Finish[i] == false && Safe(Available, Need[i]) == true) {
  81. Available.A += Allocation[i].A;
  82. Available.B += Allocation[i].B;
  83. Available.C += Allocation[i].C;
  84. Finish[i] = true;
  85. test = true;
  86. count++;
  87. SafeSequence.push_back(i);
  88. }
  89. }
  90. }
  91. if (count == num) {
  92. std: : cout << " Granted " << std::endl << " SafeSequence : ";
  93. for (int i = 0; i < num; i++) std: : cout << "P" << SafeSequence[i] << std::setw(3);
  94. }
  95. else std::cout << "Rejected " << std::endl;
  96. }
  97. main() {
  98. std: : fstream file("resource.txt", std::ios:: in );
  99. int data[10][6],
  100. i = 0,
  101. j = 0;
  102. if (!file) {
  103. std: : cout << "File not found . " << std::endl;
  104. exit(EXIT_FAILURE);
  105. }
  106. else {
  107. int a, b, c;
  108. while (file >> a >> b >> c) {
  109. data[i][j] = a;
  110. data[i][++j] = b;
  111. data[i][++j] = c;
  112. ++j %= 6;
  113. if (j == 0) i++;
  114. }
  115. file.close();
  116. }
  117. Banker Process(i);
  118. Process.AddData(data);
  119. struct resource Available;
  120. std::cout << " Input the Available resources : ";
  121. std::cin >> Available.A >> Available.B >> Available.C;
  122. std::cout << std::endl;
  123. char ch;
  124. std::cout << "Assign the process to request for resources? (y/n) ";
  125. std::cin >> ch;
  126. struct resource Request;
  127. switch (ch) {
  128. case 'y':
  129. std::cout << std::endl << "Input the assigned process : ";
  130. int p;
  131. std: : cin >> p;
  132. std: : cout << std::endl << "Input the resources to be
  133. requested: ";
  134. std::cin >> Request.A >> Request.B >> Request.C;
  135. std: : cout << std::endl;
  136. Process.SafeTest(Available, p, Request);
  137. break;
  138. case 'n':
  139. Process.SafeTest(Available);
  140. break;
  141. }
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement