Guest User

Untitled

a guest
Nov 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Process {
  4. int id;
  5. int size;
  6. int sizeLeft;
  7. int blockNum;
  8. bool flag;
  9. };
  10.  
  11. void enterMemory(int * & memory, int & size, int * & arr) {
  12. cout << "Enter the no. of memory blocks : ";
  13. cin >> size;
  14. memory = new int[size];
  15. arr = new int[size];
  16. cout << "Enter the size of each block :";
  17. for (int i = 0; i < size; i++) {
  18. cin >> memory[i];
  19. }
  20. }
  21.  
  22. void copyM(Process * & p, const int & num, int * & memory, int * &arr, int size) {
  23. for (int i = 0; i < size; i++) {
  24. arr[i] = memory [i];
  25. }
  26. for (int i = 0; i < num; i++) {
  27. p[i].flag = false;
  28. }
  29. }
  30.  
  31. void enterProcess(Process * & p, int & num) {
  32. cout << "Enter the no. of processes : ";
  33. cin >> num;
  34. p = new Process[num];
  35. cout << "Enter the size of each Process :";
  36. for (int i = 0; i < num; i++) {
  37. cin >> p[i].size;
  38. p[i].flag = false;
  39. p[i].id = i + 1;
  40. }
  41. }
  42.  
  43. void firstFit(int *arr, const int & size, Process * p, const int & num) {
  44. for (int i = 0; i < num; i++) {
  45. for (int j = 0; j < size; j++) {
  46. if (arr[j] >= p[i].size) {
  47. p[i].blockNum = j + 1;
  48. p[i].flag = true;
  49. p[i].sizeLeft = arr[j] = arr[j] - p[i].size;
  50. break;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. void bestFit(int *arr, const int & size, Process * p, const int & num) {
  57. cout << "\n\nBest fit\n";
  58. int min;
  59. for (int i = 0; i < num; i++) {
  60. min = -1;
  61. for (int j = 0; j < size; j++) {
  62. if (arr[j] >= p[i].size) {
  63. if (min == -1) {
  64. min = j;
  65. }
  66. else if ((min != -1) && (arr[j] < arr[min])) {
  67. min = j;
  68. }
  69. }
  70. }
  71. if (min != -1) {
  72. p[i].blockNum = min + 1;
  73. p[i].flag = true;
  74. p[i].sizeLeft = arr[min] = arr[min] - p[i].size;
  75. }
  76. }
  77. }
  78.  
  79. void worstFit(int *arr, const int & size, Process * p, const int & num) {
  80. cout << "\n\nWorst fit\n";
  81. int max;
  82. for (int i = 0; i < num; i++) {
  83. max = -1;
  84. for (int j = 0; j < size; j++) {
  85. if (arr[j] >= p[i].size) {
  86. if (max == -1) {
  87. max = j;
  88. }
  89. else if ((max != -1) && (arr[j] > arr[max])) {
  90. max = j;
  91. }
  92. }
  93. }
  94. if (max != -1) {
  95. p[i].blockNum = max + 1;
  96. p[i].flag = true;
  97. p[i].sizeLeft = arr[max] = arr[max] - p[i].size;
  98. }
  99. }
  100. }
  101.  
  102.  
  103. void display(Process * p, const int & num) {
  104. for (int i = 0; i < num; i++) {
  105. if (p[i].flag == true) {
  106. cout << "Process " << p[i].id << " of size " << p[i].size << " Assigned in Memory block " << p[i].blockNum << "-------- size left = " << p[i].sizeLeft << endl;
  107. }
  108. }
  109. for (int i = 0; i < num; i++) {
  110. if (p[i].flag == false) {
  111. cout << "Process " << p[i].id << " is Waiting " << endl;
  112. }
  113. }
  114. }
  115. int main() {
  116. int *memory;
  117. int *arr;
  118. int size;
  119. Process *p;
  120. int num;
  121.  
  122. enterMemory(memory, size, arr);
  123. enterProcess(p, num);
  124.  
  125. copyM(p, num, memory, arr, size);
  126. firstFit(arr, size, p, num);
  127. display(p, num);
  128.  
  129. copyM(p, num, memory, arr, size);
  130. bestFit(arr, size, p, num);
  131. display(p, num);
  132.  
  133. copyM(p, num, memory, arr, size);
  134. worstFit(arr, size, p, num);
  135. display(p, num);
  136. return 0;
  137. }
Add Comment
Please, Sign In to add comment