The two simplest scheduling algorithms choose the process that came first, or the one that needs the least CPU time.

First-Come, First-Served (FCFS)

FCFS runs processes in the order of their arrival, using a FIFO ready queue. It is non-preemptive and very simple. Its weakness is the convoy effect: if a long process arrives first, all the short processes behind it must wait, so the average waiting time can be very high. FCFS is fair in the sense of order but unsuitable for time-sharing.

Definition: Shortest Job First. A scheduling algorithm that gives the CPU to the ready process with the smallest next CPU burst, which gives the minimum average waiting time.

Shortest Job First (SJF)

SJF gives the CPU to the process with the smallest next CPU burst. Ties are broken by FCFS. It is provably optimal: it gives the minimum average waiting time for a given set of processes. Its difficulty is that the length of the next CPU burst is not known in advance. It is estimated from past bursts by exponential averaging: τ(n+1) = α t(n) + (1 − α) τ(n), where t(n) is the last actual burst, τ(n) the last estimate, and α between 0 and 1. SJF can cause starvation of long processes if short ones keep arriving.

Preemptive SJF (SRTF)

The preemptive version is Shortest Remaining Time First (SRTF). When a new process arrives with a burst shorter than the remaining time of the running process, the running process is preempted. It gives an even lower average waiting time when processes arrive at different times.

Gantt Charts

A Gantt chart is a bar showing which process runs in each interval. From it the completion time of each process is read, and turnaround and waiting times are computed.

Table 2.5: Example processes (all arrive at time 0)

Process Burst (ms) Order
P1 24 Arrives first
P2 3 Second
P3 3 Third

Example 2.5: FCFS and SJF compared

Problem: For the processes in the table, find the average waiting time under FCFS and under SJF.

Solution:

  1. FCFS order P1, P2, P3. Waiting: P1 = 0, P2 = 24, P3 = 27. Average = 51 / 3 = 17 ms.
  2. SJF order P2, P3, P1. Waiting: P2 = 0, P3 = 3, P1 = 6. Average = 9 / 3 = 3 ms.

Result: FCFS averages 17 ms and SJF only 3 ms, showing the convoy effect of FCFS.

#include <stdio.h>
void avg(const char *name, int b[], int n) {
    int wait = 0, total = 0;
    for (int i = 0; i < n; i++) { total += wait; wait += b[i]; }
    printf("%s: average waiting = %.2f ms\n",
           name, (double)total / n);
}
int main(void) {
    int fcfs[] = {24, 3, 3}, sjf[] = {3, 3, 24};
    avg("FCFS", fcfs, 3);
    avg("SJF ", sjf, 3);
    return 0;
}

Output

FCFS: average waiting = 17.00 ms
SJF : average waiting = 3.00 ms

Common mistakes

  • Forgetting the arrival times when drawing a Gantt chart.
  • Thinking that SJF is practical without estimating burst times.
  • Ignoring the starvation of long jobs under SJF.

Key takeaways

  • FCFS runs in arrival order and suffers from the convoy effect.
  • SJF chooses the shortest next burst and gives the minimum average waiting time.
  • SRTF is preemptive SJF.
  • Burst times are estimated by exponential averaging.

Practice questions

  1. What is the convoy effect?
  2. Why is SJF optimal?
  3. What is SRTF?

Answers to the practice questions

  1. Short processes waiting behind a long one under FCFS, which raises the average waiting time.
  2. Running shorter jobs first minimises the total, and hence the average, waiting time.
  3. The preemptive version of SJF, which preempts when a new process has a shorter remaining time.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II