Shortest Job Next (SJN) in Operating System (original) (raw)

Last Updated : 23 Jul, 2025

In **Shortest Job Next(SJN), when choosing the next job to run, look at all the processes in the ready state and dispatch the one with the smallest service time. In this case, we need to know the service times at any given point in time. It is a **non-preemptive algorithm. A new job will not be given a chance at the CPU until the current job finishes even if the new job is shorter.

Key Concepts And Terms

**Examples:

Let us consider a case where the arrival time for all the processes are same.

Process Arrival Time Service Time Completion Time Turnaround
p1 0 140 340 340
p2 0 75 75 75
p3 0 320 940 940
p4 0 280 620 620
p5 0 125 200 200

All the processes p1, p2, p3, p4, and p5 arrive at the same time. The operating system will compare the service times and find out the shortest service time to execute. Therefore, process p2 will execute first, process p5 next, process p1 then, and later followed by process p4 and p3.

 Gantt Chart

Gantt Chart

Now let us consider an example with different arrival times.

Process Arrival Time Service Time Completion Time Turnaround
p1 0 140 140 140-0=140
p2 40 75 215 215-40=175
p3 50 320 535 535-50=485
p4 300 280 940 940-300=640
p5 315 125 660 660-315=345

In this case, process p1 will arrive at 0 seconds and get executed first. The service time of p1 is 140 seconds so p2 and p3 will arrive in the meantime at 40 and 50 seconds respectively. Once p1 completes at 140 seconds, then **turnaround time is calculated by subtracting completion time from arrival time. After the completion of process p1, the operating system compares the service time of processes p2 and p3 which are waiting to be executed. Since p2 has a shorter service time, process p2 will be executed next. Then completion time will be calculated by adding the service time to the arrival time and the turnaround time is completed using the same procedure as process p1. Next process p3 will get executed and during its execution, process p4 and p5 will arrive. However, job p3 will continue its execution until it finishes at 215+320seconds =535seconds. (Note here the completion time is calculated by adding the service time to the completion time of the last executed process.) After that, the operating system compares the service times of processes p4 and p5. Since process p5 has a lower service time, it will be executed next followed by process p4.

 Gantt Chart

Gantt Chart

Advantages

Disadvantages

Conclusion

The SJN scheduling algorithm can be useful in reducing waiting times and increasing system throughput in some situations. However, it does have drawbacks, such as the possibility of causing starvation, requiring accurate job length predictions, and being unresponsive in certain contexts. It is important to carefully evaluate the system's requirements and characteristics before deciding to use SJN as a scheduling algorithm.