SJF (Shortest Job First) allocates the CPU to the process which has the smallest CPU-bursts time.

SJF is also called Shortest Job Next(SJN) scheduling.

Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.

It is used frequently in long-term scheduling but it cannot be implemented in short-term scheduling.

It can either be a preemptive or nonpreemptive.

## Preemptive SJF scheduling

Preemptive SJF scheduling is sometime called Shortest-remaining-time-first-Scheduling.

### How does it work?

Preemptive means a Job can be stopped midway to give CPU time to another Job. In Preemptive SJF, the job which has minimum remaining time will be executed next

### For Processes That Arrive At The Same Time

For example, consider the set of processes that arrive at time 0 and CPU-burst time given in millisecond:

Process CPU-Burst Time P1 20 P2 4 P3 3 Following shows the

**Gantt Chart**:In the above diagram: 3,7 and 27 are finish times

To put it differently, process execution will be as follows. In the diagram below: 3, 4, and 20 are duration of each process.

P3 ^{3}P2 ^{4}P1 ^{20}P3 ^{3}P2 ^{4}P1 ^{20}### Waiting Time and Turnaround time

ProcessOrder Arrival

TimeCPU

BurstWaiting

TimeTurn

around

timeP1 Job executed Third 0 20 7 27 P2 Job executed Second 0 4 3 7 P3 Job executed First

because it takes

less time0 3 0 3

Average Waiting Time = (7+3+0)/3 = 3.333 ms Average Turnaround Time = (27+7+3)/3 = 12.333 ms ### For Processes That Arrive At Different Times

Let us say, the processes arrive in the different arrival time, and say, the process is served by using Preemptive SJF Scheduling.

Process Arrival Time CPU-Burst Time P1 0 20 P2 1 4 P3 3 3 Now, based on the processes which are in queue at the start:

- Process which takes the least time will be executed first.
- Once first process is progressing, Let us say, other processes arrive.
- Algorithm will calculate the remaining time for all the processes.
- Current or other process that arrived, whichever has least remaining time will execute next.
- In a similar way, as more processes arrive, current process is paused and process with least remaining time will be executed.

**Gantt Chart**:Above Gantt chart can also be represented as shown below

P1 ^{1}P2 ^{2}P2 ^{2}P3 ^{3}P1 ^{19}P1 ^{1}P2 ^{2}P2 ^{2}P3 ^{3}P1 ^{19}### Waiting Time and Turnaround time

ProcessArrival

TimeCPU

BurstStart

TimeWaiting Time =

Start Time -

Arrival TimeFinish

TimeTurnaround

time=

Finish Time -

Arrival TimeP1 0 20 8 8-1=7 27 27-0=27 P2 1 4 1 1-1=0 5 5-1=4 P3 3 3 5 5-3=2 8 8-3=5 Average Waiting Time = (7+0+2)/3 = 3 ms Average Turnaround Time = (27+4+5)/3 = 12 ms Average Waiting Time

= (7+0+2)/3

= 3 msAverage Turnaround Time

= (27+4+5)/3

= 12 ms### Advantages

It helps achieve lesser average waiting time and lesser turnaround time.

SJF (Shortest Job First) allocates the CPU to the process which has the smallest CPU-bursts time.

SJF is also called Shortest Job Next(SJN) scheduling.

Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.

It is used frequently in long-term scheduling but it cannot be implemented in short-term scheduling.

It can either be a preemptive or nonpreemptive.

## Preemptive SJF scheduling

Preemptive SJF scheduling is sometime called Shortest-remaining-time-first-Scheduling.

### How does it work?

Preemptive means a Job can be stopped midway to give CPU time to another Job. In Preemptive SJF, the job which has minimum remaining time will be executed next

### For Processes That Arrive At The Same Time

For example, consider the set of processes that arrive at time 0 and CPU-burst time given in millisecond:

Process CPU-Burst Time P1 20 P2 4 P3 3 Following shows the

**Gantt Chart**:In the above diagram: 3,7 and 27 are finish times

To put it differently, process execution will be as follows. In the diagram below: 3, 4, and 20 are duration of each process.

P3 ^{3}P2 ^{4}P1 ^{20}P3 ^{3}P2 ^{4}P1 ^{20}### Waiting Time and Turnaround time

ProcessOrder Arrival

TimeCPU

BurstWaiting

TimeTurn

around

timeP1 Job executed Third 0 20 7 27 P2 Job executed Second 0 4 3 7 P3 Job executed First

because it takes

less time0 3 0 3

Average Waiting Time = (7+3+0)/3 = 3.333 ms Average Turnaround Time = (27+7+3)/3 = 12.333 ms ### For Processes That Arrive At Different Times

Let us say, the processes arrive in the different arrival time, and say, the process is served by using Preemptive SJF Scheduling.

Process Arrival Time CPU-Burst Time P1 0 20 P2 1 4 P3 3 3 Now, based on the processes which are in queue at the start:

- Process which takes the least time will be executed first.
- Once first process is progressing, Let us say, other processes arrive.
- Algorithm will calculate the remaining time for all the processes.
- Current or other process that arrived, whichever has least remaining time will execute next.
- In a similar way, as more processes arrive, current process is paused and process with least remaining time will be executed.

**Gantt Chart**:Above Gantt chart can also be represented as shown below

P1 ^{1}P2 ^{2}P2 ^{2}P3 ^{3}P1 ^{19}P1 ^{1}P2 ^{2}P2 ^{2}P3 ^{3}P1 ^{19}### Waiting Time and Turnaround time

ProcessArrival

TimeCPU

BurstStart

TimeWaiting Time =

Start Time -

Arrival TimeFinish

TimeTurnaround

time=

Finish Time -

Arrival TimeP1 0 20 8 8-1=7 27 27-0=27 P2 1 4 1 1-1=0 5 5-1=4 P3 3 3 5 5-3=2 8 8-3=5 Average Waiting Time = (7+0+2)/3 = 3 ms Average Turnaround Time = (27+4+5)/3 = 12 ms Average Waiting Time

= (7+0+2)/3

= 3 msAverage Turnaround Time

= (27+4+5)/3

= 12 ms### Advantages

It helps achieve lesser average waiting time and lesser turnaround time.