Guest User

Untitled

a guest
Jun 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #ifndef SCHEDULING_H_
  2. #define SCHEDULING_H_
  3.  
  4. #include <vector>
  5.  
  6. typedef unsigned int uint;
  7.  
  8. class Scheduling
  9. {
  10. uint currActiveProcessID;
  11. uint timeCounter = 0;
  12.  
  13. std::vector<uint> arrivalTime;
  14. //When process start to execute
  15. std::vector<uint> burstTime;
  16. //process wait to execute after they have arrived
  17. std::vector<uint> waitingTime;
  18.  
  19. public:
  20. Scheduling(uint);
  21. Scheduling() = default;
  22. Scheduling(const Scheduling&) = delete;
  23. Scheduling &operator=(const Scheduling&) = delete;
  24. Scheduling(Scheduling&&) = delete;
  25. Scheduling &operator=(Scheduling&&) = delete;
  26. ~Scheduling() = default;
  27.  
  28. void calcWaitingTime();
  29. void printInfo();
  30. };
  31.  
  32. #endif
  33.  
  34. #include <iostream>
  35. #include <array>
  36. #include <vector>
  37. #include <algorithm> // std::find
  38. #include <iterator> // std::begin, std::end
  39. #include <limits> //std::numeric_limits
  40. #include "scheduling.h"
  41.  
  42. typedef unsigned int uint;
  43.  
  44.  
  45. Scheduling::Scheduling(uint n)
  46. {
  47. for ( int i = 0; i < n; i++)
  48. {
  49. arrivalTime.reserve(n);
  50. burstTime.reserve(n);
  51. waitingTime.reserve(n);
  52.  
  53. uint val;
  54. std::cout << "Enter arrival time for process " << i+1 << "n";
  55. std::cin >> val;
  56. arrivalTime.push_back(val);
  57.  
  58. std::cout << "Enter burst time for process " << i+1 << "n";
  59. std::cin >> val;
  60. burstTime.push_back(val);
  61.  
  62. waitingTime.push_back(0);
  63. }
  64. }
  65.  
  66. bool isAllZeroes(std::vector<uint>& burstTimeCopy)
  67. {
  68. for (int i = 0; i < burstTimeCopy.size(); i++)
  69. {
  70. if (burstTimeCopy[i] != 0)
  71. {
  72. return false;
  73. }
  74. else
  75. {
  76. return true;
  77. }
  78. }
  79. }
  80.  
  81. void Scheduling::calcWaitingTime()
  82. {
  83. std::vector<uint> burstTimeCopy;
  84. burstTimeCopy.reserve(burstTime.size());
  85. uint index = 0;
  86. std::vector<uint>::iterator it;
  87. while (isAllZeroes(burstTimeCopy) == false)
  88. {
  89. auto max = std::max_element(arrivalTime.begin(), arrivalTime.end());
  90. if (timeCounter <= *max)
  91. {
  92. it = arrivalTime.end();
  93. it = std::find(arrivalTime.begin(), arrivalTime.end(), timeCounter);
  94. if (it != arrivalTime.end())
  95. {
  96. index = std::distance(arrivalTime.begin(), it);
  97. }
  98.  
  99. if (burstTimeCopy.empty() == true || index != currActiveProcessID)
  100. {
  101. burstTimeCopy.push_back(burstTime[index]);
  102. }
  103.  
  104. uint minBurstTime = std::numeric_limits<uint>::max();
  105. //Find index with minimum burst Time remaining
  106. for (int i = 0; i < burstTimeCopy.size(); i++)
  107. {
  108. if (burstTimeCopy[i] != 0 && burstTimeCopy[i] < minBurstTime)
  109. {
  110. minBurstTime = burstTimeCopy[i];
  111. index = i;
  112. }
  113. }
  114. currActiveProcessID = index;
  115. burstTimeCopy[currActiveProcessID] -= 1;
  116.  
  117. for (int i = 0; i < arrivalTime.size(); i++)
  118. {
  119. if (timeCounter >= arrivalTime[i] && i != currActiveProcessID
  120. && burstTimeCopy[i] != 0)
  121. {
  122. waitingTime[i] += 1;
  123. }
  124. }
  125. timeCounter++;
  126. }
  127. else
  128. {
  129. uint minBurstTime = std::numeric_limits<uint>::max();
  130. //Find index with minimum burst Time remaining
  131. for (int i = 0; i < burstTimeCopy.size(); i++)
  132. {
  133. if (burstTimeCopy[i] != 0 && burstTimeCopy[i] < minBurstTime)
  134. {
  135. minBurstTime = burstTimeCopy[i];
  136. index = i;
  137. }
  138. }
  139. currActiveProcessID = index;
  140. burstTimeCopy[index] -= minBurstTime;
  141.  
  142. for (int i = 0; i < arrivalTime.size(); i++)
  143. {
  144. if (i != currActiveProcessID && burstTimeCopy[i] != 0)
  145. {
  146. waitingTime[i] = waitingTime[i] + minBurstTime;
  147. }
  148. }
  149. timeCounter += minBurstTime;
  150. }
  151. }
  152. }
  153.  
  154. void Scheduling::printInfo()
  155. {
  156. std::cout << "ProcessIDtArrival TimetBurst TimetWaiting Timen";
  157. for (int i = 0; i < arrivalTime.size(); i++)
  158. {
  159. std::cout << i+1 << "tt" << arrivalTime[i] << "tt" << burstTime[i];
  160. std::cout << "tt" << waitingTime[i] << "n";
  161. }
  162. }
  163.  
  164. int main()
  165. {
  166. int num;
  167. std::cout << "Enter the number of processesn";
  168. std::cin >> num;
  169. Scheduling shortestJobFirst(num);
  170. shortestJobFirst.calcWaitingTime();
  171. shortestJobFirst.printInfo();
  172. }
Add Comment
Please, Sign In to add comment