×

Search anything:

# Proof that Shortest Job First (SJF) Algorithm is Most Optimal

#### Operating System

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Shortest Job First (SJF) Algorithm is a scheduling algorithm where the idea is that the process with the shortest execution time should be processed first. SJF algorithm is the most optimal CPU scheduling algorithm and we have proved this mathematically in this article at OpenGenus.

## Proof

Note the following terminologies:

• There are N processes
• tk is the execution time of process k
• Wk is the waiting time of process k
• Let the set S of N processes be ordered {1, 2, ..., N} in increasing order of execution time.

As process k is executed just after process k-1, we can conclude:

Wk = Wk-1 + tk-1 [Equation 1]

Note waiting time of first process is 0. W1 = 0.

The total waiting time across N processes of set S be:

WS = (W1 + W2 + ... + Wn) / N [Equation 2]

If we replace Equation 1 in Equation 2, we get:

WS = (n-1)t1 + (n-2)t2 + ... + (n-k)tk + ... + tn-1 [Equation 3]

Now, if we select 2 processes randomly say process K-J and K such that the execution time of process K > execution time of process K-J.

If we swap these 2 processes and calculate the new waiting time, we get WS1.

WS1 = (n-1)t1 + ... + (n-K+J)tK + ... + (n-K)tK-J + (n-k)tk + ... + tn-1 [Equation 3]

If we subtract Equation 4 from Equation 3, we get:

WS - WS1 = ((n-K+J)tK-J + (n-K)tK) - ((n-K+J)tK + (n-K)tK-J)

WS - WS1 = (-J) * tK-J + (-J) tK

So, WS - WS1 < 0

Therefore, WS < WS1

Hence, we can conclude that if we swap any two process in this ordered set required by SJF algorithm, the total / average waiting time increases.

Hence, we have mathematically proved that Shortest Job First (SJF) Algorithm is Most Optimal Scheduling algorithm.

## Conclusion

Shortest Job First (SJF) Algorithm is Most Optimal Scheduling algorithm. We have presented the mathematical proof in this article at OpenGenus. You can apply the same concepts to mathematically analyze other scheduling algorithms.

Although SJF algorithm is most optimal, it presents some practical challenges to be implemented in real systems:

• The execution time of processes is not known beforehand. For specific systems, execution time can be predicted.
• Real time systems are dynamic that is the list of processes keep changing. In this case, the process that takes significantly long time to execute will keep getting delayed and may not get executed ever. Such systems are prone to attacks.