Shortest Job Next (SJN) in Operating System
Last Updated :
11 Oct, 2023
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.
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
Now let us consider an example with different arrival times.
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
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.
Share your thoughts in the comments
Please Login to comment...