Shortest Job First Scheduling or SJF allocates the CPU to the process which has the smallest CPU-bursts time.
SJF is also called Shortest Job Next scheduling shortly referred to as SJN. Since Job can be called as a process, SJF is also referred to as Shortest Process Next or SPN scheduling
Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.
SJF is used frequently in long term scheduling but it cannot be implemented in short-term scheduling.
SJF can either be a preemptive or nonpreemptive.
Pros of using SJF:
Helps achieve lesser turnaround time
Disadvantages/drawbacks/cons of using SJF:
Jobs or processes that need more time will wait and result in Job starvation or process starvation
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
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: