From abaf3840f2438002f436baf1ef0d1e466128d34b Mon Sep 17 00:00:00 2001 From: asmhatre Date: Thu, 12 Nov 2026 20:45:02 +0530 Subject: [PATCH] SJF and SRTF scheduling programs with Gantt charts and captured output Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_017ic4b5v46dt7TVsHZxtoYC --- os/sjf/.gitignore | 1 + os/sjf/input/all-at-zero.txt | 5 + os/sjf/input/idle-gap.txt | 4 + os/sjf/input/legacy-4proc.txt | 4 + os/sjf/input/preemption.txt | 4 + os/sjf/input/staggered.txt | 6 + os/sjf/legacy/old_sjf_sort_by_burst.cpp | 110 ++++++++++++++++++ os/sjf/output/01-sjf-all-at-zero.txt | 14 +++ os/sjf/output/02-sjf-staggered.txt | 15 +++ os/sjf/output/03-srtf-staggered.txt | 15 +++ os/sjf/output/04-sjf-preemption-case.txt | 13 +++ os/sjf/output/05-srtf-preemption-case.txt | 13 +++ os/sjf/output/06-sjf-idle-gap.txt | 13 +++ os/sjf/output/07-legacy-wrong-result.txt | 9 ++ os/sjf/output/08-sjf-same-input-corrected.txt | 14 +++ os/sjf/scripts/run-all.sh | 19 +++ os/sjf/sjf_nonpreemptive.cpp | 72 ++++++++++++ os/sjf/srtf_preemptive.cpp | 62 ++++++++++ 18 files changed, 393 insertions(+) create mode 100644 os/sjf/.gitignore create mode 100644 os/sjf/input/all-at-zero.txt create mode 100644 os/sjf/input/idle-gap.txt create mode 100644 os/sjf/input/legacy-4proc.txt create mode 100644 os/sjf/input/preemption.txt create mode 100644 os/sjf/input/staggered.txt create mode 100644 os/sjf/legacy/old_sjf_sort_by_burst.cpp create mode 100644 os/sjf/output/01-sjf-all-at-zero.txt create mode 100644 os/sjf/output/02-sjf-staggered.txt create mode 100644 os/sjf/output/03-srtf-staggered.txt create mode 100644 os/sjf/output/04-sjf-preemption-case.txt create mode 100644 os/sjf/output/05-srtf-preemption-case.txt create mode 100644 os/sjf/output/06-sjf-idle-gap.txt create mode 100644 os/sjf/output/07-legacy-wrong-result.txt create mode 100644 os/sjf/output/08-sjf-same-input-corrected.txt create mode 100755 os/sjf/scripts/run-all.sh create mode 100644 os/sjf/sjf_nonpreemptive.cpp create mode 100644 os/sjf/srtf_preemptive.cpp diff --git a/os/sjf/.gitignore b/os/sjf/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/os/sjf/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/os/sjf/input/all-at-zero.txt b/os/sjf/input/all-at-zero.txt new file mode 100644 index 0000000..f6ca63e --- /dev/null +++ b/os/sjf/input/all-at-zero.txt @@ -0,0 +1,5 @@ +4 +0 5 +0 3 +0 8 +0 4 diff --git a/os/sjf/input/idle-gap.txt b/os/sjf/input/idle-gap.txt new file mode 100644 index 0000000..f1f9cc4 --- /dev/null +++ b/os/sjf/input/idle-gap.txt @@ -0,0 +1,4 @@ +3 +0 2 +6 3 +7 1 diff --git a/os/sjf/input/legacy-4proc.txt b/os/sjf/input/legacy-4proc.txt new file mode 100644 index 0000000..e1b3aba --- /dev/null +++ b/os/sjf/input/legacy-4proc.txt @@ -0,0 +1,4 @@ +0 7 +2 4 +4 1 +5 4 diff --git a/os/sjf/input/preemption.txt b/os/sjf/input/preemption.txt new file mode 100644 index 0000000..caec418 --- /dev/null +++ b/os/sjf/input/preemption.txt @@ -0,0 +1,4 @@ +3 +0 8 +1 4 +2 2 diff --git a/os/sjf/input/staggered.txt b/os/sjf/input/staggered.txt new file mode 100644 index 0000000..ad852f1 --- /dev/null +++ b/os/sjf/input/staggered.txt @@ -0,0 +1,6 @@ +5 +0 7 +2 4 +4 1 +5 4 +6 3 diff --git a/os/sjf/legacy/old_sjf_sort_by_burst.cpp b/os/sjf/legacy/old_sjf_sort_by_burst.cpp new file mode 100644 index 0000000..15c7b6b --- /dev/null +++ b/os/sjf/legacy/old_sjf_sort_by_burst.cpp @@ -0,0 +1,110 @@ +// ============================================================ +// Shortest Job First (SJF) CPU Scheduling Algorithm in C++ +// Non-preemptive: selects process with smallest burst time first +// ============================================================ + +#include +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; +} diff --git a/os/sjf/output/01-sjf-all-at-zero.txt b/os/sjf/output/01-sjf-all-at-zero.txt new file mode 100644 index 0000000..d5d052b --- /dev/null +++ b/os/sjf/output/01-sjf-all-at-zero.txt @@ -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 diff --git a/os/sjf/output/02-sjf-staggered.txt b/os/sjf/output/02-sjf-staggered.txt new file mode 100644 index 0000000..2f3649e --- /dev/null +++ b/os/sjf/output/02-sjf-staggered.txt @@ -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 diff --git a/os/sjf/output/03-srtf-staggered.txt b/os/sjf/output/03-srtf-staggered.txt new file mode 100644 index 0000000..c347503 --- /dev/null +++ b/os/sjf/output/03-srtf-staggered.txt @@ -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 diff --git a/os/sjf/output/04-sjf-preemption-case.txt b/os/sjf/output/04-sjf-preemption-case.txt new file mode 100644 index 0000000..3e92985 --- /dev/null +++ b/os/sjf/output/04-sjf-preemption-case.txt @@ -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 diff --git a/os/sjf/output/05-srtf-preemption-case.txt b/os/sjf/output/05-srtf-preemption-case.txt new file mode 100644 index 0000000..c0a60eb --- /dev/null +++ b/os/sjf/output/05-srtf-preemption-case.txt @@ -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 diff --git a/os/sjf/output/06-sjf-idle-gap.txt b/os/sjf/output/06-sjf-idle-gap.txt new file mode 100644 index 0000000..ffe2b40 --- /dev/null +++ b/os/sjf/output/06-sjf-idle-gap.txt @@ -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 diff --git a/os/sjf/output/07-legacy-wrong-result.txt b/os/sjf/output/07-legacy-wrong-result.txt new file mode 100644 index 0000000..bbc81a1 --- /dev/null +++ b/os/sjf/output/07-legacy-wrong-result.txt @@ -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 diff --git a/os/sjf/output/08-sjf-same-input-corrected.txt b/os/sjf/output/08-sjf-same-input-corrected.txt new file mode 100644 index 0000000..fe565e4 --- /dev/null +++ b/os/sjf/output/08-sjf-same-input-corrected.txt @@ -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 diff --git a/os/sjf/scripts/run-all.sh b/os/sjf/scripts/run-all.sh new file mode 100755 index 0000000..df2a9bf --- /dev/null +++ b/os/sjf/scripts/run-all.sh @@ -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/" diff --git a/os/sjf/sjf_nonpreemptive.cpp b/os/sjf/sjf_nonpreemptive.cpp new file mode 100644 index 0000000..559aaea --- /dev/null +++ b/os/sjf/sjf_nonpreemptive.cpp @@ -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 +#include +#include +#include +#include +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 p(n); + for (int i = 0; i < n; i++) { p[i].pid = i + 1; cin >> p[i].at >> p[i].bt; } + + vector 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"; +} diff --git a/os/sjf/srtf_preemptive.cpp b/os/sjf/srtf_preemptive.cpp new file mode 100644 index 0000000..a540beb --- /dev/null +++ b/os/sjf/srtf_preemptive.cpp @@ -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 +#include +#include +#include +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 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 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"; +}