77 lines
3.1 KiB
C++
77 lines
3.1 KiB
C++
// Round Robin (RR) CPU scheduling - preemptive, with arrival times and a Gantt chart.
|
|
// Input : n, then n lines of "arrival burst", then the time quantum Q.
|
|
// Rule : a process that arrives while another is running joins the ready queue BEFORE
|
|
// the preempted process is put back at the tail (the usual textbook convention).
|
|
#include <algorithm>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <queue>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
struct Process {
|
|
int pid, at, bt, rem;
|
|
int ct = 0, tat = 0, wt = 0; // completion, turnaround, waiting
|
|
};
|
|
struct Slice { int pid, start, end; }; // pid = 0 means CPU idle
|
|
|
|
int main() {
|
|
int n, q;
|
|
cin >> n;
|
|
vector<Process> p(n);
|
|
for (int i = 0; i < n; i++) { p[i].pid = i + 1; cin >> p[i].at >> p[i].bt; p[i].rem = p[i].bt; }
|
|
cin >> q;
|
|
|
|
vector<int> order(n); // process indexes sorted by arrival, then pid
|
|
for (int i = 0; i < n; i++) order[i] = i;
|
|
stable_sort(order.begin(), order.end(), [&](int a, int b) { return p[a].at < p[b].at; });
|
|
|
|
queue<int> ready;
|
|
vector<Slice> gantt;
|
|
int time = 0, next = 0, finished = 0;
|
|
auto admit = [&]() { while (next < n && p[order[next]].at <= time) ready.push(order[next++]); };
|
|
|
|
while (finished < n) {
|
|
admit();
|
|
if (ready.empty()) { // nobody has arrived yet: CPU idles
|
|
int t = p[order[next]].at;
|
|
gantt.push_back({0, time, t});
|
|
time = t;
|
|
continue;
|
|
}
|
|
int i = ready.front(); ready.pop();
|
|
int run = min(q, p[i].rem);
|
|
if (!gantt.empty() && gantt.back().pid == p[i].pid && gantt.back().end == time)
|
|
gantt.back().end += run; // same process continues: extend its box
|
|
else
|
|
gantt.push_back({p[i].pid, time, time + run});
|
|
time += run;
|
|
p[i].rem -= run;
|
|
admit(); // arrivals during the slice go in first...
|
|
if (p[i].rem > 0) ready.push(i); // ...then the preempted process goes to the tail
|
|
else { p[i].ct = time; p[i].tat = p[i].ct - p[i].at; p[i].wt = p[i].tat - p[i].bt; finished++; }
|
|
}
|
|
|
|
cout << "Round Robin, time quantum = " << q << "\n\n";
|
|
cout << "PID\tAT\tBT\tCT\tTAT\tWT\n";
|
|
double sw = 0, st = 0;
|
|
for (auto& x : p) {
|
|
cout << "P" << x.pid << "\t" << x.at << "\t" << x.bt << "\t" << x.ct << "\t" << x.tat << "\t" << x.wt << "\n";
|
|
sw += x.wt; st += x.tat;
|
|
}
|
|
cout << fixed << setprecision(2);
|
|
cout << "\nAverage waiting time : " << sw / n << "\n";
|
|
cout << "Average turnaround time : " << st / n << "\n\nGantt chart:\n";
|
|
string top, nums;
|
|
for (auto& s : gantt) {
|
|
string label = (s.pid ? "P" + to_string(s.pid) : string("idle"));
|
|
if (nums.size() < top.size()) nums += string(top.size() - nums.size(), ' ');
|
|
nums += to_string(s.start);
|
|
top += "| " + label + " ";
|
|
}
|
|
top += "|";
|
|
if (nums.size() < top.size() - 1) nums += string(top.size() - 1 - nums.size(), ' ');
|
|
nums += to_string(gantt.back().end);
|
|
cout << top << "\n" << nums << "\n";
|
|
}
|