# Shortest Job First Scheduling SJF Process Scheduling in operating systems

Shortest Job First Scheduling SJF Process Scheduling in operating systems.

The process with less burst time will always execute first.

Shortest job first scheduling is a non-preemptive scheduling algorithm so processes priority does not matter.

Preferred to minimize waiting time.

Better than First come first served scheduling.

Works only when the processor knows in advance that how much time every process will take to execute on CPU.

Not preferred for interactive systems because required CPU time is not already in knowledge.

Easy to implement in Batch systems because in batch systems CPU time is already known.Â

 Process Burst Time Arrival P1 4 2nd P2 2 3rd P3 8 1st P4 3 4th

## Gantt Chart Diagram of Short Job First Scheduling (SJF)

Let us see the Gantt Chart Diagram of Short Job First Scheduling (SJF).

 Process Waiting Time P1 5 P2 0 P3 9 P4 2

Average Wait Time: (0+2+5+9) / 4 = 4

## How to calculate turn around time?

TurnAroundTime = BurstTime + WaitingTime.

### Advantages of Shortest job first scheduling

Here are the benefits/pros of using Shortest job first process scheduling technique:

• Shortest job first process scheduling reduces the average waiting time over First in First Out algorithm.
• Probably optimal with regard to average turnaround time.
• Shortest job first process scheduling technique gives the lowest average waiting time for a specific set of processes.
• Shortest job first process scheduling is appropriate for the jobs running in batch, where run times are known in advance.
• For the batch system of long-term process scheduling, a burst time estimate can be obtained from the job description.
• Shortest job first process scheduling is frequently used for long term process scheduling.
• For Short-Term Process scheduling, we need to predict the value of the next burst time.

### Disadvantages/Cons Shortest job first scheduling

Here are some drawbacks/cons of Shortest job first process scheduling algorithm:

• Shortest job first process scheduling algorithm may cause very long turnaround times or in other words we can say that it leads to the problem of starvation(aging is the solution to starvation).
• In Shortest job first process scheduling, elapsed time should be recorded, that results in more overhead on the processor.
• Operating system must know the Job completion time earlier, but it is easy to say but its hard to predict earlier.
• Shortest job first process scheduling is often used in a batch system for long term process scheduling.
• Shortest job first process scheduling requires knowledge of how long a process or job will run.
• It is hard to know the length of the upcoming CPU request in shortest job first process scheduling.
• We can’t implement the Shortest job first process scheduling for CPU process scheduling for the short term. It is because there is no specific technique to predict the length of the upcoming CPU burst.
• Shortest job first process scheduling leads to the starvation that does not reduce average turnaround time.

## Non-Preemptive SJF

In non-preemptive process scheduling, once the CPU cycle is allocated to shortest process, then this process holds the resources until it reaches a waiting state or terminated by the operating system.

## Preemptive SJF

In Preemptive SJF Scheduling, when a process comes then operating system put it into the ready queue. A process with shortest burst time starts it execution. Now, If some new process with shortest than shortest time arrives, in this case the current process is removed from execution, and the shorter than shortest process Â will win the CPU cycle.

Output

## SJF implementation in Java

Let us see the SJF implementation in Java.

## Video Lecture

What is the average turnaround time for these processes with the SJF scheduling algorithm?

Gantt Chart for SJF

Process:Â Â  1Â Â Â Â Â Â Â Â Â Â Â Â Â Â  3Â  2

|—————|–|——–|

TimeÂ Â  :Â Â  0Â Â Â Â Â Â  Â Â Â Â Â Â Â Â 8Â  9Â Â Â Â Â Â Â  13

Average Turnaround Time: ( (8-0)+(13-0.4)+(9-1.0) ) / 3 = 9.53
The preemptive version of shortest-job-first-scheduling is called shortest-remaining time first.
The kernel is preemptive?
T/F

Answer: False, the kernel is nonpremptive

Another word for nonpreemptive is ___?