Shortest Job First (SJF)
In this lesson, you will learn about the Shortest Job First (SJF) scheduling policy.
We'll cover the following
Introduction #
It turns out that a very simple approach solves this problem; in fact, it is an idea stolen from
Examples
Let’s take our example above but with SJF as our scheduling policy.
The figure above shows the results of running A, B, and C. Hopefully the diagram makes it clear why SJF performs much better with regards to average turnaround time. Simply by running B and C before A, SJF reduces average turnaround from 110 seconds to 50 (), more than a factor of two improvement.
ASIDE: PREEMPTIVE SCHEDULERS
In the old days of batch computing, a number of non-preemptive schedulers were developed; such systems would run each job to completion before considering whether to run a new job. Virtually all modern schedulers are preemptive, and quite willing to stop one process from running in order to run another. This implies that the scheduler employs the mechanisms we learned about previously; in particular, the scheduler can perform a context switch, stopping one running process temporarily and resuming (or starting) another.
In fact, given our assumptions about jobs all arriving at the same time, we could prove that SJF is indeed an optimal scheduling algorithm. However, you are in a systems course, not theory or operations research; no proofs are allowed.
Thus we arrive upon a good approach to scheduling with SJF, but our assumptions are still fairly unrealistic. Let’s relax another assumption. In particular, we can target assumption 2, and now assume that jobs can arrive at any time instead of all at once. What problems does this lead to?
(Another pause to think … are you thinking? Come on, you can do it)
Here we can illustrate the problem again with an example. This time, assume A arrives at and needs to run for 100 seconds, whereas B and C arrive at and each needs to run for 10 seconds. With pure SJF, we’d get the schedule seen in the figure below.
As you can see from the figure, even though B and C arrived shortly after A, they still are forced to wait until A has completed, and thus suffer the same convoy problem. Average turnaround time for these three jobs is 103.33 seconds ().What can a scheduler do?
Get hands-on with 1400+ tech skills courses.