Add os/round-robin: Round Robin scheduler in C++ and C with Gantt chart, quantum sweep and captured output
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
build/
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Round Robin scheduling (C++ and C)
|
||||||
|
|
||||||
|
Companion code for [Round Robin Scheduling in C and C++](https://ankurm.com/implementing-round-robin-scheduling-algorithm-in-c/).
|
||||||
|
The full explanation, dry run and viva questions live in that post.
|
||||||
|
|
||||||
|
scripts/run-all.sh # builds rr.cpp, rr.c and the legacy program, regenerates output/
|
||||||
|
|
||||||
|
* `rr.cpp` / `rr.c` — same algorithm, same output; input is `n`, then `arrival burst` per process, then the quantum
|
||||||
|
* `legacy/old_rr_tick_loop.cpp` — the earlier tick-loop version, kept as published (correct only when every arrival is 0)
|
||||||
|
* `input/` — sample workloads, `output/` — captured results quoted in the post
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
3
|
||||||
|
0 24
|
||||||
|
0 3
|
||||||
|
0 3
|
||||||
|
4
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
3
|
||||||
|
0 4
|
||||||
|
10 3
|
||||||
|
11 2
|
||||||
|
3
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
4
|
||||||
|
0 8
|
||||||
|
0 8
|
||||||
|
0 8
|
||||||
|
0 8
|
||||||
|
100
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
5
|
||||||
|
0 10
|
||||||
|
0 29
|
||||||
|
0 3
|
||||||
|
0 7
|
||||||
|
0 12
|
||||||
|
10
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
5
|
||||||
|
10
|
||||||
|
29
|
||||||
|
3
|
||||||
|
7
|
||||||
|
12
|
||||||
|
10
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
3
|
||||||
|
0 6
|
||||||
|
0 4
|
||||||
|
0 2
|
||||||
|
1
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
4
|
||||||
|
0 5
|
||||||
|
1 4
|
||||||
|
2 2
|
||||||
|
4 1
|
||||||
|
2
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
// ============================================================
|
||||||
|
// Round Robin CPU Scheduling Algorithm in C++
|
||||||
|
// Preemptive: each process gets at most Q time units per round
|
||||||
|
// ============================================================
|
||||||
|
// LEGACY: the earlier version published on ankurm.com, kept for comparison (this header comment is the only addition).
|
||||||
|
// Its waiting times are right only when every process arrives at time 0.
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Holds all attributes of a single process for Round Robin simulation
|
||||||
|
struct Process {
|
||||||
|
int processNo; // Process identifier (P1, P2, ...)
|
||||||
|
int totalBurstTime; // Original burst time (kept for final output and TT)
|
||||||
|
int remainingBurstTime; // Burst time still left to execute (decremented each tick)
|
||||||
|
int waitingTime; // Accumulated waiting time across all rounds
|
||||||
|
int turnaroundTime; // Total time from start to completion
|
||||||
|
int quantumRoundCount; // How many full time-quanta this process has already used
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
Process processes[99];
|
||||||
|
int processCount;
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
// Step 1: Read number of processes and their burst times
|
||||||
|
// -------------------------------------------------------
|
||||||
|
cout << "\n Enter No of Processes: ";
|
||||||
|
cin >> processCount;
|
||||||
|
|
||||||
|
for (int i = 0; i < processCount; i++) {
|
||||||
|
cout << "\n Enter Burst Time of Process " << i + 1 << ": ";
|
||||||
|
cin >> processes[i].totalBurstTime;
|
||||||
|
|
||||||
|
processes[i].remainingBurstTime = processes[i].totalBurstTime; // Copy for simulation
|
||||||
|
processes[i].waitingTime = 0;
|
||||||
|
processes[i].turnaroundTime = 0;
|
||||||
|
processes[i].quantumRoundCount = 0;
|
||||||
|
processes[i].processNo = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int timeQuantum;
|
||||||
|
cout << "\n Enter Time Quantum: ";
|
||||||
|
cin >> timeQuantum;
|
||||||
|
|
||||||
|
// Display entered data
|
||||||
|
cout << "\n\n Entered Data";
|
||||||
|
cout << "\n ProcesstBurst Time";
|
||||||
|
for (int i = 0; i < processCount; i++) {
|
||||||
|
cout << "\n " << processes[i].processNo
|
||||||
|
<< "\t\t" << processes[i].totalBurstTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
// Step 2: Compute total simulation time (sum of all burst times)
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int totalSimTime = 0;
|
||||||
|
for (int i = 0; i < processCount; i++) totalSimTime += processes[i].totalBurstTime;
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
// Step 3: Round Robin Simulation
|
||||||
|
// currentProc = index of process currently on CPU
|
||||||
|
// quantumTick = how many time units used in current quantum slot
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int currentProc = 0;
|
||||||
|
int quantumTick = 0;
|
||||||
|
|
||||||
|
for (int tick = 0; tick < totalSimTime; tick++) {
|
||||||
|
|
||||||
|
// When a process begins a new round (quantum slot starts),
|
||||||
|
// record the current time as the start of its wait period
|
||||||
|
if (quantumTick == 0 && processes[currentProc].remainingBurstTime != 0) {
|
||||||
|
processes[currentProc].waitingTime = tick;
|
||||||
|
// Subtract all time this process has already spent executing in previous rounds
|
||||||
|
if (processes[currentProc].quantumRoundCount != 0) {
|
||||||
|
processes[currentProc].waitingTime -=
|
||||||
|
timeQuantum * processes[currentProc].quantumRoundCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (processes[currentProc].remainingBurstTime != 0 && quantumTick != timeQuantum) {
|
||||||
|
// Process still has work to do and quantum not yet exhausted: run for 1 tick
|
||||||
|
processes[currentProc].remainingBurstTime--;
|
||||||
|
quantumTick++;
|
||||||
|
} else {
|
||||||
|
// Either quantum exhausted OR process finished — switch to next process
|
||||||
|
if (quantumTick == timeQuantum && processes[currentProc].remainingBurstTime != 0) {
|
||||||
|
// Process used a full quantum but is not finished yet
|
||||||
|
processes[currentProc].quantumRoundCount++;
|
||||||
|
}
|
||||||
|
// Move to next process in circular order
|
||||||
|
currentProc = (currentProc + 1) % processCount;
|
||||||
|
quantumTick = 0;
|
||||||
|
tick--; // Redo this tick with the newly selected process
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------
|
||||||
|
// Step 4: Compute Turnaround Time and display results
|
||||||
|
// -------------------------------------------------------
|
||||||
|
int totalWaiting = 0, totalTurnaround = 0;
|
||||||
|
|
||||||
|
cout << "\n\n Result of Round Robin Scheduling";
|
||||||
|
cout << "\n PNo\tBT\tWT\tTT";
|
||||||
|
|
||||||
|
for (int i = 0; i < processCount; i++) {
|
||||||
|
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].totalBurstTime;
|
||||||
|
totalWaiting += processes[i].waitingTime;
|
||||||
|
totalTurnaround += processes[i].turnaroundTime;
|
||||||
|
|
||||||
|
cout << "\n " << processes[i].processNo
|
||||||
|
<< "\t" << processes[i].totalBurstTime
|
||||||
|
<< "\t" << processes[i].waitingTime
|
||||||
|
<< "\t" << processes[i].turnaroundTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "\n Average Waiting Time : " << (float)totalWaiting / processCount;
|
||||||
|
cout << "\n Average Turnaround Time : " << (float)totalTurnaround / processCount;
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
Round Robin, time quantum = 10
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 10 10 10 0
|
||||||
|
P2 0 29 61 61 32
|
||||||
|
P3 0 3 23 23 20
|
||||||
|
P4 0 7 30 30 23
|
||||||
|
P5 0 12 52 52 40
|
||||||
|
|
||||||
|
Average waiting time : 23.00
|
||||||
|
Average turnaround time : 35.20
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | P2 | P3 | P4 | P5 | P2 | P5 | P2 |
|
||||||
|
0 10 20 23 30 40 50 52 61
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
Round Robin, time quantum = 4
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 24 30 30 6
|
||||||
|
P2 0 3 7 7 4
|
||||||
|
P3 0 3 10 10 7
|
||||||
|
|
||||||
|
Average waiting time : 5.67
|
||||||
|
Average turnaround time : 15.67
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | P2 | P3 | P1 |
|
||||||
|
0 4 7 10 30
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
Round Robin, time quantum = 2
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 5 12 12 7
|
||||||
|
P2 1 4 11 10 6
|
||||||
|
P3 2 2 6 4 2
|
||||||
|
P4 4 1 9 5 4
|
||||||
|
|
||||||
|
Average waiting time : 4.75
|
||||||
|
Average turnaround time : 7.75
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | P2 | P3 | P1 | P4 | P2 | P1 |
|
||||||
|
0 2 4 6 8 9 11 12
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
Round Robin, time quantum = 3
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 4 4 4 0
|
||||||
|
P2 10 3 13 3 0
|
||||||
|
P3 11 2 15 4 2
|
||||||
|
|
||||||
|
Average waiting time : 0.67
|
||||||
|
Average turnaround time : 3.67
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | idle | P2 | P3 |
|
||||||
|
0 4 10 13 15
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
Round Robin, time quantum = 1
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 6 12 12 6
|
||||||
|
P2 0 4 10 10 6
|
||||||
|
P3 0 2 6 6 4
|
||||||
|
|
||||||
|
Average waiting time : 5.33
|
||||||
|
Average turnaround time : 9.33
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | P2 | P3 | P1 | P2 | P3 | P1 | P2 | P1 | P2 | P1 |
|
||||||
|
0 1 2 3 4 5 6 7 8 9 10 12
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
Round Robin, time quantum = 100
|
||||||
|
|
||||||
|
PID AT BT CT TAT WT
|
||||||
|
P1 0 8 8 8 0
|
||||||
|
P2 0 8 16 16 8
|
||||||
|
P3 0 8 24 24 16
|
||||||
|
P4 0 8 32 32 24
|
||||||
|
|
||||||
|
Average waiting time : 12.00
|
||||||
|
Average turnaround time : 20.00
|
||||||
|
|
||||||
|
Gantt chart:
|
||||||
|
| P1 | P2 | P3 | P4 |
|
||||||
|
0 8 16 24 32
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
Enter No of Processes:
|
||||||
|
Enter Burst Time of Process 1:
|
||||||
|
Enter Burst Time of Process 2:
|
||||||
|
Enter Burst Time of Process 3:
|
||||||
|
Enter Burst Time of Process 4:
|
||||||
|
Enter Burst Time of Process 5:
|
||||||
|
Enter Time Quantum:
|
||||||
|
|
||||||
|
Entered Data
|
||||||
|
ProcesstBurst Time
|
||||||
|
1 10
|
||||||
|
2 29
|
||||||
|
3 3
|
||||||
|
4 7
|
||||||
|
5 12
|
||||||
|
|
||||||
|
Result of Round Robin Scheduling
|
||||||
|
PNo BT WT TT
|
||||||
|
1 10 0 10
|
||||||
|
2 29 32 61
|
||||||
|
3 3 20 23
|
||||||
|
4 7 23 30
|
||||||
|
5 12 40 52
|
||||||
|
Average Waiting Time : 23
|
||||||
|
Average Turnaround Time : 35.2
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
Q avg-WT avg-TAT gantt-boxes
|
||||||
|
1 25.00 37.20 45
|
||||||
|
2 25.40 37.60 24
|
||||||
|
3 25.00 37.20 17
|
||||||
|
5 24.60 36.80 12
|
||||||
|
7 24.20 36.40 9
|
||||||
|
10 23.00 35.20 8
|
||||||
|
12 22.20 34.40 6
|
||||||
|
15 24.00 36.20 6
|
||||||
|
20 27.00 39.20 6
|
||||||
|
29 28.00 40.20 5
|
||||||
|
40 28.00 40.20 5
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
identical input/classic-q4.txt
|
||||||
|
identical input/idle-gap-q3.txt
|
||||||
|
identical input/large-quantum.txt
|
||||||
|
identical input/legacy-5proc-q10.txt
|
||||||
|
identical input/quantum-1.txt
|
||||||
|
identical input/staggered-q2.txt
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
random workloads (all arrivals at 0, seed 2015): 500
|
||||||
|
waiting-time mismatches, legacy vs rr.cpp : 0
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/* Round Robin (RR) CPU scheduling in plain C - same rules and same output as rr.cpp.
|
||||||
|
* Input : n, then n lines of "arrival burst", then the time quantum Q.
|
||||||
|
* The ready queue is a circular array; arrivals during a slice are enqueued before the
|
||||||
|
* preempted process goes back to the tail. */
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAXP 64
|
||||||
|
#define MAXS 1024
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n, q;
|
||||||
|
int at[MAXP], bt[MAXP], rem[MAXP], ct[MAXP], tat[MAXP], wt[MAXP], admitted[MAXP] = {0};
|
||||||
|
int queue[MAXP + 1], head = 0, tail = 0; /* circular, one spare slot */
|
||||||
|
int gp[MAXS], gs[MAXS], ge[MAXS], g = 0; /* Gantt slices: pid (0 = idle), start, end */
|
||||||
|
int time = 0, finished = 0, i;
|
||||||
|
|
||||||
|
if (scanf("%d", &n) != 1 || n < 1 || n > MAXP) return 1;
|
||||||
|
for (i = 0; i < n; i++) { if (scanf("%d %d", &at[i], &bt[i]) != 2) return 1; rem[i] = bt[i]; }
|
||||||
|
if (scanf("%d", &q) != 1 || q < 1) return 1;
|
||||||
|
|
||||||
|
while (finished < n) {
|
||||||
|
/* admit every process that has arrived by 'time', in arrival order (ties: lower pid) */
|
||||||
|
for (;;) {
|
||||||
|
int best = -1;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
if (!admitted[i] && at[i] <= time && (best < 0 || at[i] < at[best])) best = i;
|
||||||
|
if (best < 0) break;
|
||||||
|
admitted[best] = 1; queue[tail] = best; tail = (tail + 1) % (MAXP + 1);
|
||||||
|
}
|
||||||
|
if (head == tail) { /* ready queue empty: CPU idles */
|
||||||
|
int t = -1;
|
||||||
|
for (i = 0; i < n; i++) if (!admitted[i] && (t < 0 || at[i] < t)) t = at[i];
|
||||||
|
gp[g] = 0; gs[g] = time; ge[g] = t; g++; time = t;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
i = queue[head]; head = (head + 1) % (MAXP + 1);
|
||||||
|
int run = rem[i] < q ? rem[i] : q;
|
||||||
|
if (g > 0 && gp[g - 1] == i + 1 && ge[g - 1] == time) ge[g - 1] += run;
|
||||||
|
else { gp[g] = i + 1; gs[g] = time; ge[g] = time + run; g++; }
|
||||||
|
time += run; rem[i] -= run;
|
||||||
|
for (;;) { /* arrivals during the slice go in first */
|
||||||
|
int best = -1, j;
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
if (!admitted[j] && at[j] <= time && (best < 0 || at[j] < at[best])) best = j;
|
||||||
|
if (best < 0) break;
|
||||||
|
admitted[best] = 1; queue[tail] = best; tail = (tail + 1) % (MAXP + 1);
|
||||||
|
}
|
||||||
|
if (rem[i] > 0) { queue[tail] = i; tail = (tail + 1) % (MAXP + 1); }
|
||||||
|
else { ct[i] = time; tat[i] = ct[i] - at[i]; wt[i] = tat[i] - bt[i]; finished++; }
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Round Robin, time quantum = %d\n\nPID\tAT\tBT\tCT\tTAT\tWT\n", q);
|
||||||
|
double sw = 0, st = 0;
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);
|
||||||
|
sw += wt[i]; st += tat[i];
|
||||||
|
}
|
||||||
|
printf("\nAverage waiting time : %.2f\nAverage turnaround time : %.2f\n\nGantt chart:\n", sw / n, st / n);
|
||||||
|
{ /* top row of boxes, bottom row of boundary times, aligned by column */
|
||||||
|
char top[4096] = "", nums[4096] = "", lab[16];
|
||||||
|
int tl = 0, nl = 0, k;
|
||||||
|
for (k = 0; k < g; k++) {
|
||||||
|
if (gp[k]) snprintf(lab, sizeof lab, "P%d", gp[k]); else snprintf(lab, sizeof lab, "idle");
|
||||||
|
while (nl < tl) nums[nl++] = ' ';
|
||||||
|
nl += snprintf(nums + nl, sizeof nums - nl, "%d", gs[k]);
|
||||||
|
tl += snprintf(top + tl, sizeof top - tl, "| %s ", lab);
|
||||||
|
}
|
||||||
|
tl += snprintf(top + tl, sizeof top - tl, "|");
|
||||||
|
while (nl < tl - 1) nums[nl++] = ' ';
|
||||||
|
snprintf(nums + nl, sizeof nums - nl, "%d", ge[g - 1]);
|
||||||
|
printf("%s\n%s\n", top, nums);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
// 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";
|
||||||
|
}
|
||||||
Executable
+21
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Runs the earlier tick-loop program and rr.cpp on random workloads where every process
|
||||||
|
arrives at time 0 (the only case the old program can express) and compares waiting times."""
|
||||||
|
import random, subprocess, sys
|
||||||
|
|
||||||
|
random.seed(2015)
|
||||||
|
TRIALS, mismatches = 500, 0
|
||||||
|
for _ in range(TRIALS):
|
||||||
|
n = random.randint(1, 7)
|
||||||
|
bursts = [random.randint(1, 15) for _ in range(n)]
|
||||||
|
q = random.randint(1, 9)
|
||||||
|
old_in = f"{n}\n" + "\n".join(map(str, bursts)) + f"\n{q}\n"
|
||||||
|
new_in = f"{n}\n" + "\n".join(f"0 {b}" for b in bursts) + f"\n{q}\n"
|
||||||
|
old = subprocess.run(["build/old"], input=old_in, capture_output=True, text=True).stdout
|
||||||
|
new = subprocess.run(["build/rr"], input=new_in, capture_output=True, text=True).stdout
|
||||||
|
old_wt = [int(l.split()[2]) for l in old.split("PNo")[1].splitlines()[1:1 + n]]
|
||||||
|
new_wt = [int(l.split("\t")[5]) for l in new.splitlines()[3:3 + n]]
|
||||||
|
if old_wt != new_wt:
|
||||||
|
mismatches += 1
|
||||||
|
print(f"random workloads (all arrivals at 0, seed 2015): {TRIALS}")
|
||||||
|
print(f"waiting-time mismatches, legacy vs rr.cpp : {mismatches}")
|
||||||
Executable
+39
@@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Rebuilds the C++ and C programs and regenerates every file in output/.
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
mkdir -p output build
|
||||||
|
CXX=${CXX:-g++}; CC=${CC:-gcc}
|
||||||
|
$CXX -std=c++17 -Wall -Wextra -O2 -o build/rr rr.cpp
|
||||||
|
$CC -std=c11 -Wall -Wextra -O2 -o build/rrc rr.c
|
||||||
|
$CXX -std=c++17 -o build/old legacy/old_rr_tick_loop.cpp
|
||||||
|
|
||||||
|
build/rr < input/legacy-5proc-q10.txt > output/01-rr-legacy-5proc-q10.txt
|
||||||
|
build/rr < input/classic-q4.txt > output/02-rr-classic-q4.txt
|
||||||
|
build/rr < input/staggered-q2.txt > output/03-rr-staggered-q2.txt
|
||||||
|
build/rr < input/idle-gap-q3.txt > output/04-rr-idle-gap-q3.txt
|
||||||
|
build/rr < input/quantum-1.txt > output/05-rr-quantum-1.txt
|
||||||
|
build/rr < input/large-quantum.txt > output/06-rr-large-quantum.txt
|
||||||
|
build/old < input/legacy-bursts-only.txt > output/07-legacy-full-session.txt
|
||||||
|
|
||||||
|
# Quantum sweep on the 5-process workload: average waiting time and number of Gantt boxes
|
||||||
|
{
|
||||||
|
echo "Q avg-WT avg-TAT gantt-boxes"
|
||||||
|
for q in 1 2 3 5 7 10 12 15 20 29 40; do
|
||||||
|
sed "\$s/.*/$q/" input/legacy-5proc-q10.txt | build/rr | awk -v q="$q" '
|
||||||
|
/Average waiting/ {w=$NF}
|
||||||
|
/Average turnaround/ {t=$NF}
|
||||||
|
/^\| / {b=gsub(/\| /,"| ")}
|
||||||
|
END {printf "%-3s %-8s %-9s %s\n", q, w, t, b}'
|
||||||
|
done
|
||||||
|
} > output/08-quantum-sweep.txt
|
||||||
|
|
||||||
|
# The C and C++ versions must agree byte for byte on every input
|
||||||
|
{
|
||||||
|
for f in input/*.txt; do
|
||||||
|
[ "$f" = input/legacy-bursts-only.txt ] && continue
|
||||||
|
if cmp -s <(build/rr < "$f") <(build/rrc < "$f"); then echo "identical $f"; else echo "DIFFERENT $f"; fi
|
||||||
|
done
|
||||||
|
} > output/09-c-vs-cpp.txt
|
||||||
|
python3 scripts/compare_legacy.py > output/10-legacy-vs-new.txt
|
||||||
|
echo "done: $(ls output | wc -l) files in output/"
|
||||||
Reference in New Issue
Block a user