Advertisement
Kaidul

priority schedule

Jan 29th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. /**
  2.  * author : kaidul
  3. **/
  4. using namespace std;
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <queue>
  8. #include <bitset>
  9.  
  10. #define M 100
  11.  
  12. struct process {
  13.     int processId, arrivalTime, burstTime, priority;
  14.     int waitingTime, turnAroundTime, responseTime;
  15.     bool operator < (const process& a) const {
  16.         if(arrivalTime == a.arrivalTime) return priority > a.priority;
  17.         return arrivalTime > a.arrivalTime;
  18.     }
  19. } pArray[M];
  20.  
  21. struct waitingProcess {
  22.     int processId, arrivalTime, burstTime, priority;
  23.     bool operator < (const waitingProcess& a) const {
  24.         if(priority == a.priority) return burstTime > a.burstTime;
  25.         return priority > a.priority;
  26.     }
  27. } wp;
  28.  
  29. priority_queue<process> q;
  30. priority_queue<waitingProcess> waitingList;
  31. bitset <M> taken;
  32.  
  33. struct timeLine {
  34.     int proc, start, end;
  35. } tmline[M];
  36.  
  37. int nProcess, timeInterval, total, sum, averageTime;
  38. float throughtputTime;
  39.  
  40.  
  41. int main() {
  42.     //freopen("in.txt", "r", stdin);
  43.     cin >> nProcess;
  44.  
  45.     for (int i = 1; i <= nProcess ; i++) {
  46.         pArray[i].processId = i;
  47.         cin >> pArray[i].arrivalTime >> pArray[i].burstTime >> pArray[i].priority;
  48.         q.push(pArray[i]);
  49.     }
  50.     cin >> timeInterval;
  51.  
  52.     cout << "Priority Sceduling Algorithm : \n";
  53.  
  54.     int i = 1;
  55.     process next, top;
  56.  
  57.     waitingProcess waiting;
  58.     tmline[0].start = 0, tmline[0].proc = 0, tmline[0].end = 0;
  59.  
  60.     cout << "\nExecution Sequence : \n";
  61.     while (!waitingList.empty() || !q.empty()) { //untill there are any process left to be executed
  62.         tmline[i].start = tmline[i-1].end;
  63.         if(waitingList.empty() && !q.empty()) { // no processes to wait
  64.             top = q.top();
  65.             q.pop();
  66.             if(!q.empty()) {
  67.                 waitingProcess p;
  68.                 next = q.top();
  69.                 if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
  70.                     if(next.priority < top.priority) {
  71.                         tmline[i].end = next.arrivalTime;
  72.                         tmline[i].proc = top.processId;
  73.                         int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
  74.                         p.processId = top.processId, p.arrivalTime = top.arrivalTime, p.priority = top.priority, p.burstTime = exeRemain;
  75.                         waitingList.push(p);
  76.                     } else {
  77.                         q.pop();
  78.                         q.push(top);
  79.                         p.processId = next.processId, p.arrivalTime = next.arrivalTime, p.priority = next.priority, p.burstTime = next.burstTime;
  80.                         waitingList.push(p);
  81.                         continue;
  82.                     }
  83.                 } else {
  84.                     tmline[i].end = tmline[i].start + top.burstTime;
  85.                     tmline[i].proc = top.processId;
  86.                 }
  87.  
  88.             } else {
  89.                 tmline[i].end = tmline[i].start + top.burstTime;
  90.                 tmline[i].proc = top.processId;
  91.             }
  92.         } else if(!waitingList.empty() && q.empty()) { // process queue is alredy empty, now we have to concern about the waiting list only :)
  93.             waitingProcess wt = waitingList.top();
  94.             tmline[i].end = tmline[i].start + wt.burstTime;
  95.             tmline[i].proc = wt.processId;
  96.             waitingList.pop();
  97.  
  98.         } else { // both wating list and process queue are not empty
  99.             top = q.top();
  100.             waiting = waitingList.top();
  101.             if(top.priority < waiting.priority) {
  102.                 q.pop();
  103.                 if(!q.empty()) {
  104.                     waitingProcess p;
  105.                     next = q.top();
  106.                     if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
  107.                         if(next.priority < top.priority) {
  108.                             tmline[i].end = next.arrivalTime;
  109.                             tmline[i].proc = top.processId;
  110.                             int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
  111.                             p.processId = top.processId, p.arrivalTime = top.arrivalTime, p.priority = top.priority, p.burstTime = exeRemain;
  112.                             waitingList.push(p);
  113.                         } else {
  114.                             q.pop();
  115.                             q.push(top);
  116.                             p.processId = next.processId, p.arrivalTime = next.arrivalTime, p.priority = next.priority, p.burstTime = next.burstTime;
  117.                             waitingList.push(p);
  118.                             continue;
  119.                         }
  120.                     } else {
  121.                         tmline[i].end = tmline[i].start + top.burstTime;
  122.                         tmline[i].proc = top.processId;
  123.                     }
  124.  
  125.                 } else {
  126.                     tmline[i].end = tmline[i].start + top.burstTime;
  127.                     tmline[i].proc = top.burstTime;
  128.                 }
  129.             } else { //process of waiting list has less burst time than prpcess of queue, so it will be executed first
  130.                 q.pop();
  131.                 waitingList.pop();
  132.                 waitingProcess wt;
  133.                 wt.processId = top.processId ,wt.arrivalTime = top.arrivalTime, wt.burstTime = top.burstTime, wt.priority = top.priority;
  134.                 waitingList.push(wt);
  135.                 tmline[i].end = tmline[i].start + waiting.burstTime;
  136.                 tmline[i].proc = waiting.processId;
  137.             }
  138.  
  139.         }
  140.         cout << tmline[i].start << " " << tmline[i].end << " " << tmline[i].proc << "\n";
  141.         i++;
  142.     }
  143.  
  144.     cout << "\n";
  145.  
  146.     taken.reset();
  147.     int range = i -1;
  148.  
  149.     // calulating response time
  150.     for (int i = 1; i <= range; i++) {
  151.         if(taken.test(tmline[i].proc)) continue;
  152.         pArray[tmline[i].proc].responseTime = tmline[i].start - pArray[tmline[i].proc].arrivalTime;
  153.         taken.set(tmline[i].proc, 1);
  154.     }
  155.  
  156.     // Calculating Turn-Around Time
  157.     taken.reset();
  158.     for (int i = range; i >= 1; i--) {
  159.         if(taken.test(tmline[i].proc)) continue;
  160.         pArray[tmline[i].proc].turnAroundTime = tmline[i].end - pArray[tmline[i].proc].arrivalTime;
  161.         taken.set(tmline[i].proc, 1);
  162.     }
  163.  
  164.     // Calculating Waiting Time
  165.     /**
  166.       key: turn-around time = execution(burst) time + waiting time
  167.     **/
  168.     int total = 0;
  169.     for (int i = 1; i <= nProcess; i++ ) {
  170.         pArray[i].waitingTime = pArray[i].turnAroundTime - pArray[i].burstTime;
  171.         cout << pArray[i].processId << "\n";
  172.         cout << "Waiting Time : " << pArray[i].waitingTime << "\n";
  173.         total += pArray[i].waitingTime;
  174.         cout << "TurnAround Time : " << pArray[i].turnAroundTime << "\n";
  175.         cout << "Response Time : " << pArray[i].responseTime << "\n\n";
  176.     }
  177.  
  178.     averageTime = total / nProcess;
  179.     cout << "Average waiting Time : " << averageTime << "\n";
  180.  
  181.     throughtputTime = (float)nProcess / tmline[range].end;
  182.     cout << "Throughput Time : " << throughtputTime << "\n";
  183.  
  184.     return false;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement