Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1. void SJFSolution2()
  2. {
  3. ///Shortest Job First with preemption, mainly depends on CPU burst time
  4. ///variables
  5. int finishT[n];
  6. int actualArrivalT[n];
  7. int CPUrunT=0;
  8. int CPUidleT=0;
  9. int numProDone=0;
  10. int countV=0;
  11. int countN[n]={0};//used for counting the actual arrival time, to calculate the response time of each process
  12.  
  13. ///make a copy of process pro, array
  14. for(unsigned int i=0;i<n;i++)
  15. {
  16. tempPro[i].processID=pro[i].processID;
  17. tempPro[i].arrivalT=pro[i].arrivalT;
  18. tempPro[i].CPUburstT=pro[i].CPUburstT;
  19. tempPro[i].ppriority=pro[i].ppriority;
  20. }
  21. while(numProDone<n)//checking if all the processes have done, if not, increment by 1.
  22. {
  23. ///Ready Queue Processes
  24. for(unsigned int i=0;i<n;i++)//get all the possible processes in ready queue, not include any process that has the CPUburstT of 0 or the remainingT of 0
  25. {
  26. if ((tempPro[i].arrivalT<=CPUrunT)&&(tempPro[i].CPUburstT>0))
  27. {
  28. tempProReadyQ.push_back(process());//creating a copy of ready queue processes, vector
  29. tempProReadyQ[countV].processID=tempPro[i].processID;
  30. tempProReadyQ[countV].arrivalT=tempPro[i].arrivalT;
  31. tempProReadyQ[countV].CPUburstT=tempPro[i].CPUburstT;
  32. tempProReadyQ[countV].ppriority=tempPro[i].ppriority;
  33. countV++;
  34. }
  35. }
  36. countV=0;
  37. if(tempProReadyQ.empty())//if ready queue is empty and no process is brought in, then it means the CPU is idle
  38. {
  39. CPUidleT++;
  40. CPUrunT++;//anything before this count from 0, anything else below this count from 1
  41. }
  42. else if (!tempProReadyQ.empty())//else ready queue has processes
  43. {
  44. if (tempProReadyQ.size()==1)//if there is only one process in the ready queue
  45. {
  46. tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
  47. countN[tempProReadyQ[0].processID-1]++;
  48. if(countN[tempProReadyQ[0].processID-1]==1)
  49. {
  50. actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
  51. }
  52. if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
  53. {
  54. finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
  55. numProDone++;
  56. }
  57. CPUrunT++;
  58. }
  59. else//there are more than one process in the ready queue
  60. {
  61. int minEle=INT_MAX;
  62. int tempProcessIndex=0;
  63. for(unsigned i=0; i<tempProReadyQ.size();i++)
  64. {
  65. if(tempProReadyQ[i].CPUburstT<minEle)
  66. {
  67. minEle=tempProReadyQ[i].CPUburstT;//smallest process ID
  68. tempProcessIndex=i;
  69. }
  70. }
  71. //sort(tempProReadyQ.begin(),tempProReadyQ.end(),checkbtvector);//sort the process based on their CPU burst time, smallest CPUburst time is the first element
  72. for(unsigned int i=0;i<tempProReadyQ.size();i++)//to collect the processes ID that has the same shortest CPUburstT
  73. {
  74. if(tempProReadyQ[i].CPUburstT==minEle)
  75. //if(tempProReadyQ[i].CPUburstT==tempProReadyQ[0].CPUburstT)//check if there are more than one process that has the same shortest CPUburstT
  76. {
  77. tempMin.push_back(tempProReadyQ[i].processID);//it stores process IDs
  78. }
  79. }
  80.  
  81. if(!tempMin.empty())//there are processes with the same CPUburstT
  82. {
  83. int minEle=INT_MAX;
  84. for(unsigned i=0; i<tempMin.size();i++)
  85. {
  86. if(tempMin[i]<minEle)
  87. {
  88. minEle=tempMin[i];//smallest process ID
  89. }
  90. }
  91. tempPro[minEle-1].CPUburstT--;
  92. countN[minEle-1]++;
  93. if(countN[minEle-1]==1)
  94. {
  95. actualArrivalT[minEle-1]=CPUrunT;//only needs the 1st actual arrival time for each process
  96. }
  97. if(tempPro[minEle-1].CPUburstT==0)
  98. {
  99. finishT[minEle-1]=CPUrunT+1;
  100. numProDone++;
  101. //tempProReadyQ.erase(tempProReadyQ.begin()+(tempMin[0]-1));
  102. }
  103.  
  104. CPUrunT++;
  105. }
  106. else if(tempMin.empty())//no processes with the same CPUburstT
  107. {
  108. tempPro[tempProcessIndex].CPUburstT--;
  109. countN[tempProcessIndex]++;
  110. if(countN[tempProcessIndex]==1)
  111. {
  112. actualArrivalT[tempProcessIndex]=CPUrunT;//only needs the 1st actual arrival time for each process
  113. }
  114. if(tempPro[tempProcessIndex].CPUburstT==0)
  115. {
  116. finishT[tempProcessIndex]=CPUrunT+1;
  117. numProDone++;
  118. }
  119.  
  120. CPUrunT++;
  121. }
  122. tempMin.clear();
  123. }
  124. }
  125. tempProReadyQ.clear();//used it when it vector
  126. //while(!tempProReadyQ.empty())
  127. //{
  128. // tempProReadyQ.pop_back();
  129. //}
  130. tempProReadyQ.shrink_to_fit();
  131. }
  132.  
  133. void SJFSolution3()
  134. {
  135. ///Shortest Job First with preemption, mainly depends on CPU burst time
  136. ///variables
  137. int finishT[n];
  138. int actualArrivalT[n];
  139. int CPUrunT=0;
  140. int CPUidleT=0;
  141. int numProDone=0;
  142. int countV=0;
  143. int countN[n]={0};//used for counting the actual arrival time, to calculate the response time of each process
  144.  
  145. ///make a copy of process pro, array
  146. for(unsigned int i=0;i<n;i++)
  147. {
  148. tempPro[i].processID=pro[i].processID;
  149. tempPro[i].arrivalT=pro[i].arrivalT;
  150. tempPro[i].CPUburstT=pro[i].CPUburstT;
  151. tempPro[i].ppriority=pro[i].ppriority;
  152. }
  153. while(numProDone<n)//checking if all the processes have done, if not, increment by 1.
  154. {
  155. ///Ready Queue Processes
  156. for(unsigned int i=0;i<n;i++)//get all the possible processes in ready queue, not include any process that has the CPUburstT of 0 or the remainingT of 0
  157. {
  158. if ((tempPro[i].arrivalT<=CPUrunT)&&(tempPro[i].CPUburstT>0))
  159. {
  160. tempProReadyQ.push_back(process());//creating a copy of ready queue processes, vector
  161. tempProReadyQ[countV].processID=tempPro[i].processID;
  162. tempProReadyQ[countV].arrivalT=tempPro[i].arrivalT;
  163. tempProReadyQ[countV].CPUburstT=tempPro[i].CPUburstT;
  164. tempProReadyQ[countV].ppriority=tempPro[i].ppriority;
  165. countV++;
  166. }
  167. }
  168. countV=0;
  169. if(tempProReadyQ.empty())//if ready queue is empty and no process is brought in, then it means the CPU is idle
  170. {
  171. CPUidleT++;
  172. CPUrunT++;//anything before this count from 0, anything else below this count from 1
  173. }
  174. else if (!tempProReadyQ.empty())//else ready queue has processes
  175. {
  176. if (tempProReadyQ.size()==1)//if there is only one process in the ready queue
  177. {
  178. tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
  179. countN[tempProReadyQ[0].processID-1]++;
  180. if(countN[tempProReadyQ[0].processID-1]==1)
  181. {
  182. actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
  183. }
  184. if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
  185. {
  186. finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
  187. numProDone++;
  188. }
  189. CPUrunT++;
  190. }
  191. else//there are more than one process in the ready queue
  192. {
  193. int minEle=INT_MAX;
  194. int tempProcessIndex=0;
  195. for(unsigned i=0; i<tempProReadyQ.size();i++)
  196. {
  197. if(tempProReadyQ[i].CPUburstT<minEle)
  198. {
  199. minEle=tempProReadyQ[i].CPUburstT;//smallest process ID
  200. tempProcessIndex=i;
  201. }
  202. }
  203. sort(tempProReadyQ.begin(),tempProReadyQ.end(),checkbtvector);//sort the process based on their CPU burst time, smallest CPUburst time is the first element
  204. for(unsigned int i=0;i<tempProReadyQ.size();i++)//to collect the processes ID that has the same shortest CPUburstT
  205. {
  206.  
  207. if(tempProReadyQ[i].CPUburstT==tempProReadyQ[0].CPUburstT)//check if there are more than one process that has the same shortest CPUburstT
  208. {
  209. tempMin.push_back(tempProReadyQ[i].processID);//it stores process IDs
  210. }
  211. }
  212.  
  213. if(!tempMin.empty())//there are processes with the same CPUburstT
  214. {
  215.  
  216. sort(tempMin.begin(),tempMin.end(),findMin);//find the smallest process ID
  217. tempPro[tempMin[0]-1].CPUburstT--;
  218. countN[tempMin[0]-1]++;
  219. if(countN[tempMin[0]-1]==1)
  220. {
  221. actualArrivalT[tempMin[0]-1]=CPUrunT;//only needs the 1st actual arrival time for each process
  222. }
  223. if(tempPro[tempMin[0]-1].CPUburstT==0)
  224. {
  225. finishT[tempMin[0]-1]=CPUrunT+1;
  226. numProDone++;
  227. }
  228. CPUrunT++;
  229. }
  230. else if(tempMin.empty())//no processes with the same CPUburstT
  231. {
  232. tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
  233. countN[tempProReadyQ[0].processID-1]++;
  234. if(countN[tempProReadyQ[0].processID-1]==1)
  235. {
  236. actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
  237. }
  238. if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
  239. {
  240. finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
  241. numProDone++;
  242. }
  243. CPUrunT++;
  244. }
  245. tempMin.clear();
  246. }
  247. }
  248. tempProReadyQ.clear();//used it when it vector
  249. tempProReadyQ.shrink_to_fit();
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement