SJF - preemptive


SUBMITTED BY: Guest

DATE: Nov. 12, 2013, 2:49 a.m.

FORMAT: Text only

SIZE: 6.5 kB

HITS: 935

  1. /**
  2. * author : kaidul
  3. **/
  4. using namespace std;
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <queue>
  8. #include <bitset>
  9. #define M 100
  10. struct process {
  11. int processId, arrivalTime, burstTime, priority;
  12. int waitingTime, turnAroundTime, responseTime;
  13. bool operator < (const process& a) const {
  14. if(arrivalTime == a.arrivalTime) return burstTime > a.burstTime;
  15. return arrivalTime > a.arrivalTime;
  16. }
  17. } pArray[M];
  18. struct waitingProcess {
  19. int processId, arrivalTime, burstTime, priority;
  20. bool operator < (const waitingProcess& a) const {
  21. if(burstTime == a.burstTime) return arrivalTime > a.arrivalTime;
  22. return burstTime > a.burstTime;
  23. }
  24. } wp;
  25. priority_queue<process> q;
  26. priority_queue<waitingProcess> waitingList;
  27. bitset <M> taken;
  28. struct timeLine {
  29. int proc, start, end;
  30. } tmline[M];
  31. int nProcess, timeInterval, averageTime;
  32. float throughtputTime;
  33. int main() {
  34. freopen("input.txt", "r", stdin);
  35. cin >> nProcess;
  36. for (int i = 1; i <= nProcess ; i++) {
  37. pArray[i].processId = i;
  38. cin >> pArray[i].arrivalTime >> pArray[i].burstTime >> pArray[i].priority;
  39. q.push(pArray[i]);
  40. }
  41. cin >> timeInterval;
  42. cout << "Shortest Job First - Preemptive : \n";
  43. int i = 1;
  44. process next, top;
  45. waitingProcess waiting;
  46. tmline[0].start = 0, tmline[0].proc = 0, tmline[0].end = 0;
  47. cout << "\nExecution Sequence : \n";
  48. while (!waitingList.empty() || !q.empty()) {
  49. tmline[i].start = tmline[i-1].end;
  50. if(waitingList.empty() && !q.empty()) {
  51. top = q.top();
  52. q.pop();
  53. if(!q.empty()) {
  54. waitingProcess p;
  55. next = q.top();
  56. if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
  57. tmline[i].end = next.arrivalTime;
  58. tmline[i].proc = top.processId;
  59. int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
  60. p.processId = top.processId, p.arrivalTime = top.arrivalTime, p.priority = top.priority, p.burstTime = exeRemain;
  61. waitingList.push(p);
  62. } else {
  63. tmline[i].end = tmline[i].start + top.burstTime;
  64. tmline[i].proc = top.processId;
  65. }
  66. } else {
  67. tmline[i].end = tmline[i].start + top.burstTime;
  68. tmline[i].proc = top.processId;
  69. }
  70. } else if(!waitingList.empty() && q.empty()) { // queue khali, waiting list khali na - sobtheke soja case
  71. waitingProcess wt = waitingList.top();
  72. tmline[i].end = tmline[i].start + wt.burstTime;
  73. tmline[i].proc = wt.processId;
  74. waitingList.pop();
  75. } else { // both queue have processes
  76. top = q.top();
  77. waiting = waitingList.top();
  78. if(top.burstTime < waiting.burstTime) {
  79. q.pop();
  80. if(!q.empty()) {
  81. waitingProcess wt;
  82. next = q.top();
  83. if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
  84. tmline[i].end = next.arrivalTime;
  85. tmline[i].proc = top.processId;
  86. int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
  87. wt.processId = top.processId, wt.arrivalTime = top.arrivalTime, wt.priority = top.priority, wt.burstTime = exeRemain;
  88. waitingList.push(wt);
  89. } else {
  90. tmline[i].end = tmline[i].start + top.burstTime;
  91. tmline[i].proc = top.processId;
  92. }
  93. } else {
  94. tmline[i].end = tmline[i].start + top.burstTime;
  95. tmline[i].proc = top.burstTime;
  96. }
  97. } else { // waitinglist and queue 2ta tei process ase, waiting er tar burst time kom, so se age execute hobe
  98. q.pop();
  99. waitingList.pop();
  100. waitingProcess wt;
  101. wt.processId = top.processId ,wt.arrivalTime = top.arrivalTime, wt.burstTime = top.burstTime, wt.priority = top.priority;
  102. waitingList.push(wt);
  103. tmline[i].end = tmline[i].start + waiting.burstTime;
  104. tmline[i].proc = waiting.processId;
  105. }
  106. }
  107. cout << tmline[i].start << " " << tmline[i].end << " " << tmline[i].proc << "\n";
  108. i++;
  109. }
  110. cout << "\n";
  111. taken.reset();
  112. int range = i -1;
  113. // calulating response time
  114. for (int i = 1; i <= range; i++) {
  115. if(taken.test(tmline[i].proc)) continue;
  116. pArray[tmline[i].proc].responseTime = tmline[i].start - pArray[tmline[i].proc].arrivalTime;
  117. taken.set(tmline[i].proc, 1);
  118. }
  119. // Calculating Turn-Around Time
  120. taken.reset();
  121. for (int i = range; i >= 1; i--) {
  122. if(taken.test(tmline[i].proc)) continue;
  123. pArray[tmline[i].proc].turnAroundTime = tmline[i].end - pArray[tmline[i].proc].arrivalTime;
  124. taken.set(tmline[i].proc, 1);
  125. }
  126. // Calculating Waiting Time
  127. /**
  128. key: turn-around time = execution(burst) time + waiting time
  129. **/
  130. int total = 0;
  131. for (int i = 1; i <= nProcess; i++ ) {
  132. pArray[i].waitingTime = pArray[i].turnAroundTime - pArray[i].burstTime;
  133. cout << pArray[i].processId << "\n";
  134. cout << "Waiting Time : " << pArray[i].waitingTime << "\n";
  135. total += pArray[i].waitingTime;
  136. cout << "TurnAround Time : " << pArray[i].turnAroundTime << "\n";
  137. cout << "Response Time : " << pArray[i].responseTime << "\n\n";
  138. }
  139. averageTime = total / nProcess;
  140. cout << "Average waiting Time : " << averageTime << "\n";
  141. throughtputTime = (float)nProcess / tmline[range].end;
  142. cout << "Throughput Time : " << throughtputTime << "\n";
  143. return false;
  144. }

comments powered by Disqus