All Seven Algorithms on One Problem
Chapter Fifty-Four
Syllabus topic Module 1, "CPU Scheduling - Scheduling Criteria; Scheduling Algorithms"
Pages 210 to 213 of 452
In one line
The same four processes take seven different amounts of waiting depending on nothing but the rule used to choose between them.
The problem
Four processes, with arrivals, bursts and priorities. A smaller priority number is a higher priority.
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
The total work is 16 units and nothing waits for a device, so every algorithm finishes at time 16 and every one keeps the processor busy from 0 to 16. Utilisation and throughput are therefore identical for all seven, which is worth saying before the table: the only things that change are the waiting, the turnaround, the response and the number of switches.
Each schedule below is shown as a chart on one line: the process, and the interval it held the processor.
The seven, worked
First come first served
Schedule: first come first served
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-7] P2[7-11] P3[11-12] P4[12-16]
Average waiting time: 19 / 4 = 4.75
Average turnaround time: 35 / 4 = 8.75
Average response time: 19 / 4 = 4.75
Context switches: 3
Shortest job first
Schedule: shortest job first
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-7] P3[7-8] P2[8-12] P4[12-16]
Average waiting time: 16 / 4 = 4
Average turnaround time: 32 / 4 = 8
Average response time: 16 / 4 = 4
Context switches: 3
Shortest remaining time first
Schedule: shortest remaining time first
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-2] P2[2-4] P3[4-5] P2[5-7] P4[7-11] P1[11-16]
Average waiting time: 12 / 4 = 3
Average turnaround time: 28 / 4 = 7
Average response time: 2 / 4 = 0.50
Context switches: 5
Priority
Schedule: priority
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-7] P2[7-11] P4[11-15] P3[15-16]
Average waiting time: 22 / 4 = 5.50
Average turnaround time: 38 / 4 = 9.50
Average response time: 22 / 4 = 5.50
Context switches: 3
Priority (preemptive)
Schedule: priority (preemptive)
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-2] P2[2-6] P1[6-11] P4[11-15] P3[15-16]
Average waiting time: 21 / 4 = 5.25
Average turnaround time: 37 / 4 = 9.25
Average response time: 17 / 4 = 4.25
Context switches: 4
Round robin, quantum 2
Schedule: round robin, quantum 2
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
All Seven Algorithms on One Problem
P1[0-2] P2[2-4] P1[4-6] P3[6-7] P2[7-9] P4[9-11] P1[11-13] P4[13-15] P1[15-16]
Average waiting time: 20 / 4 = 5
Average turnaround time: 36 / 4 = 9
Average response time: 6 / 4 = 1.50
Context switches: 8
Round robin, quantum 4
Schedule: round robin, quantum 4
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 7 | 2 |
| P2 | 2 | 4 | 1 |
| P3 | 4 | 1 | 3 |
| P4 | 5 | 4 | 2 |
P1[0-4] P2[4-8] P3[8-9] P1[9-12] P4[12-16]
Average waiting time: 18 / 4 = 4.50
Average turnaround time: 34 / 4 = 8.50
Average response time: 13 / 4 = 3.25
Context switches: 4
The comparison
| Algorithm | Waiting | Turnaround | Response | Switches |
|---|---|---|---|---|
| First come first served | 4.75 | 8.75 | 4.75 | 3 |
| Shortest job first | 4 | 8 | 4 | 3 |
| Shortest remaining time first | 3 | 7 | 0.50 | 5 |
| Priority | 5.50 | 9.50 | 5.50 | 3 |
| Priority (preemptive) | 5.25 | 9.25 | 4.25 | 4 |
| Round robin, quantum 2 | 5 | 9 | 1.50 | 8 |
| Round robin, quantum 4 | 4.50 | 8.50 | 3.25 | 4 |
Six things to read off that table, and each is a possible question.
- Shortest remaining time first wins on waiting time, at 3 units, and it is not close. That
is Chapter forty eight's optimality, applied at every instant rather than only at completions.
- It also wins on response time, at half a unit, because it starts a short newcomer
immediately.
- Round robin at quantum 2 is second on response at 1.50, and it pays for it with eight
context switches, more than twice anybody else's. That is the trade of Chapter fifty one in one line of a table.
- Round robin at quantum 4 is better than at quantum 2 on every measure except response. A
larger quantum means fewer switches and less waiting, and worse response. There is no quantum that is best at everything.
- Priority is the worst on waiting and turnaround here, at 5.50 and 9.50, because the
priorities were not chosen to match the burst lengths. Priority scheduling optimises what the priorities say, and if they say the wrong thing it optimises the wrong thing.
- Preemptive priority beats non preemptive priority on all three times and costs one more
switch. That is the general shape of preemption: better times, more switches.
The ranking, and what it is worth
| Criterion | Best here | Worst here |
|---|---|---|
| Waiting time | shortest remaining time first, 3 | priority, 5.50 |
| Turnaround time | shortest remaining time first, 7 | priority, 9.50 |
| Response time | shortest remaining time first, 0.50 | priority, 5.50 |
| Context switches | first come first served, shortest job first and priority, 3 | round robin at quantum 2, 8 |
And now the sentence that matters more than the table. Shortest remaining time first won everything on this problem and it is not the algorithm any general purpose system uses, for three reasons already given: it needs burst lengths nobody knows (Chapter forty eight), it can starve a long process while it is running (Chapter forty nine), and this problem has no input and output in it at all.
All Seven Algorithms on One Problem
A comparison on one problem ranks the algorithms on that problem. What makes a scheduler good is how it behaves over every workload, including the ones that arrive tomorrow, and that is why the algorithm real systems use is the one that measures rather than assumes: the multilevel feedback queue of Chapter fifty three, which approximates the winner of this table without needing anything it cannot know.
What it does not mean
These averages are not properties of the algorithms. They are properties of this problem under those algorithms. Change one arrival time and the ranking can change.
Fewer context switches is not better by itself. First come first served has three and the worst response time of the non priority algorithms.
Utilisation is not a way to tell these apart. With no input and output in the problem, every algorithm keeps the processor busy for all sixteen units.
Quick revision
- The same four processes give seven different sets of averages. The
total work, the finishing time, utilisation and throughput are identical for all seven.
- Shortest remaining time first gave the lowest waiting, turnaround and response on this
problem.
- Round robin at quantum 2 gave the second best response and eight switches, against
three for the non preemptive algorithms.
- A larger quantum means fewer switches and lower waiting, and worse response.
- Priority was worst here because the priorities did not match the burst lengths: it
optimises what the priorities say.
- Preemption improved all three times and cost one more switch, which is its general shape.
- A comparison on one problem is a ranking on that problem. The winner here is unusable in
practice, and the multilevel feedback queue is what a real system runs.
Test yourself
- Why are utilisation and throughput the same for all seven algorithms here? No process
waits for a device, so the processor is busy from 0 to 16 whatever the order, and all four finish by 16.
- Which algorithm gave the lowest average waiting time, and why? Shortest remaining time
first, because at every instant it runs the process with the least work left, which is the optimal choice for average waiting time.
- Which gave the best response time, and which was second? Shortest remaining time first at
0.50, then round robin at quantum 2 at 1.50.
- What did round robin at quantum 2 pay for its response time? Eight context switches, more
All Seven Algorithms on One Problem
than twice as many as any non preemptive algorithm.
- Why did priority scheduling do worst here? Because the priorities were not related to the
burst lengths. Priority scheduling optimises the order the priorities specify, and here they specified an order that was bad for waiting time.
- Compare preemptive and non preemptive priority on this problem. The preemptive form was
better on waiting, turnaround and response, and cost one more context switch.
- If shortest remaining time first wins, why does no general purpose system use it? It needs
burst lengths that cannot be known, it can starve a long process, and a comparison on one problem with no input and output is not a comparison of workloads. A multilevel feedback queue approximates it using only what can be measured.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.