Function Signature
Description
Implements the SJF (Shortest Job First) scheduling algorithm. Among available processes, the one with the shortest CPU burst time is executed first. This is a non-preemptive algorithm that minimizes average waiting time.Parameters
Array of arrival times for each process. Each element represents when a process arrives in the system.
Array of CPU burst times for each process. Each element represents how long a process needs to execute.
Return Value
Returns an object containing scheduling metrics:Array of start times for each process (indexed by process ID).
Array of completion times for each process (indexed by process ID).
Array of waiting times for each process. Calculated as:
start_time - arrival_time.Array of turnaround times for each process. Calculated as:
completion_time - arrival_time. Represents total time from arrival to completion.Total time required to complete all processes (maximum completion time).
Example Usage
Sample Output
Output Explanation
- Process 0: Arrives at 0, starts immediately (only available process), runs for 8 units, completes at 8
- Process 3: Arrives at 3, has shortest burst (1), starts at 8, completes at 9
- Process 2: Has next shortest burst (2), starts at 9, completes at 11
- Process 1: Has longest burst (4), starts at 11, completes at 15
Algorithm Details
How It Works
- At each scheduling point, find all processes that have arrived
- Among available processes, select the one with shortest CPU burst time
- Execute selected process to completion (non-preemptive)
- Repeat until all processes complete
- If no processes are available, CPU remains idle and time advances
Complexity
- Time Complexity: O(n²) in worst case due to repeated filtering and sorting
- Space Complexity: O(n) for storing process data and results
Characteristics
- Non-preemptive: Once a process starts, it runs to completion
- Optimal: Minimizes average waiting time among non-preemptive algorithms
- Starvation Risk: Long processes may wait indefinitely if short processes keep arriving
- Requires Knowledge: CPU burst time must be known in advance
Tie Breaking
When multiple processes have the same shortest CPU time:- Priority is given to the process that arrived first
- If arrival times are also equal, lower process ID is selected