---
description: What is Shortest Job First Scheduling? SJF is an algorithm in which the process having the smallest execution time is chosen for the next execution. This scheduling method can be preemptive or non-pre
title: Shortest Job First (SJF): Preemptive, Non-Preemptive Example
image: https://www.guru99.com/images/shortest-job-first-sjf.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Shortest Job First (SJF) is a CPU scheduling algorithm that selects the process with the smallest execution time to run next. It can be preemptive or non-preemptive and significantly reduces the average waiting time for processes.

* ⏱️ **Definition:** The process with the shortest burst time is chosen for the next execution.
* 🔀 **Two Types:** SJF can be non-preemptive or preemptive (Shortest Remaining Time First).
* 📉 **Key Benefit:** It gives the lowest average waiting time for a given set of processes.
* 🏭 **Best Use:** Ideal for batch systems where job run times are known in advance.
* ❓ **Main Limitation:** Burst time must be known ahead, which is hard to predict.
* ⚠️ **Risk:** Long processes may starve if short jobs keep arriving.

[ Read More ](javascript:void%280%29;) 

![Shortest Job First \(SJF\) Scheduling]()

## What is Shortest Job First Scheduling?

**Shortest Job First (SJF)** is an algorithm in which the process having the smallest execution time is chosen for the next execution. This scheduling method can be preemptive or non-preemptive. It significantly reduces the average waiting time for other processes awaiting execution. The full form of SJF is Shortest Job First.

**There are basically two types of SJF methods:**

* Non-Preemptive SJF
* Preemptive SJF

## Characteristics of SJF Scheduling

* It is associated with each job as a unit of time to complete.
* This algorithm method is helpful for batch-type processing, where waiting for jobs to complete is not critical.
* It can improve process throughput by making sure that shorter jobs are executed first, hence possibly having a short turnaround time.
* It improves job output by offering shorter jobs, which should be executed first, and which mostly have a shorter turnaround time.

## Non-Preemptive SJF

In non-preemptive scheduling, once the CPU cycle is allocated to a process, the process holds it till it reaches a waiting state or is terminated.

Consider the following five processes, each having its own unique burst time and arrival time.

| Process Queue | Burst time | Arrival time |
| ------------- | ---------- | ------------ |
| P1            | 6          | 2            |
| P2            | 2          | 5            |
| P3            | 8          | 1            |
| P4            | 3          | 0            |
| P5            | 4          | 4            |

**Step 0)** At time = 0, P4 arrives and starts execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob1.png)

**Step 1)** At time = 1, Process P3 arrives. But P4 still needs 2 execution units to complete. It will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob2.png)

**Step 2)** At time = 2, process P1 arrives and is added to the waiting queue. P4 will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob3.png)

**Step 3)** At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed because its burst time is less compared to P3.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob4.png)

**Step 4)** At time = 4, process P5 arrives and is added to the waiting queue. P1 will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob5.png)

**Step 5)** At time = 5, process P2 arrives and is added to the waiting queue. P1 will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob6.png)

**Step 6)** At time = 9, process P1 will finish its execution. The burst time of P3, P5, and P2 is compared. Process P2 is executed because its burst time is the lowest.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob7.png)

**Step 7)** At time = 10, P2 is executing and P3 and P5 are in the waiting queue.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob8.png)

**Step 8)** At time = 11, process P2 will finish its execution. The burst time of P3 and P5 is compared. Process P5 is executed because its burst time is lower.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob9.png)

**Step 9)** At time = 15, process P5 will finish its execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob10.png)

**Step 10)** At time = 23, process P3 will finish its execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob11.png)

**Step 11)** Let us calculate the average waiting time for the above example.

Wait time
P4 = 0 - 0 = 0
P1 = 3 - 2 = 1
P2 = 9 - 5 = 4
P5 = 11 - 4 = 7
P3 = 15 - 1 = 14

Average Waiting Time = (0 + 1 + 4 + 7 + 14)/5 = 26/5 = 5.2

### RELATED ARTICLES

* [CPU Core, Multi-Core, Thread, Core vs Threads, Hyper-Threading ](https://www.guru99.com/cpu-core-multicore-thread.html "CPU Core, Multi-Core, Thread, Core vs Threads, Hyper-Threading")
* [Difference Between RAM and ROM ](https://www.guru99.com/difference-between-rom-ram.html "Difference Between RAM and ROM")
* [FCFS Scheduling Algorithm: What is, Example Program ](https://www.guru99.com/fcfs-scheduling.html "FCFS Scheduling Algorithm: What is, Example Program")
* [8 BEST Operating Systems (2026 OS List) ](https://www.guru99.com/best-operating-systems.html "8 BEST Operating Systems (2026 OS List)")

## Preemptive SJF

In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process with the shortest burst time begins execution. If a process with an even shorter burst time arrives, the current process is removed or preempted from execution, and the shorter job is allocated a CPU cycle.

Consider the following five processes:

| Process Queue | Burst time | Arrival time |
| ------------- | ---------- | ------------ |
| P1            | 6          | 2            |
| P2            | 2          | 5            |
| P3            | 8          | 1            |
| P4            | 3          | 0            |
| P5            | 4          | 4            |

**Step 0)** At time = 0, P4 arrives and starts execution.

| Process Queue | Burst time | Arrival time |
| ------------- | ---------- | ------------ |
| P1            | 6          | 2            |
| P2            | 2          | 5            |
| P3            | 8          | 1            |
| P4            | 3          | 0            |
| P5            | 4          | 4            |

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob12.png)

**Step 1)** At time = 1, Process P3 arrives. But P4 has a shorter burst time. It will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob13.png)

**Step 2)** At time = 2, process P1 arrives with burst time = 6\. The burst time is more than that of P4\. Hence, P4 will continue execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob14.png)

**Step 3)** At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed because its burst time is lower.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob15.png)

**Step 4)** At time = 4, process P5 will arrive. The burst time of P3, P5, and P1 is compared. Process P5 is executed because its burst time is lowest. Process P1 is preempted.

| Process Queue | Burst time              | Arrival time |
| ------------- | ----------------------- | ------------ |
| P1            | 5 out of 6 is remaining | 2            |
| P2            | 2                       | 5            |
| P3            | 8                       | 1            |
| P4            | 3                       | 0            |
| P5            | 4                       | 4            |

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob16.png)

**Step 5)** At time = 5, process P2 will arrive. The burst time of P1, P2, P3, and P5 is compared. Process P2 is executed because its burst time is least. Process P5 is preempted.

| Process Queue | Burst time              | Arrival time |
| ------------- | ----------------------- | ------------ |
| P1            | 5 out of 6 is remaining | 2            |
| P2            | 2                       | 5            |
| P3            | 8                       | 1            |
| P4            | 3                       | 0            |
| P5            | 3 out of 4 is remaining | 4            |

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob17.png)

**Step 6)** At time = 6, P2 is executing.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob18.png)

**Step 7)** At time = 7, P2 finishes its execution. The burst time of P1, P3, and P5 is compared. Process P5 is executed because its burst time is lesser.

| Process Queue | Burst time              | Arrival time |
| ------------- | ----------------------- | ------------ |
| P1            | 5 out of 6 is remaining | 2            |
| P2            | 2                       | 5            |
| P3            | 8                       | 1            |
| P4            | 3                       | 0            |
| P5            | 3 out of 4 is remaining | 4            |

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob19.png)

**Step 8)** At time = 10, P5 will finish its execution. The burst time of P1 and P3 is compared. Process P1 is executed because its burst time is less.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob20.png)

**Step 9)** At time = 15, P1 finishes its execution. P3 is the only process left. It will start execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob21.png)

**Step 10)** At time = 23, P3 finishes its execution.

[](https://www.guru99.com/images/1/122419%5F0538%5FShortestJob22.png)

**Step 11)** Let us calculate the average waiting time for the above example.

Wait time
P4 = 0 - 0 = 0
P1 = (3 - 2) + 6 = 7
P2 = 5 - 5 = 0
P5 = 4 - 4 + 2 = 2
P3 = 15 - 1 = 14

Average Waiting Time = (0 + 7 + 0 + 2 + 14)/5 = 23/5 = 4.6

## Advantages of SJF

Here are the benefits/pros of using the SJF method:

* SJF is frequently used for long term scheduling.
* It reduces the average waiting time over the FIFO (First In First Out) algorithm.
* The SJF method gives the lowest average waiting time for a specific set of processes.
* It is appropriate for the jobs running in batch, where run times are known in advance.
* For the batch system of long-term scheduling, a burst time estimate can be obtained from the job description.
* For Short-Term Scheduling, we need to predict the value of the next burst time.
* It is probably optimal with regard to average turnaround time.

## Disadvantages/Cons of SJF

Here are some drawbacks/cons of the SJF algorithm:

* Job completion time must be known earlier, but it is hard to predict.
* It is often used in a batch system for long term scheduling.
* SJF cannot be implemented for [CPU scheduling](https://www.guru99.com/cpu-scheduling-algorithms.html) for the short term. It is because there is no specific method to predict the length of the upcoming CPU burst.
* This algorithm may cause very long turnaround times or starvation.
* Requires knowledge of how long a process or job will run.
* It leads to starvation that does not reduce average turnaround time.
* It is hard to know the length of the upcoming CPU request.
* Elapsed time should be recorded, which results in more overhead on the processor.

## FAQs

🔁 What is the difference between SJF and SRTF?

SRTF (Shortest Remaining Time First) is simply the preemptive version of SJF. In SJF, a running job finishes before the next is chosen. In SRTF, a newly arrived job with a shorter remaining time can preempt the running process.

🍽️ Why does SJF scheduling cause starvation?

SJF always favors the shortest job. If short processes keep arriving, a long process may never get the CPU and waits indefinitely. This is starvation. Aging, which slowly raises a waiting job’s priority, is used to prevent it.

🏆 Is Shortest Job First an optimal scheduling algorithm?

Yes. SJF is provably optimal because it produces the minimum possible average waiting time for a given set of processes. However, this is only true if the burst times are known in advance, which is rarely possible in practice.

🤖 How can AI predict burst time for SJF scheduling?

AI and machine learning can analyze a process’s history, code features, and past runs to estimate its CPU burst time. Better predictions make SJF more accurate, reducing waiting time compared to traditional exponential-averaging estimates.

🧠 Can AI make SJF practical for short-term CPU scheduling?

Potentially. SJF struggles for short-term scheduling because burst times are unknown. AI that predicts bursts in real time could make SJF usable, but the prediction overhead and errors must stay low enough to keep the scheduling decision worthwhile.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/shortest-job-first-sjf.png","url":"https://www.guru99.com/images/shortest-job-first-sjf.png","width":"700","height":"250","caption":"Shortest Job First (SJF)","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/operating-system","name":"Operating System"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html","name":"Shortest Job First (SJF): Preemptive, Non-Preemptive Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#webpage","url":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html","name":"Shortest Job First (SJF): Preemptive, Non-Preemptive Example","dateModified":"2026-08-06T17:12:59+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/shortest-job-first-sjf.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/nathaniel","name":"Nathaniel Brooks","description":"I'm Nathaniel Brooks, a seasoned professional in OS tutorials, specializing in creating comprehensive guides to help you master your operating system skills.","url":"https://www.guru99.com/author/nathaniel","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/nathaniel-brooks-author.png","url":"https://www.guru99.com/images/nathaniel-brooks-author.png","caption":"Nathaniel Brooks","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Operating System","headline":"Shortest Job First (SJF): Preemptive, Non-Preemptive Example","description":"What is Shortest Job First Scheduling? SJF is an algorithm in which the process having the smallest execution time is chosen for the next execution. This scheduling method can be preemptive or non-pre","keywords":"bigdata, programming, database, server","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/nathaniel","name":"Nathaniel Brooks"},"dateModified":"2026-08-06T17:12:59+05:30","image":{"@id":"https://www.guru99.com/images/shortest-job-first-sjf.png"},"copyrightYear":"2026","name":"Shortest Job First (SJF): Preemptive, Non-Preemptive Example","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between SJF and SRTF?","acceptedAnswer":{"@type":"Answer","text":"SRTF (Shortest Remaining Time First) is simply the preemptive version of SJF. In SJF, a running job finishes before the next is chosen. In SRTF, a newly arrived job with a shorter remaining time can preempt the running process."}},{"@type":"Question","name":"Why does SJF scheduling cause starvation?","acceptedAnswer":{"@type":"Answer","text":"SJF always favors the shortest job. If short processes keep arriving, a long process may never get the CPU and waits indefinitely. This is starvation. Aging, which slowly raises a waiting job's priority, is used to prevent it."}},{"@type":"Question","name":"Is Shortest Job First an optimal scheduling algorithm?","acceptedAnswer":{"@type":"Answer","text":"Yes. SJF is provably optimal because it produces the minimum possible average waiting time for a given set of processes. However, this is only true if the burst times are known in advance, which is rarely possible in practice."}},{"@type":"Question","name":"How can AI predict burst time for SJF scheduling?","acceptedAnswer":{"@type":"Answer","text":"AI and machine learning can analyze a process's history, code features, and past runs to estimate its CPU burst time. Better predictions make SJF more accurate, reducing waiting time compared to traditional exponential-averaging estimates."}},{"@type":"Question","name":"Can AI make SJF practical for short-term CPU scheduling?","acceptedAnswer":{"@type":"Answer","text":"Potentially. SJF struggles for short-term scheduling because burst times are unknown. AI that predicts bursts in real time could make SJF usable, but the prediction overhead and errors must stay low enough to keep the scheduling decision worthwhile."}}]}],"@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#schema-1138064","isPartOf":{"@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/shortest-job-first-sjf-scheduling.html#webpage"}}]}
```
