Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

CPU scheduling: FCFS, SJF and round robin

After this lesson: I can compare first come first served, shortest job first and round robin by their average waiting time.

55 min lesson4 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Which process should the processor run next?

A CPU scheduling algorithm is the rule that picks which waiting process gets the processor next. Different rules give different waiting times, so the rule changes how fast a computer feels.

Three simple rules explain most of the idea: first come first served, shortest job first and round robin.

By the end of this lesson

Note

I can compare first come first served, shortest job first and round robin by their average waiting time.

Key words

WordWhat it means
Waiting timethe time a process spends in the ready queue
Turnaround timethe time from arriving to finishing
FCFSfirst come first served: run in the order of arrival
SJFshortest job first: run the shortest burst next
Round robingive each process a short turn, in a circle
Starvationa process that waits far too long, or for ever

The cells students fill in are left blank in this preview.

First come first served runs in arrival order

The simplest rule is first come first served. The process that arrived first runs until it finishes. Then the next one runs.

Waiting time is easy to read off. A process waits for every burst in front of it. Four processes arrive together and need 6, 8, 7 and 3 time units.

Worked example: first come first served

ProcessBurstStarts atWaiting timeTurnaround time
P16006
P286614
P37141421
P43212124
Average10.2516.25

The cells students fill in are left blank in this preview.

Add the four waiting times, divide by four

Command
python3 -c "b=[6,8,7,3]; print(sum(sum(b[:i]) for i in range(4)) / 4)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Shortest job first gives the smallest average wait

In shortest job first the scheduler always runs the process with the shortest burst. Short processes no longer queue behind long ones.

Run the same four processes in the order 3, 6, 7, 8. The long ones go last, so fewer processes wait for them.

Worked example: shortest job first

ProcessBurstStarts atWaiting timeTurnaround time
P43003
P16339
P379916
P28161624
Average7.013.0

The cells students fill in are left blank in this preview.

The same sum, with the bursts sorted first

Command
python3 -c "b=sorted([6,8,7,3]); print(sum(sum(b[:i]) for i in range(4)) / 4)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think shortest job first is the best rule, so every system should use it. It does give the smallest average wait. But a system cannot know how long a process will run, and a long process may starve while short ones keep arriving. Use it as a yardstick, not as the whole answer.

Round robin trades average wait for fairness

In round robin each process gets a turn of at most one time slice, then goes to the back of the queue. Nobody waits long for a first turn.

Here the slice is 4. The average wait is longer than in first come first served. The gain is that every process starts within 12 units, so a user at a keyboard sees an answer soon.

Worked example: round robin, slice of 4

ProcessBurstFirst turn atFinishes atWaiting time
P1601711
P2842113
P3782417
P43121512
Average13.25

The cells students fill in are left blank in this preview.

Real systems mix these ideas and add priorities

Real systems do not pick one rule. They give each process a share of the processor and let a user ask for less. On Linux the request is called niceness: a higher number means a nicer process, one that lets others go first.

A new shell starts at niceness 0. The nice command prints its own niceness, and -n 5 raises it by five.

Print the niceness of this shell's commands

Command
nice

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Print it again for a nicer command

Command
nice -n 5 nice

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.