Scheduling Algorithms in Operating Systems - Part 2 - Shortest Job First (SJF)
Published: 2021-12-07 12:16:38

---
title: Scheduling Algorithms in Operating Systems - Part 2 - Shortest Job First (SJF)
published: true
description: 
tags: 
cover_image: https://cdn-images-1.medium.com/max/2000/1*YFMjCPU7Ttl0xb1lmiUkVg.jpeg
---
### You can skip the intro if you've gone through the [**Part - 1**](https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a)
Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.

For this purpose, there are many scheduling algorithms like the **First Come First Serve** (FCFS) algorithm, **Shortest Job First** (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.

 1. **Arrival Time**: The time at which the process arrives in the processor for execution.

 2. **Waiting Time**: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :
>  *Waiting time = Completion Time- Arrival Time- Service Time*

3. **Service Time/ Burst time**: The time required by the process to complete its execution.

4. **Turnaround Time**: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
>  *Turnaround Time = Service Time + Waiting Time*

## Shortest Job First/ Shortest Process Next :

In this algorithm, the processes that require the least time for execution are executed first.

Let us first consider

### Non-Preemptive Shortest Job First Algorithm :

Let's consider this example:

![](https://cdn-images-1.medium.com/max/2710/0*B8BtbMPGolaXgeBA.png)

Process A is the first process to enter the processor. So it starts executing.

![](https://cdn-images-1.medium.com/max/2060/0*223JsCF8oc909CuX.png)

B is the only process in the queue now, so, it starts executing. As and when the processes arrive, they are added to the waiting queue.

![](https://cdn-images-1.medium.com/max/2060/0*6Y7gj1maQtGCxkrq.png)

Now, in the waiting queue, as we can see process E is the process that will require the least time for execution. Hence, process E starts executing.

![](https://cdn-images-1.medium.com/max/2060/0*D_SymeeGlIcqhsMZ.png)

C being the process requiring the least time starts executing next.

![](https://cdn-images-1.medium.com/max/2060/0*v1P_5giYzyA3E-4X.png)

Finally, D starts executing

![](https://cdn-images-1.medium.com/max/2510/0*po0nXg8M7InraeAn.png)

This is how the SJF algorithm works.

Now, let's calculate the waiting and turnaround time.

![](https://cdn-images-1.medium.com/max/2710/0*LinlW7BFaqgEP3xl.png)

Advantages :

 1. No bias for longer processes.

 2. Improved response time.

Disadvantages :

 1. Increased variability hence reduced predictability.

 2. The processing time of the process needs to be known.

 3. Possibility of starvation for longer processes.

This waiting and turnaround time can be further improved using the below method:

### Preemptive Shortest Job First Algorithm (Shortest remaining time first):

In this method, while a process is executing, if a process requiring lesser service time enters, that process is executed first.

Consider the example:

![](https://cdn-images-1.medium.com/max/2710/0*8ztnrpqFcXPasdYJ.png)

A being the process to arrive first starts executing first.

![](https://cdn-images-1.medium.com/max/2060/0*NklvC2bAZq9ZzF9Q.png)

Here, process B arrives at 2ms. Already executing process i.e. process A requires 1ms to complete and B needs 6ms which is greater. Hence, A continues execution.

Then B starts executing.

![](https://cdn-images-1.medium.com/max/2060/0*RBgQJucUiz_yYOcZ.png)

But at 4ms, C enters the queue. B requires 5ms to complete its execution whereas C requires 4ms. Hence, C starts executing and B is added to the queue.

![](https://cdn-images-1.medium.com/max/2060/0*7mVzZD9kXmi6KDTj.png)

While C was executing, the D process entered the queue but since the time required for D to execute was greater than that of process C, processes were not switched.

Now all the processes are either executed or are in the queue. So now the process requiring minimum time for execution will be executed first. i.e. process E.

![](https://cdn-images-1.medium.com/max/2060/0*XfK_we3E5Bxxp7rr.png)

Now, the queue has processes B and D both requiring 5ms. So any process can be executed next.

B entered the queue first hence, we will execute B first.

![](https://cdn-images-1.medium.com/max/2508/0*S7vUxWbv-SrRGyfl.png)

Now, D is the only process left. Hence, it will get executed next.

![](https://cdn-images-1.medium.com/max/2508/0*C3zpGcksSMlXFF4T.png)

Ok, so let us now calculate the average waiting and turnaround time.

![](https://cdn-images-1.medium.com/max/2710/0*k7QcHgKYrjb3DVj6.png)

Advantages :

 1. Improved turnaround time and waiting time than preemptive SJF.

 2. No bias towards longer processes.

Disadvantages :

 1. The scheduler must have an estimate of processing time.

 2. Risk of starvation of longer processes.

 3. Elapsed service time must be recorded which increases overhead.

In the next part, we'll see the Round Robin Algorithm.


Hope this helps you!

Have a nice day!!