Implementing SJF (Shortest Job First) Scheduling Algorithm in C++
In this post, we implement the Shortest Job First (SJF) CPU scheduling algorithm in C++. SJF always selects the process with the smallest burst time from the ready queue and executes it next. This greedy approach provably minimizes the average waiting time, making SJF theoretically optimal among all non-preemptive algorithms when all jobs are available simultaneously. What is SJF Scheduling? In SJF, the scheduler sorts all ready processes by their burst (execution) time in ascending order and then runs them in that sequence. The shortest job finishes earliest, releasing the CPU quickly for others and keeping the average wait low. It is non-preemptive — once started, a process runs to completion. The preemptive variant is called Shortest Remaining Time First (SRTF). Waiting Time (WT): WT[i] = WT[i-1] + BurstTime[i-1] - (ArrivalTime[i] - ArrivalTime[i-1]) Turnaround Time (TT): TT[i] = WT[i] + BurstTime[i] The key risk with SJF is starvation: if short jobs keep arriving, long jobs may never execute.