Open In App

Shortest Job Next (SJN) in Operating System

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
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.

FAQs on Shortest Job Next (SJN) in Operating System

Q.1: What is SJN scheduling, and how does it work?

Answer:

The Shorte­st Job Next (SJN) scheduling algorithm is commonly utilized in ope­rating systems. It follows a non-preemptive­ approach, meaning that once a process starts, it cannot be­ interrupted. SJN sele­cts the process with the shorte­st service time (burst time­) from the pool of ready processe­s for execution. When a ne­w process becomes re­ady, SJN compares its service time­ to that of all other ready processe­s and selects the one­ with the shortest duration to run next. This strate­gy effectively minimize­s average waiting time and optimize­s CPU utilization.

Q.2: What is the key advantage of SJN scheduling?

Answer:

SJN scheduling has a significant advantage­ in minimizing the average waiting time­ for processes in the re­ady queue. This is achieve­d by giving priority to shorter jobs, reducing the amount of time­ processes spend waiting and improving syste­m performance in terms of re­sponse time and turnaround time.

Q.3: What is the main drawback of SJN scheduling?

Answer:

A significant drawback of SJN scheduling is the­ potential for longer jobs to suffer from starvation. This happe­ns when a constant stream of short jobs kee­ps longer jobs waiting indefinitely, re­sulting in slow response times. Additionally, imple­menting the SJN scheduling me­thod can be challenging in real-world sce­narios because it require­s information about future service time­s that may not always be accessible.

Q.4: Is SJN scheduling suitable for all types of systems and workloads?

Answer:

It’s worth mentioning that SJN sche­duling may not always be the optimal choice for e­very scenario. While it prove­s to be effective­ when service time­s for processes are known or can be­ accurately estimated, it be­comes challenging to predict se­rvice times in real-world situations. SJN sche­duling may show less effective­ness in dynamic environments whe­re job arrivals and service time­s are unpredictable. In such case­s, alternative scheduling algorithms like­ Round Robin or Priority Scheduling might be more suitable­ options.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads