SJF and SRTF scheduling programs with Gantt charts and captured output

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b5v46dt7TVsHZxtoYC
This commit is contained in:
asmhatre
2026-11-12 20:45:02 +05:30
co-authored by Claude Sonnet 5.5
commit abaf3840f2
18 changed files with 393 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
build/
+5
View File
@@ -0,0 +1,5 @@
4
0 5
0 3
0 8
0 4
+4
View File
@@ -0,0 +1,4 @@
3
0 2
6 3
7 1
+4
View File
@@ -0,0 +1,4 @@
0 7
2 4
4 1
5 4
+4
View File
@@ -0,0 +1,4 @@
3
0 8
1 4
2 2
+6
View File
@@ -0,0 +1,6 @@
5
0 7
2 4
4 1
5 4
6 3
+110
View File
@@ -0,0 +1,110 @@
// ============================================================
// Shortest Job First (SJF) CPU Scheduling Algorithm in C++
// Non-preemptive: selects process with smallest burst time first
// ============================================================
#include <iostream>
using namespace std;
// Structure to group all attributes of a single process together
struct Process {
int processNo; // Original process number (preserved after sorting)
int arrivalTime; // Time at which process arrives in the ready queue
int burstTime; // CPU burst time required by the process
int waitingTime; // Time spent waiting before CPU is allocated
int turnaroundTime; // Total time from arrival to completion
};
int main() {
const int PROCESS_COUNT = 4;
Process processes[PROCESS_COUNT];
// -------------------------------------------------------
// Step 1: Read arrival and burst times from the user
// -------------------------------------------------------
for (int i = 0; i < PROCESS_COUNT; i++) {
cout << "\n Enter arrival time of P" << i + 1 << ": ";
cin >> processes[i].arrivalTime;
cout << " Enter burst time of P" << i + 1 << ": ";
cin >> processes[i].burstTime;
processes[i].processNo = i + 1; // Remember original process number
}
// Display the entered data before sorting
cout << "\n\n Entered Data (before sorting)";
cout << "\n PNO\tAT\tBT";
for (int i = 0; i < PROCESS_COUNT; i++) {
cout << "\n P" << processes[i].processNo
<< "\t" << processes[i].arrivalTime
<< "\t" << processes[i].burstTime;
}
// -------------------------------------------------------
// Step 2: Sort processes by burst time (ascending = SJF order)
// Uses selection sort; swaps entire Process struct to keep data intact
// -------------------------------------------------------
for (int outer = 0; outer < PROCESS_COUNT - 1; outer++) {
for (int inner = outer + 1; inner < PROCESS_COUNT; inner++) {
if (processes[outer].burstTime > processes[inner].burstTime) {
// Swap the entire process record
Process temp = processes[outer];
processes[outer] = processes[inner];
processes[inner] = temp;
}
}
}
// After sorting: processes[0] has the shortest burst time
// -------------------------------------------------------
// Step 3: Compute Waiting Time and Turnaround Time
// -------------------------------------------------------
processes[0].waitingTime = 0; // First job waits 0
processes[0].turnaroundTime = processes[0].burstTime; // TT = 0 + BT
int totalBurstTime = processes[0].burstTime;
for (int i = 1; i < PROCESS_COUNT; i++) {
// WT[i] = WT[i-1] + BT[i-1] - gap in arrival times
processes[i].waitingTime =
processes[i-1].waitingTime + processes[i-1].burstTime
- (processes[i].arrivalTime - processes[i-1].arrivalTime);
// Guard: waiting time cannot go negative
if (processes[i].waitingTime < 0) processes[i].waitingTime = 0;
processes[i].turnaroundTime =
processes[i].waitingTime + processes[i].burstTime;
totalBurstTime += processes[i].burstTime;
}
// -------------------------------------------------------
// Step 4: Print results
// -------------------------------------------------------
cout << "\n\n Result (sorted by burst time — shortest first)";
cout << "\n PNO\tAT\tWT\tBT\tTT";
for (int i = 0; i < PROCESS_COUNT; i++) {
cout << "\n P" << processes[i].processNo
<< "\t" << processes[i].arrivalTime
<< "\t" << processes[i].waitingTime
<< "\t" << processes[i].burstTime
<< "\t" << processes[i].turnaroundTime;
}
cout << "\n Total Burst Time: " << totalBurstTime;
// -------------------------------------------------------
// Step 5: Compute averages
// -------------------------------------------------------
int totalWaiting = 0, totalTurnaround = 0;
for (int i = 0; i < PROCESS_COUNT; i++) {
totalWaiting += processes[i].waitingTime;
totalTurnaround += processes[i].turnaroundTime;
}
cout << "\n Average Waiting Time : " << (float)totalWaiting / PROCESS_COUNT;
cout << "\n Average Turnaround Time : " << (float)totalTurnaround / PROCESS_COUNT;
cout << endl;
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
Non-preemptive SJF
PID AT BT CT TAT WT
P1 0 5 12 12 7
P2 0 3 3 3 0
P3 0 8 20 20 12
P4 0 4 7 7 3
Average waiting time : 5.50
Average turnaround time : 10.50
Gantt chart:
| P2 | P4 | P1 | P3 |
0 3 7 12 20
+15
View File
@@ -0,0 +1,15 @@
Non-preemptive SJF
PID AT BT CT TAT WT
P1 0 7 7 7 0
P2 2 4 15 13 9
P3 4 1 8 4 3
P4 5 4 19 14 10
P5 6 3 11 5 2
Average waiting time : 4.80
Average turnaround time : 8.60
Gantt chart:
| P1 | P3 | P5 | P2 | P4 |
0 7 8 11 15 19
+15
View File
@@ -0,0 +1,15 @@
Preemptive SJF (SRTF)
PID AT BT CT TAT WT
P1 0 7 19 19 12
P2 2 4 7 5 1
P3 4 1 5 1 0
P4 5 4 14 9 5
P5 6 3 10 4 1
Average waiting time : 3.80
Average turnaround time : 7.60
Gantt chart:
| P1 | P2 | P3 | P2 | P5 | P4 | P1 |
0 2 4 5 7 10 14 19
+13
View File
@@ -0,0 +1,13 @@
Non-preemptive SJF
PID AT BT CT TAT WT
P1 0 8 8 8 0
P2 1 4 14 13 9
P3 2 2 10 8 6
Average waiting time : 5.00
Average turnaround time : 9.67
Gantt chart:
| P1 | P3 | P2 |
0 8 10 14
+13
View File
@@ -0,0 +1,13 @@
Preemptive SJF (SRTF)
PID AT BT CT TAT WT
P1 0 8 14 14 6
P2 1 4 7 6 2
P3 2 2 4 2 0
Average waiting time : 2.67
Average turnaround time : 7.33
Gantt chart:
| P1 | P2 | P3 | P2 | P1 |
0 1 2 4 7 14
+13
View File
@@ -0,0 +1,13 @@
Non-preemptive SJF
PID AT BT CT TAT WT
P1 0 2 2 2 0
P2 6 3 9 3 0
P3 7 1 10 3 2
Average waiting time : 0.67
Average turnaround time : 2.67
Gantt chart:
| P1 | idle | P2 | P3 |
0 2 6 9 10
+9
View File
@@ -0,0 +1,9 @@
Result (sorted by burst time — shortest first)
PNO AT WT BT TT
P3 4 0 1 1
P2 2 3 4 7
P4 5 4 4 8
P1 0 13 7 20
Total Burst Time: 16
Average Waiting Time : 5
Average Turnaround Time : 9
@@ -0,0 +1,14 @@
Non-preemptive SJF
PID AT BT CT TAT WT
P1 0 7 7 7 0
P2 2 4 12 10 6
P3 4 1 8 4 3
P4 5 4 16 11 7
Average waiting time : 4.00
Average turnaround time : 8.00
Gantt chart:
| P1 | P3 | P2 | P4 |
0 7 8 12 16
+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Rebuilds both programs and regenerates every file in output/.
set -euo pipefail
cd "$(dirname "$0")/.."
mkdir -p output build
CXX=${CXX:-g++}
$CXX -std=c++17 -Wall -Wextra -O2 -o build/sjf sjf_nonpreemptive.cpp
$CXX -std=c++17 -Wall -Wextra -O2 -o build/srtf srtf_preemptive.cpp
$CXX -std=c++17 -o build/old legacy/old_sjf_sort_by_burst.cpp
build/sjf < input/all-at-zero.txt > output/01-sjf-all-at-zero.txt
build/sjf < input/staggered.txt > output/02-sjf-staggered.txt
build/srtf < input/staggered.txt > output/03-srtf-staggered.txt
build/sjf < input/preemption.txt > output/04-sjf-preemption-case.txt
build/srtf < input/preemption.txt > output/05-srtf-preemption-case.txt
build/sjf < input/idle-gap.txt > output/06-sjf-idle-gap.txt
build/old < input/legacy-4proc.txt | sed -n '/Result/,$p' > output/07-legacy-wrong-result.txt
{ echo 4; cat input/legacy-4proc.txt; } | build/sjf > output/08-sjf-same-input-corrected.txt
echo "done: $(ls output | wc -l) files in output/"
+72
View File
@@ -0,0 +1,72 @@
// Shortest Job First (SJF) - NON-preemptive, with arrival times and a Gantt chart.
// At every decision point, pick the arrived, unfinished process with the smallest burst time.
// Ties are broken by earlier arrival, then by lower process number.
#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
struct Process {
int pid, at, bt;
int ct = 0, tat = 0, wt = 0; // completion, turnaround, waiting
bool done = false;
};
struct Slice { int pid, start, end; }; // pid = 0 means CPU idle
int main() {
int n;
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; }
vector<Slice> gantt;
int time = 0, finished = 0;
while (finished < n) {
int pick = -1;
for (int i = 0; i < n; i++) {
if (p[i].done || p[i].at > time) continue;
if (pick == -1 || p[i].bt < p[pick].bt ||
(p[i].bt == p[pick].bt && p[i].at < p[pick].at)) pick = i;
}
if (pick == -1) { // nobody has arrived: CPU idles to next arrival
int next = INT_MAX;
for (auto& q : p) if (!q.done) next = min(next, q.at);
gantt.push_back({0, time, next});
time = next;
continue;
}
gantt.push_back({p[pick].pid, time, time + p[pick].bt});
time += p[pick].bt; // runs to completion: non-preemptive
p[pick].ct = time;
p[pick].tat = p[pick].ct - p[pick].at;
p[pick].wt = p[pick].tat - p[pick].bt;
p[pick].done = true;
finished++;
}
cout << "Non-preemptive SJF\n\n";
cout << "PID\tAT\tBT\tCT\tTAT\tWT\n";
double sw = 0, st = 0;
for (auto& q : p) {
cout << "P" << q.pid << "\t" << q.at << "\t" << q.bt << "\t" << q.ct << "\t" << q.tat << "\t" << q.wt << "\n";
sw += q.wt; st += q.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;
auto putNum = [&](int v) { nums += to_string(v); };
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(), ' ');
putNum(s.start);
top += "| " + label + " ";
}
top += "|";
if (nums.size() < top.size() - 1) nums += string(top.size() - 1 - nums.size(), ' ');
putNum(gantt.back().end);
cout << top << "\n" << nums << "\n";
}
+62
View File
@@ -0,0 +1,62 @@
// Shortest Remaining Time First (SRTF) - PREEMPTIVE SJF, simulated one time unit at a time.
// At every tick, run the arrived process with the least remaining time. A newly arrived
// process with a smaller remaining time preempts the running one.
#include <climits>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
struct Process { int pid, at, bt, rem, ct = 0, tat = 0, wt = 0; };
struct Slice { int pid, start, end; }; // pid = 0 means idle
int main() {
int n;
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; }
vector<Slice> gantt;
int time = 0, finished = 0;
while (finished < n) {
int pick = -1;
for (int i = 0; i < n; i++) {
if (p[i].rem == 0 || p[i].at > time) continue;
if (pick == -1 || p[i].rem < p[pick].rem ||
(p[i].rem == p[pick].rem && p[i].at < p[pick].at)) pick = i;
}
int pid = (pick == -1) ? 0 : p[pick].pid;
if (!gantt.empty() && gantt.back().pid == pid) gantt.back().end = time + 1; // extend slice
else gantt.push_back({pid, time, time + 1});
if (pick != -1 && --p[pick].rem == 0) {
p[pick].ct = time + 1;
p[pick].tat = p[pick].ct - p[pick].at;
p[pick].wt = p[pick].tat - p[pick].bt;
finished++;
}
time++;
}
cout << "Preemptive SJF (SRTF)\n\n";
cout << "PID\tAT\tBT\tCT\tTAT\tWT\n";
double sw = 0, st = 0;
for (auto& q : p) {
cout << "P" << q.pid << "\t" << q.at << "\t" << q.bt << "\t" << q.ct << "\t" << q.tat << "\t" << q.wt << "\n";
sw += q.wt; st += q.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;
auto putNum = [&](int v) { nums += to_string(v); };
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(), ' ');
putNum(s.start);
top += "| " + label + " ";
}
top += "|";
if (nums.size() < top.size() - 1) nums += string(top.size() - 1 - nums.size(), ' ');
putNum(gantt.back().end);
cout << top << "\n" << nums << "\n";
}