Open In App

Shortest Job First or SJF CPU Scheduling

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Shortest Job First (SJF) or Shortest Job Next (SJN) is a scheduling process that selects the waiting process with the smallest execution time to execute next. This scheduling method may or may not be preemptive. Significantly reduces the average waiting time for other processes waiting to be executed.

Implementation of SJF Scheduling

  • Sort all the processes according to the arrival time. 
  • Then select that process that has minimum arrival time and minimum Burst time. 
  • After completion of the process make a pool of processes (a ready queue) that arrives afterward till the completion of the previous process and select that process in that queue which is having minimum Burst time. 

Estimation Formula Concept in SJF Scheduling

The Shortest Job First (SJF) Scheduling algorithm selects the process with the smallest burst time for execution. But in some cases, the exact burst time of a process may not be known in advance. In such scenarios, an estimation formula is used to predict the next burst time based on the previous burst times.

Estimation Formula

T_{n+1} = \alpha \cdot t_n + (1 - \alpha) \cdot T_n

Where:

  • Tn+1​: Predicted burst time for the next process.
  • Tn: Previously predicted burst time.
  • tn: Actual burst time of the previous process.
  • α: Smoothing factor (0 ≤ α ≤ 1).

Characteristics of SJF Scheduling

  • Shortest Job first has the advantage of having a minimum average waiting time among all operating system scheduling algorithms.
  • It is associated with each task as a unit of time to complete.
  • It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing.

Example of Non Pre-emptive Shortest Job First CPU Scheduling Algorithm

Example: Consider the following table of arrival time and burst time for three processes P1, P2 and P3.

ProcessBurst TimeArrival Time
 P1   6 ms0 ms
 P2 8 ms2 ms
 P3 3 ms4 ms

Step-by-Step Execution:

  1. Time 0-6 (P1): P1 runs for 6 ms (total time left: 0 ms)
  2. Time 6-9 (P3): P3 runs for 3 ms (total time left: 0 ms)
  3. Time 9-17 (P2): P2 runs for 8 ms (total time left: 0 ms)

Gantt chart :


As we know,

  • Turn Around time = Completion time - arrival time
  • Waiting Time = Turn around time - burst time

Now, lets calculate average waiting time and turn around time:

Process

Arrival Time (AT)

Burst Time (BT)

Completion Time (CT)Turn Around Time (TAT)Waiting Time (WT)
 P1  

0

6

66-0 = 66-6 = 0
 P2

2

8

1717-2 = 1515-8 = 7
 P3

4

3

99-4 = 55-3 = 2
  • Average Turn around time = (6 + 15 + 5)/3 = 8.6 ms
  • Average waiting time = ( 2 + 0 + 7 )/3 = 9/3 = 3 ms

To learn about Preemptive Shortest Job First or Shortest Remaining Time First, please refer the following

To learn about Non-Preemptive Shortest Job First, please refer the following

Code Implementation

To learn about how to implement this CPU scheduling algorithm, please refer the following

Advantages of SJF Scheduling

  • SJF is better than the First come first serve(FCFS) algorithm as it reduces the average waiting time.
  • SJF is generally used for long term scheduling.
  • It is suitable for the jobs running in batches, where run times are already known.
  • SJF is probably optimal in terms of average Turn Around Time (TAT).

Disadvantages of SJF Scheduling

  • SJF may cause very long turn-around times or starvation.
  • In SJF job completion time must be known earlier.
  • Many times it becomes complicated to predict the length of the upcoming CPU request.

Next Article

Similar Reads