Introduction

When several processes are ready and there is one CPU, the short-term scheduler decides which runs next. The decision is made over and over, every few milliseconds, so the policy shapes how long jobs take, how responsive the system feels and whether some process can be postponed forever.

Processes alternate between CPU bursts and I/O bursts. Scheduling is about CPU bursts: each scheduling algorithm takes the set of ready processes, each with an arrival time and a burst length, and builds a Gantt chart, a timeline showing who runs when. The chart gives completion, turnaround, waiting and response times.

The scheduling criteria and the preemption rule are used with the algorithms that choose by arrival order, by burst length and by priority: FCFS, SJF, SRTF, HRRN and priority scheduling, with the tie-breaking rules that make the schedule unique. Round robin and multilevel queues follow in Round Robin and Multilevel Queue Scheduling.

Prerequisites

Core concepts

Times and criteria

For a process with arrival time AA, burst time BB, first time on the CPU SS and completion time CC:

QuantityDefinition
Turnaround time (TAT)C−AC - A
Waiting time (WT)TAT−B\text{TAT} - B: total time spent in the ready queue
Response time (RT)S−AS - A: time until the first run
ThroughputProcesses completed per unit time
CPU utilizationFraction of time the CPU does useful work

Waiting time is time in the ready queue only. For a process that also does I/O, waiting time excludes the I/O; in these CPU-burst exercises every process has one burst, so WT=TAT−B\text{WT} = \text{TAT} - B holds exactly. For a non-preemptive algorithm, response time equals waiting time, because a process never waits again once started.

Optimization goals. Maximize utilization and throughput; minimize turnaround, waiting and response time. No algorithm is best on all of them at once.

When the scheduler runs

Scheduling decisions happen when a process (1) switches from running to waiting, (2) switches from running to ready (for example, after a timer interrupt), (3) switches from waiting to ready, or (4) terminates. Non-preemptive scheduling decides only at (1) and (4): a running process keeps the CPU until it blocks or finishes. Preemptive scheduling also decides at (2) and (3): an arrival can take the CPU away from the running process.

The dispatcher performs the switch; its delay is the dispatch latency (the context-switch cost).

Tie-breaking conventions used in the traces

Tie convention used in the traces. (a) When two ready processes are equal on the algorithm's key, the one that arrived earlier goes first; if they also arrived together, the lower process number goes first. (b) In a preemptive algorithm, a newly arrived process preempts the running one only if it is strictly better; on a tie the running process keeps the CPU. (c) Priority numbers: a smaller number means higher priority. (d) Unless a question gives a context-switch cost, switching is free. A stated problem may give different rules; when it does, those rules win.

The algorithms

First-come, first-served (FCFS)

Run processes in arrival order; non-preemptive. A FIFO queue implements it.

  • Simple, no starvation.
  • Convoy effect: one long CPU-bound process holds the CPU while many short or I/O-bound ones wait behind it, so both the CPU and the devices are poorly used. Three jobs arriving at time 0 with bursts 20, 2, 2 in that order wait 0,20,220, 20, 22, average 14; in the order 2, 2, 20 they wait 0,2,40, 2, 4, average 2.

Shortest job first (SJF), non-preemptive

When the CPU is free, pick the ready process with the shortest burst.

  • Optimal for average waiting time among non-preemptive schedules of a fixed set of jobs available together (exchange argument, section "Mathematical explanation").
  • Needs burst lengths in advance, so real systems predict them.
  • Starvation: a long job can wait forever if short jobs keep arriving.

Shortest remaining time first (SRTF), preemptive SJF

At every arrival, compare the new process's burst with the remaining time of the running process; switch if the new one is strictly shorter.

  • Minimizes average waiting time among all schedules when arrivals are known, at the cost of more context switches.
  • Same starvation risk as SJF, stronger.

Priority scheduling

Each process has a priority; the highest priority runs. Non-preemptive priority waits for the CPU to be free; preemptive priority lets a higher-priority arrival preempt. SJF is priority scheduling with priority = predicted burst.

  • Starvation (indefinite blocking) of low-priority processes. The fix is aging: raise a process's priority the longer it waits.

Highest response ratio next (HRRN)

Non-preemptive. When the CPU is free, compute for each ready process

R=W+BB=1+WB,R = \frac{W + B}{B} = 1 + \frac{W}{B},

where WW is the time it has waited so far, and run the one with the largest RR. All jobs start at ratio 1. For equal positive waiting time, shorter jobs have a higher ratio and their ratio grows faster per unit waiting. This balances short-job preference with aging; starvation claims require assumptions about the workload, such as a finite set of finite jobs.

Mathematical explanation

Why SJF minimizes average waiting time

Take jobs all available at time 0, run in some order with bursts b1,b2,…,bnb_1, b_2, \dots, b_n. Job kk waits b1+⋯+bk−1b_1 + \dots + b_{k-1}, so the total waiting time is

∑k=1n(n−k) bk.\sum_{k=1}^{n} (n - k)\, b_k .

The burst in position kk is counted n−kn - k times, so to minimize the sum the largest coefficients must multiply the smallest bursts: sort in increasing order. If two adjacent jobs are out of order (bk>bk+1b_k > b_{k+1}), swapping them changes the total by bk+1−bk<0b_{k+1} - b_k < 0. This exchange argument proves optimality for simultaneous arrivals; with staggered arrivals the preemptive SRTF is the optimal one.

Predicting the next burst: exponential averaging

With tnt_n the measured length of the nn-th burst and τn\tau_n its prediction,

τn+1=α tn+(1−α) τn,0≤α≤1.\tau_{n+1} = \alpha\, t_n + (1 - \alpha)\, \tau_n, \qquad 0 \le \alpha \le 1 .

α=0\alpha = 0 ignores the measured bursts (the prediction stays τ0\tau_0); α=1\alpha = 1 uses only the last burst. Unrolling, the weight of tn−jt_{n-j} is α(1−α)j\alpha (1 - \alpha)^{j}: older bursts count geometrically less. Example with α=0.5\alpha = 0.5, τ0=10\tau_0 = 10 and bursts 6, 4, 6: τ1=8\tau_1 = 8, τ2=6\tau_2 = 6, τ3=6\tau_3 = 6.

Worked examples: one workload, five algorithms

ProcessArrivalBurstPriority
P1073
P2241
P3414
P4542

FCFS

Gantt: P1 0 to 7, P2 7 to 11, P3 11 to 12, P4 12 to 16.

ProcessCTTATWT
P1770
P21195
P31287
P416117

Average WT =19/4=4.75= 19/4 = 4.75; average TAT =35/4=8.75= 35/4 = 8.75.

SJF (non-preemptive)

At 7, P1 finishes and P2 (4), P3 (1) and P4 (4) are ready: P3 runs. At 8, P2 and P4 tie at 4; P2 arrived first. Gantt: P1 0 to 7, P3 7 to 8, P2 8 to 12, P4 12 to 16. WT: P1 0, P2 6, P3 3, P4 7; average 4. Average TAT 8.

SRTF

  • At 2, P2 arrives with 4 against P1's remaining 5: P2 preempts.
  • At 4, P3 arrives with 1 against P2's remaining 2: P3 preempts.
  • At 5, P3 is done; P2 (2 left) beats P4 (4) and P1 (5).
  • At 7, P4 (4) beats P1 (5).

Gantt: P1 0 to 2, P2 2 to 4, P3 4 to 5, P2 5 to 7, P4 7 to 11, P1 11 to 16.

ProcessCTTATWTRT
P1161690
P27510
P35100
P411622

Average WT =3= 3, average TAT =7= 7, average RT =0.5= 0.5. SRTF beats SJF (4) on average waiting time, as it must.

FCFS, SJF and SRTF Gantt charts for the example workload
Figure 1. Gantt charts for the four-process workload: FCFS (average waiting time 4.75), SJF (4) and SRTF (3), with every segment boundary marked.

Priority, non-preemptive and preemptive

Non-preemptive: P1 runs 0 to 7. At 7 the ready set is P2 (priority 1), P3 (4) and P4 (2): P2 runs 7 to 11, then P4 11 to 15, then P3 15 to 16. WT: P1 0, P2 5, P3 11, P4 6; average 5.5.

Preemptive: at 2, P2 (priority 1) preempts P1 (3) and runs to 6. At 6 the best is P4 (2), which runs 6 to 10. Then P1 (3) runs 10 to 15 and P3 (4) runs last, 15 to 16. WT: P1 8, P2 0, P3 11, P4 1; average 5. P3 has the worst waiting time in both: low priority means it waits behind everyone.

HRRN versus SJF

Jobs A (arrival 0, burst 10), B (1, 4) and C (9, 1). A runs 0 to 10. At 10, B has waited 9: RB=(9+4)/4=3.25R_B = (9 + 4)/4 = 3.25; C has waited 1: RC=(1+1)/1=2R_C = (1 + 1)/1 = 2. HRRN runs B (10 to 14) then C (14 to 15). SJF would run C first (10 to 11) and then B (11 to 15). SJF has the lower average waiting time (11/311/3 against 14/314/3), but HRRN rewards B for having waited.

A tie in SRTF

P1 (arrival 0, burst 4) and P2 (arrival 1, burst 3). At time 1, P1 has 3 units left and P2 needs 3: a tie. Under the convention above the running P1 continues: P1 0 to 4, P2 4 to 7. Under the opposite rule (an arrival wins ties) the chart is P1 0 to 1, P2 1 to 4, P1 4 to 7. The average waiting time is 1.5 either way, but the completion times differ, so a question asking for one process's completion time depends on the rule.

Context-switch cost

Run FCFS on the four-process workload with a switch cost of 1 unit between consecutive processes (none at time 0). Gantt: P1 0 to 7, switch, P2 8 to 12, switch, P3 13 to 14, switch, P4 15 to 19. Three switches; the last process finishes at 19 instead of 16, and CPU utilization is 16/19≈84.2%16/19 \approx 84.2\%. WT becomes P1 0, P2 6, P3 9, P4 10.

An idle gap and an arrival at a completion instant

SJF (non-preemptive) on P1 (arrival 0, burst 3), P2 (5, 4), P3 (5, 2), P4 (7, 1).

IntervalRunningReason
0 to 3P1Only process present
3 to 5idleNothing has arrived
5 to 7P3P2 and P3 arrive together; P3 is shorter
7 to 8P4P4 arrives at 7, the instant P3 finishes, so it is in the ready queue when the scheduler chooses
8 to 12P2
ProcessCTTATWT
P1330
P21273
P3720
P4810

Average TAT =13/4=3.25= 13/4 = 3.25, average WT =3/4=0.75= 3/4 = 0.75. The idle interval counts in the schedule length but not in anyone's waiting time: CPU utilization is 10/12≈83.3%10/12 \approx 83.3\%. If P4 were (wrongly) treated as arriving after the decision at 7, P2 would run 7 to 11 and P4 would wait 4 units. SRTF gives the same chart here, because no arrival is shorter than the remaining time of the running process.

CPU and I/O bursts

Two processes arrive at 0. A has CPU 3, I/O 5, CPU 2; B has CPU 4, I/O 2, CPU 1. FCFS, each process has its own I/O device (I/O runs in parallel with the CPU and with other I/O), and a process returning from I/O joins the back of the ready queue.

TimeCPUEvent
0 to 3AA starts I/O at 3, until 8
3 to 7BB starts I/O at 7, until 9
7 to 8idleBoth processes are doing I/O
8 to 10AB returns at 9 and waits
10 to 11B

A finishes at 10 and B at 11. The CPU is busy 3+4+2+1=103 + 4 + 2 + 1 = 10 of 11 units, so utilization is 10/11≈90.9%10/11 \approx 90.9\% and the idle time is 1 unit. Waiting time counts only time spent ready but not running: A waits 0, B waits 3 (0 to 3) plus 1 (9 to 10) =4= 4. TAT minus the total burst does not give WT here, because the I/O time must also be subtracted: B's TAT is 11, and 11−4−2−1=411 - 4 - 2 - 1 = 4.

Important properties and observations

AlgorithmPreemptiveStarvationOptimal forNotes
FCFSNoNoNothing in generalConvoy effect
SJFNoYesAverage WT, simultaneous arrivalsNeeds burst prediction
SRTFYesYesAverage WT with arrivalsMany switches
PriorityEitherYes (fix: aging)Whatever the priority encodesSJF is a special case
HRRNNoCombats starvation by aging; finite-workload assumptionCompromiseRatio grows with waiting
  • In SRTF, preemption can occur only at an arrival (or completion); the remaining times of other processes do not change while they wait.
  • FCFS is the special case of priority scheduling where priority is arrival time.
  • With all arrivals at 0, SJF and SRTF give the same schedule.

Common misconceptions.

  • "SJF never starves anyone." It can, under a stream of short jobs.
  • "Waiting time is completion minus arrival." That is turnaround; subtract the burst.
  • "Priority 1 is the lowest." It depends on the system; questions say which, and these notes use smaller = higher.

GATE examination perspective

  • Gantt-chart arithmetic. Draw the chart first, then fill CT, TAT =CT−A= CT - A, WT =TAT−B= TAT - B. From time zero (including initial idle time), total CPU-burst time plus idle time plus charged context-switch overhead equals the last completion time.
  • Ties and preemption. Read whether preemption is allowed and how ties are broken; equal remaining times in SRTF and equal priorities change individual completion times.
  • Idle CPU. If no process has arrived when the CPU frees up, the chart has an idle gap; idle time adds to the schedule length and lowers CPU utilization (busy time / total time), but it is not part of any process's waiting time.
  • Context-switch overhead. Add the stated cost between different processes; check whether it applies at time 0 and after the last job.
  • Starvation and convoy. SJF, SRTF and priority starve; FCFS does not under finite service times; HRRN combats starvation through aging under the stated finite-workload/arrival assumptions; FCFS suffers the convoy effect.
  • Exponential averaging. Apply τn+1=αtn+(1−α)τn\tau_{n+1} = \alpha t_n + (1 - \alpha)\tau_n step by step.

Summary

Sources

  1. A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley: Chapter 5 (5.1 Basic Concepts, 5.2 Scheduling Criteria, 5.3 Scheduling Algorithms).
  2. A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed., Pearson: Section 2.4 (2.4.1 Introduction to Scheduling, 2.4.2 Scheduling in Batch Systems, 2.4.3 Scheduling in Interactive Systems).

Next steps