Operating Systems Module 3: CPU scheduling Lesson 3.6 15 min

Project: beat the scheduler

Four scheduling challenges to solve with the simulator: make two algorithms tie, make round robin lose, starve a job and rescue it, and keep an interactive job at the top of MLFQ.

You have read about five schedulers and watched them run. Now it is your turn to drive. Each challenge below is a simulator with a goal: a condition about the runs, written in the same terms as the fields panel. The lesson’s inputs do not reach it. Open Try it (it is already open), change the inputs, press Run, and the goal box turns green when you have done it.

The goal box also shows the current values of everything the goal mentions, so after each run you can see how close you are. If you get stuck, each challenge has a hint underneath. Your progress on a goal is remembered in this browser.

A few reminders for editing the processes box: one process per line, NAME arrive A burst B, optionally priority P (smaller is more important) and io AT FOR (after AT ms of CPU, an I/O of FOR ms).

Challenge 1: make FCFS as good as SJF

SJF gives the best possible average wait (Lesson 3.2), and FCFS is often far worse. But not always. Change the processes so that FCFS and SJF give exactly the same average wait. You may not just delete processes until one is left: keep all three.

Challenge 1: make FCFS as good as SJF

  1. A, B and C arrive A, B and C arrive and join the ready queue. Ready queue: A → B → C.
    Time
    0 ms
    Running
    idle
    Ready queue
    A → B → C
    A
    8 of 8 ms left
    B
    4 of 4 ms left
    C
    1 of 1 ms left
  2. A runs The scheduler picks A, because it is first in the ready queue.
    Time
    0 ms
    Running
    A
    Ready queue
    B → C
    A
    8 of 8 ms left
    B
    4 of 4 ms left
    C
    1 of 1 ms left
  3. A finishes, B runs A finishes at 8 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 8 ms. The scheduler picks B, because it is first in the ready queue.
    Time
    8 ms
    Running
    B
    Ready queue
    C
    A
    done at 8: waited 0, turnaround 8
    B
    4 of 4 ms left
    C
    1 of 1 ms left
  4. B finishes, C runs B finishes at 12 ms. It waited 8 ms in all, and its turnaround (arrival to finish) was 12 ms. The scheduler picks C, because it is the only process ready.
    Time
    12 ms
    Running
    C
    Ready queue
    empty
    A
    done at 8: waited 0, turnaround 8
    B
    done at 12: waited 8, turnaround 12
    C
    1 of 1 ms left
  5. C finishes C finishes at 13 ms. It waited 12 ms in all, and its turnaround (arrival to finish) was 13 ms. Every process is done at 13 ms. The average wait is 6.67 ms.
    Time
    13 ms
    Running
    idle
    Ready queue
    empty
    A
    done at 8: waited 0, turnaround 8
    B
    done at 12: waited 8, turnaround 12
    C
    done at 13: waited 12, turnaround 13
    Average wait
    6.67 ms
    Average turnaround
    11 ms
    Average response
    6.67 ms

Hint. FCFS runs jobs in arrival order, and when several arrive together, in the order they are listed. When does that order happen to be the shortest first? There is a second way, too: change the arrival times so that the CPU is never choosing between two ready jobs.

What it shows. FCFS is only bad when long jobs happen to come first. The scheduler’s choice matters only when there is a choice to make, which is when several jobs are ready at once.

Challenge 2: make round robin lose

Round robin usually beats FCFS on average wait, because short jobs get out early (Lesson 3.3). With these three jobs and a 4 ms slice, it does. Find a slice length that makes round robin’s average wait worse than FCFS’s, on the same jobs. Change only the round robin slice.

Challenge 2: make round robin lose to FCFS

  1. A, B and C arrive A, B and C arrive and join the ready queue. Ready queue: A → B → C.
    Time
    0 ms
    Running
    idle
    Ready queue
    A → B → C
    A
    9 of 9 ms left
    B
    4 of 4 ms left
    C
    4 of 4 ms left
  2. A runs The scheduler picks A, because it is at the front of the ready queue, and it may run for up to 4 ms.
    Time
    0 ms
    Running
    A
    Ready queue
    B → C
    A
    9 of 9 ms left
    B
    4 of 4 ms left
    C
    4 of 4 ms left
  3. Slice over: A → B A has used its whole 4 ms slice. The timer interrupt gives the CPU back to the kernel, and A goes to the back of the ready queue. The scheduler picks B, because it is at the front of the ready queue, and it may run for up to 4 ms.
    Time
    4 ms
    Running
    B
    Ready queue
    C → A
    A
    5 of 9 ms left
    B
    4 of 4 ms left
    C
    4 of 4 ms left
  4. B finishes, C runs B finishes at 8 ms. It waited 4 ms in all, and its turnaround (arrival to finish) was 8 ms. The scheduler picks C, because it is at the front of the ready queue, and it may run for up to 4 ms.
    Time
    8 ms
    Running
    C
    Ready queue
    A
    A
    5 of 9 ms left
    B
    done at 8: waited 4, turnaround 8
    C
    4 of 4 ms left
  5. C finishes, A runs C finishes at 12 ms. It waited 8 ms in all, and its turnaround (arrival to finish) was 12 ms. The scheduler picks A, because it is the only process ready.
    Time
    12 ms
    Running
    A
    Ready queue
    empty
    A
    5 of 9 ms left
    B
    done at 8: waited 4, turnaround 8
    C
    done at 12: waited 8, turnaround 12
  6. A finishes A finishes at 17 ms. It waited 8 ms in all, and its turnaround (arrival to finish) was 17 ms. Every process is done at 17 ms. The average wait is 6.67 ms.
    Time
    17 ms
    Running
    idle
    Ready queue
    empty
    A
    done at 17: waited 8, turnaround 17
    B
    done at 8: waited 4, turnaround 8
    C
    done at 12: waited 8, turnaround 12
    Average wait
    6.67 ms
    Average turnaround
    12.33 ms
    Average response
    4 ms

Hint. Try very small slices. Watch when B and C finish in each pane: when every job keeps getting interrupted, all of them finish late.

What it shows. Round robin helps when short jobs can finish within a slice or two. When the slice is tiny, every job is interleaved with every other, nobody finishes early, and the average wait grows, even before you add the cost of the switches themselves.

Challenge 3: starve a job, then rescue it

This challenge has two runs of the same processes under priority scheduling. The top pane uses your aging setting. The bottom pane is always the same processes with no aging. The goal has two parts:

  1. Without aging, job L must wait at least 20 ms: make it starve, by adding important jobs that keep arriving.
  2. With your aging setting, L must wait 10 ms or less: rescue it.

Challenge 3: starve L without aging, rescue it with aging

  1. L and H1 arrive L and H1 arrive and join the ready queue. Ready queue: H1 → L.
    Time
    0 ms
    Running
    idle
    Ready queue
    H1 → L
    L
    3 of 3 ms left, priority 5
    H1
    4 of 4 ms left, priority 1
    H2
    arrives at 3
  2. H1 runs The scheduler picks H1, because its priority, 1, is the most important (a smaller number wins).
    Time
    0 ms
    Running
    H1
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    4 of 4 ms left, priority 1
    H2
    arrives at 3
  3. H2 arrives H2 arrives and joins the ready queue. Ready queue: H2 → L.
    Time
    3 ms
    Running
    H1
    Ready queue
    H2 → L
    L
    3 of 3 ms left, priority 5
    H1
    1 of 4 ms left, priority 1
    H2
    4 of 4 ms left, priority 1
  4. H1 finishes, H2 runs H1 finishes at 4 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 4 ms. The scheduler picks H2, because its priority, 1, is the most important (a smaller number wins).
    Time
    4 ms
    Running
    H2
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    4 of 4 ms left, priority 1
  5. H2 finishes, L runs H2 finishes at 8 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 5 ms. The scheduler picks L, because it is the only process ready.
    Time
    8 ms
    Running
    L
    Ready queue
    empty
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
  6. L finishes L finishes at 11 ms. It waited 8 ms in all, and its turnaround (arrival to finish) was 11 ms. Every process is done at 11 ms. The average wait is 3 ms.
    Time
    11 ms
    Running
    idle
    Ready queue
    empty
    L
    done at 11: waited 8, turnaround 11
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    Average wait
    3 ms
    Average turnaround
    6.67 ms
    Average response
    3 ms

Hint. For part 1, add more priority-1 jobs, each arriving before the previous one finishes, so the CPU always has an important job ready. For part 2, set Priority aging every to a small number of milliseconds and run again.

What it shows. Starvation is not a bug in any one line: it comes from a steady supply of more important work. Aging turns “how long have you waited?” into part of the priority, so every job gets a turn in the end.

Challenge 4: stay at the top of MLFQ

Edit is an interactive job: it computes for 3 ms, then waits 3 ms for I/O, and repeats. Long is a CPU-bound job. With these levels, Edit gets pushed down to level 2. Make Edit stay at the top level (level 1) for the whole run. You may change the levels, or Edit’s behaviour.

Challenge 4: keep Edit at the top level

  1. Long arrives Long arrives and joins the ready queue at the top level. Ready queue: Long.
    Time
    0 ms
    Running
    idle
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    20 of 20 ms left, level 1
    Edit
    arrives at 1
  2. Long runs The scheduler picks Long, because it is the only process ready.
    Time
    0 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    20 of 20 ms left, level 1
    Edit
    arrives at 1
  3. Edit arrives Edit arrives and joins the ready queue at the top level. Ready queue: Edit.
    Time
    1 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    19 of 20 ms left, level 1
    Edit
    6 of 6 ms left, level 1
  4. Slice over: Long → Edit Long has used its whole 2 ms slice. The timer interrupt gives the CPU back to the kernel, and Long moves down to level 2. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    2 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    18 of 20 ms left, level 2
    Edit
    6 of 6 ms left, level 1
  5. Slice over: Edit → Long Edit has used its whole 2 ms slice. The timer interrupt gives the CPU back to the kernel, and Edit moves down to level 2. The scheduler picks Long, because it is first in the highest level that has a job (level 2, slice 4 ms).
    Time
    4 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    18 of 20 ms left, level 2
    Edit
    4 of 6 ms left, level 2
  6. Slice over: Long → Edit Long has used its whole 4 ms slice. The timer interrupt gives the CPU back to the kernel, and Long moves down to level 3. The scheduler picks Edit, because it is first in the highest level that has a job (level 2, slice 4 ms).
    Time
    8 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    14 of 20 ms left, level 3
    Edit
    4 of 6 ms left, level 2
  7. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    9 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 12
    Long
    14 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 2, in I/O until 12
  8. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    12 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    11 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 2
  9. Edit preempts Long Edit is at a higher level than Long, so Long is preempted. The scheduler picks Edit, because it is first in the highest level that has a job (level 2, slice 4 ms).
    Time
    12 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    11 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 2
  10. Edit finishes, Long runs Edit finishes at 15 ms. It waited 5 ms in all, and its turnaround (arrival to finish) was 14 ms. The scheduler picks Long, because it is the only process ready.
    Time
    15 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    11 of 20 ms left, level 3
    Edit
    done at 15: waited 5, turnaround 14
  11. Long finishes Long finishes at 26 ms. It waited 6 ms in all, and its turnaround (arrival to finish) was 26 ms. Every process is done at 26 ms. The average wait is 5.5 ms.
    Time
    26 ms
    Running
    idle
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    done at 26: waited 6, turnaround 26
    Edit
    done at 15: waited 5, turnaround 14
    Average wait
    5.5 ms
    Average turnaround
    20 ms
    Average response
    0.5 ms

Hint. A job moves down when it uses up a whole slice. Compare Edit’s bursts with the top level’s slice. Either make the top slice longer than Edit’s bursts, or make Edit do its I/O sooner, for example Edit arrive 1 burst 6 io 1 3 io 2 3 io 3 3 io 4 3 io 5 3.

What it shows. MLFQ judges a job by one thing: does it use whole slices? The size of the top slice decides what counts as “interactive”. Set it too small, and real interactive programs get treated as CPU hogs.

You beat the scheduler

Look back at what you changed. In every challenge the scheduler followed its rules perfectly; what decided the outcome was the workload and the settings. That is the practical lesson of this module: no scheduler is best for everything, and when a system feels slow, the question is which rule is meeting which workload.

Quick check

Quick check

  1. Under first come, first served, when does the order of arrival stop mattering for the average wait?
  2. Three jobs of equal length arrive together. Compared with FCFS, what does round robin with a tiny slice do to the average wait?
  3. A job always asks for the disk before its first slice runs out. Where does MLFQ keep it?

Review: what you learned earlier

Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.

Spaced review

This review picks questions for you, so it needs JavaScript.

Module 3 recap

You can now judge a schedule by its waiting, turnaround and response times, its throughput and its fairness, and you know these pull against each other. You have seen first come, first served and its convoy effect; shortest job first, the best on average waiting time but impossible to run exactly; round robin and the trade-off in choosing a slice; priorities, starvation and aging; and the multi-level feedback queue that learns which jobs are interactive. You know how Linux shares the CPU fairly by virtual runtime, weighs tasks by their nice values, and keeps every core busy with per-core queues and work stealing.

Next, Module 4: once several threads really do run at the same time, they can trip over each other’s data. You will see how a simple count++ can lose updates, and how locks, atomic instructions and semaphores keep shared data safe.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion