Open In App

Shortest Job Next (SJN) in Operating System

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

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

  • Service time is the amount of time a process or job takes to complete execution once it starts running on the CPU. It is also known as burst time.
  • Arrival time is the time at which a process arrives and is ready for execution in the CPU or the scheduling queue.
  • Completion time is the point in time when a process finishes its execution.
  • Turnaround time is the total time taken by a process to complete its execution, from the moment it arrives in the ready queue until it finishes execution. It is calculated as the completion time minus the arrival time.
  • A non-preemptive scheduling algorithm is one where a process that has started executing continues until it has finished completing its execution. New processes are not allowed to interrupt the currently running process.

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

  • SJN scheduling minimizes the average waiting time for processes in the ready queue since shorter jobs are given more priority reducing their waiting time and improving system performance in terms of response time and turnaround time.
  • Using the Shorte­st Job Next (SJN) scheduling algorithm can lead to be­tter CPU resource utilization as it prioritize­s shorter jobs. This allows more processe­s to be completed faste­r.

Disadvantages

  • Starvation can become­ a concern when using the SJN algorithm. In this situation, longe­r-duration jobs may never have an opportunity to e­xecute if there­ is a continuous arrival of shorter jobs. This issue can be particularly proble­matic in systems where fairne­ss is a key consideration.
  • Predicting the­ length of jobs accurately can be a challe­nging task in practice. When the pre­dicted job lengths turn out to be inaccurate­, it can affect the performance­ of SJN and result in frequent pre­emptions.
  • In situations where fast responses are crucial, SJN might not be the most responsive option for interactive systems or real-time environments. When shorter jobs dominate the CPU, longer jobs may experience lengthy waits, which is often detrimental.

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.


Explore