Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author : kaidul
- **/
- using namespace std;
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <bitset>
- #define M 100
- struct process {
- int processId, arrivalTime, burstTime, priority;
- int waitingTime, turnAroundTime, responseTime;
- bool operator < (const process& a) const {
- if(arrivalTime == a.arrivalTime) return priority > a.priority;
- return arrivalTime > a.arrivalTime;
- }
- } pArray[M];
- struct waitingProcess {
- int processId, arrivalTime, burstTime, priority;
- bool operator < (const waitingProcess& a) const {
- if(priority == a.priority) return burstTime > a.burstTime;
- return priority > a.priority;
- }
- } wp;
- priority_queue<process> q;
- priority_queue<waitingProcess> waitingList;
- bitset <M> taken;
- struct timeLine {
- int proc, start, end;
- } tmline[M];
- int nProcess, timeInterval, total, sum, averageTime;
- float throughtputTime;
- int main() {
- //freopen("in.txt", "r", stdin);
- cin >> nProcess;
- for (int i = 1; i <= nProcess ; i++) {
- pArray[i].processId = i;
- cin >> pArray[i].arrivalTime >> pArray[i].burstTime >> pArray[i].priority;
- q.push(pArray[i]);
- }
- cin >> timeInterval;
- cout << "Priority Sceduling Algorithm : \n";
- int i = 1;
- process next, top;
- waitingProcess waiting;
- tmline[0].start = 0, tmline[0].proc = 0, tmline[0].end = 0;
- cout << "\nExecution Sequence : \n";
- while (!waitingList.empty() || !q.empty()) { //untill there are any process left to be executed
- tmline[i].start = tmline[i-1].end;
- if(waitingList.empty() && !q.empty()) { // no processes to wait
- top = q.top();
- q.pop();
- if(!q.empty()) {
- waitingProcess p;
- next = q.top();
- if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
- if(next.priority < top.priority) {
- tmline[i].end = next.arrivalTime;
- tmline[i].proc = top.processId;
- int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
- p.processId = top.processId, p.arrivalTime = top.arrivalTime, p.priority = top.priority, p.burstTime = exeRemain;
- waitingList.push(p);
- } else {
- q.pop();
- q.push(top);
- p.processId = next.processId, p.arrivalTime = next.arrivalTime, p.priority = next.priority, p.burstTime = next.burstTime;
- waitingList.push(p);
- continue;
- }
- } else {
- tmline[i].end = tmline[i].start + top.burstTime;
- tmline[i].proc = top.processId;
- }
- } else {
- tmline[i].end = tmline[i].start + top.burstTime;
- tmline[i].proc = top.processId;
- }
- } else if(!waitingList.empty() && q.empty()) { // process queue is alredy empty, now we have to concern about the waiting list only :)
- waitingProcess wt = waitingList.top();
- tmline[i].end = tmline[i].start + wt.burstTime;
- tmline[i].proc = wt.processId;
- waitingList.pop();
- } else { // both wating list and process queue are not empty
- top = q.top();
- waiting = waitingList.top();
- if(top.priority < waiting.priority) {
- q.pop();
- if(!q.empty()) {
- waitingProcess p;
- next = q.top();
- if(next.arrivalTime < (top.burstTime + tmline[i-1].end)) {
- if(next.priority < top.priority) {
- tmline[i].end = next.arrivalTime;
- tmline[i].proc = top.processId;
- int exeRemain = (top.burstTime + tmline[i-1].end) - next.arrivalTime;
- p.processId = top.processId, p.arrivalTime = top.arrivalTime, p.priority = top.priority, p.burstTime = exeRemain;
- waitingList.push(p);
- } else {
- q.pop();
- q.push(top);
- p.processId = next.processId, p.arrivalTime = next.arrivalTime, p.priority = next.priority, p.burstTime = next.burstTime;
- waitingList.push(p);
- continue;
- }
- } else {
- tmline[i].end = tmline[i].start + top.burstTime;
- tmline[i].proc = top.processId;
- }
- } else {
- tmline[i].end = tmline[i].start + top.burstTime;
- tmline[i].proc = top.burstTime;
- }
- } else { //process of waiting list has less burst time than prpcess of queue, so it will be executed first
- q.pop();
- waitingList.pop();
- waitingProcess wt;
- wt.processId = top.processId ,wt.arrivalTime = top.arrivalTime, wt.burstTime = top.burstTime, wt.priority = top.priority;
- waitingList.push(wt);
- tmline[i].end = tmline[i].start + waiting.burstTime;
- tmline[i].proc = waiting.processId;
- }
- }
- cout << tmline[i].start << " " << tmline[i].end << " " << tmline[i].proc << "\n";
- i++;
- }
- cout << "\n";
- taken.reset();
- int range = i -1;
- // calulating response time
- for (int i = 1; i <= range; i++) {
- if(taken.test(tmline[i].proc)) continue;
- pArray[tmline[i].proc].responseTime = tmline[i].start - pArray[tmline[i].proc].arrivalTime;
- taken.set(tmline[i].proc, 1);
- }
- // Calculating Turn-Around Time
- taken.reset();
- for (int i = range; i >= 1; i--) {
- if(taken.test(tmline[i].proc)) continue;
- pArray[tmline[i].proc].turnAroundTime = tmline[i].end - pArray[tmline[i].proc].arrivalTime;
- taken.set(tmline[i].proc, 1);
- }
- // Calculating Waiting Time
- /**
- key: turn-around time = execution(burst) time + waiting time
- **/
- int total = 0;
- for (int i = 1; i <= nProcess; i++ ) {
- pArray[i].waitingTime = pArray[i].turnAroundTime - pArray[i].burstTime;
- cout << pArray[i].processId << "\n";
- cout << "Waiting Time : " << pArray[i].waitingTime << "\n";
- total += pArray[i].waitingTime;
- cout << "TurnAround Time : " << pArray[i].turnAroundTime << "\n";
- cout << "Response Time : " << pArray[i].responseTime << "\n\n";
- }
- averageTime = total / nProcess;
- cout << "Average waiting Time : " << averageTime << "\n";
- throughtputTime = (float)nProcess / tmline[range].end;
- cout << "Throughput Time : " << throughtputTime << "\n";
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement