Explanation
1. Purpose
This program simulates the Non-Preemptive Shortest Job First (SJF) CPU scheduling algorithm. It schedules processes based on the shortest execution time among the available jobs and calculates each process's Waiting Time (WT), Turnaround Time (TAT), and their overall averages.
2. Key Concepts
- Lists and Map/Split: Parsing comma-separated inputs into integer lists.
- Greedy Selection Logic: Searching for the minimum value (
burst time) among currently eligible items. - Simulation Loop: Advancing a simulated clock (
time) and tracking completion status using state arrays (done).
3. Code Walkthrough
-
Input & Initialization (Lines 1–11):
Reads the total processes ($n$), arrival times ($at$), and burst times ($bt$). Initializes tracking lists for waiting time ($wt$), turnaround time ($tat$), and completion status ($done$ with $0$s). -
Finding the Shortest Job (Lines 13–21):
Runs a loop until all processes are finished (count < n). In each iteration, it scans for an uncompleted process (done[i] == 0) that has already arrived (at[i] <= time) and has the smallest burst time (small). -
Execution & Time Update (Lines 23–31):
- CPU Idle (
small == -1): If no process has arrived yet, the clock advances by 1 (time += 1). - Process Runs: If a job is picked:
- $WT = \text{Current Time} - \text{Arrival Time}$
- Current time advances by the burst time (
time += bt[small]). - $TAT = WT + BT$
- Marks the process as completed (
done = 1) and incrementscount.
- CPU Idle (
-
Results Display (Lines 33–42):
Prints a summary table showing each process's metrics, followed by the calculated averages usingsum() / n.
4. Output
The program outputs:
- A tab-separated table displaying the Process ID, Arrival Time (AT), Burst Time (BT), Waiting Time (WT), and Turnaround Time (TAT) for each process.
- The Average Waiting Time and Average Turnaround Time.