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
| Word | What it means |
|---|---|
| Waiting time | the time a process spends in the ready queue |
| Turnaround time | the time from arriving to finishing |
| FCFS | first come first served: run in the order of arrival |
| SJF | shortest job first: run the shortest burst next |
| Round robin | give each process a short turn, in a circle |
| Starvation | a 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
| Process | Burst | Starts at | Waiting time | Turnaround time |
|---|---|---|---|---|
| P1 | 6 | 0 | 0 | 6 |
| P2 | 8 | 6 | 6 | 14 |
| P3 | 7 | 14 | 14 | 21 |
| P4 | 3 | 21 | 21 | 24 |
| Average | 10.25 | 16.25 |
The cells students fill in are left blank in this preview.
Add the four waiting times, divide by four
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
| Process | Burst | Starts at | Waiting time | Turnaround time |
|---|---|---|---|---|
| P4 | 3 | 0 | 0 | 3 |
| P1 | 6 | 3 | 3 | 9 |
| P3 | 7 | 9 | 9 | 16 |
| P2 | 8 | 16 | 16 | 24 |
| Average | 7.0 | 13.0 |
The cells students fill in are left blank in this preview.
The same sum, with the bursts sorted first
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
| Process | Burst | First turn at | Finishes at | Waiting time |
|---|---|---|---|---|
| P1 | 6 | 0 | 17 | 11 |
| P2 | 8 | 4 | 21 | 13 |
| P3 | 7 | 8 | 24 | 17 |
| P4 | 3 | 12 | 15 | 12 |
| Average | 13.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
niceShown 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
nice -n 5 niceShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.