SJF (Shortest Job First) allocates the CPU to the process which has the smallest CPU-bursts time.
SJF is also called Shortest Job Next(SJN) scheduling.
Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.
It is used frequently in long-term scheduling but it cannot be implemented in short-term scheduling.
It can either be a preemptive or nonpreemptive.
Preemptive SJF scheduling is sometime called Shortest-remaining-time-first-Scheduling.
Preemptive means a Job can be stopped midway to give CPU time to another Job. In Preemptive SJF, the job which has minimum remaining time will be executed next
For example, consider the set of processes that arrive at time 0 and CPU-burst time given in millisecond:
Following shows the Gantt Chart:
In the above diagram: 3,7 and 27 are finish times
To put it differently, process execution will be as follows. In the diagram below: 3, 4, and 20 are duration of each process.
Let us say, the processes arrive in the different arrival time, and say, the process is served by using Preemptive SJF Scheduling.
Now, based on the processes which are in queue at the start:
Above Gantt chart can also be represented as shown below
It helps achieve lesser average waiting time and lesser turnaround time.