WEBVTT

00:00.000 --> 00:05.586
OSlings Practice 34k-36k, Problem 3: Scheduling.

00:06.186 --> 00:11.975
We'll work it out on the practice set's own pages, as the answer key's handwriting appears.

00:12.575 --> 00:16.467
A script reveals it, and this explainer was built with Claude.

00:17.000 --> 00:25.991
Problem 3 draws on one OSlings exercise: 36k_scheduling, from week 9, on Friday, October 23.

00:26.591 --> 00:29.854
You wrote one method there, pick_next for RoundRobin.

00:30.454 --> 00:35.743
Everything else came with the exercise: the trait, the scheduler loop and the test.

00:37.240 --> 00:41.987
Problem 3 opens with the round-robin policy from 36k.

00:42.587 --> 00:45.406
After each turn, the process yields with swtch,

00:46.006 --> 00:48.783
and the scheduler loop marks it Runnable again.

00:49.280 --> 00:52.736
A call to pick_next is handed the state of every slot,

00:53.336 --> 00:55.597
and n is how many slots there are.

00:56.197 --> 01:01.305
The scan takes each offset in turn, from 0 up to, but not including, n.

01:01.800 --> 01:05.979
The slot it looks at is self.next plus the offset, mod n.

01:06.579 --> 01:09.639
So the scan starts at the cursor, self.next,

01:10.239 --> 01:13.576
and wraps around the end of the table, to the first slot.

01:14.080 --> 01:18.367
The call to find stops at the first Runnable slot it reaches, i.

01:18.967 --> 01:25.158
The last map moves the cursor one past i, wrapping the same way, and the call returns i.

01:25.758 --> 01:28.596
If no slot is Runnable, it returns None.

01:29.720 --> 01:35.063
Part (a). With self.next at 0, a 5-slot table starts like this.

01:35.663 --> 01:38.435
The slots count from 0, in the order listed:

01:39.035 --> 01:41.379
A, B, C, an unused slot, then D.

01:41.880 --> 01:45.634
Right after the third turn, B wakes up and becomes Runnable.

01:46.234 --> 01:48.652
Fill in the table for the first 7 turns:

01:49.252 --> 01:55.843
the index a call to pick_next returns, the process that runs, and self.next after the turn.

01:57.360 --> 02:01.583
Turn 1 starts at the cursor, 0, and slot 0 is Runnable:

02:02.183 --> 02:06.160
the scan stops at once, and the call returns index 0.

02:07.160 --> 02:09.660
Slot 0 holds A, so A runs.

02:10.660 --> 02:13.164
Then the cursor moves one past it, to 1.

02:14.680 --> 02:18.831
Turn 2 starts at 1, where B is Sleeping, so the scan skips it.

02:19.431 --> 02:21.798
Slot 2 is Runnable: index 2.

02:22.798 --> 02:25.111
Slot 2 holds C, so C runs.

02:26.111 --> 02:27.924
And the cursor moves to 3.

02:29.440 --> 02:33.675
Turn 3 starts at 3, the unused slot, and the scan skips it.

02:34.275 --> 02:36.780
Slot 4 is Runnable: index 4.

02:37.780 --> 02:40.270
Slot 4 holds D, so D runs.

02:41.270 --> 02:46.255
The cursor moves one past 4, the last slot, and wraps around to 0.

02:47.360 --> 02:49.727
Then B wakes up, and becomes Runnable.

02:50.327 --> 02:53.121
Now every slot but the unused one is Runnable:

02:53.721 --> 02:59.385
each process that ran is Runnable again, since the loop marks it so after its turn.

03:00.880 --> 03:04.634
Turn 4 starts where turn 3 left the cursor, at 0.

03:05.234 --> 03:09.797
Slot 0 is Runnable again, so the call returns index 0.

03:10.797 --> 03:11.991
A runs again.

03:12.991 --> 03:14.612
The cursor moves to 1.

03:16.120 --> 03:22.421
Turn 5 starts at 1, and slot 1 is Runnable now, so the call returns index 1.

03:23.421 --> 03:28.753
Slot 1 holds B, which woke after turn 3: B runs, its first turn.

03:29.753 --> 03:31.373
The cursor moves to 2.

03:32.880 --> 03:37.103
Turn 6 starts at 2, and slot 2 is Runnable: index 2.

03:38.103 --> 03:40.417
Slot 2 holds C, so C runs.

03:41.417 --> 03:43.133
The cursor moves to 3.

03:44.640 --> 03:53.077
Turn 7 is turn 3 again: it starts at 3, the scan skips the unused slot, and the call returns index 4.

03:54.077 --> 03:56.567
Slot 4 holds D, so D runs.

03:57.567 --> 03:59.731
And the cursor wraps around to 0.

04:00.840 --> 04:05.437
Down the column, the processes ran A, C, D, then A, B, C, D.

04:06.037 --> 04:11.072
No call returned None: each process is Runnable again after its turn.

04:11.672 --> 04:17.207
And self.next is always an index, a slot number, never a process letter.

04:18.920 --> 04:26.181
Part (b). The 36k test runs pids 1, 3 and 4, while pid 2 sleeps, for 3 turns each.

04:26.781 --> 04:31.974
It expects them interleaved: 1 3 4, 1 3 4, 1 3 4.

04:32.480 --> 04:40.799
What order do you get if a call to pick_next sets self.next = i, instead of (i + 1) % n? And why?

04:41.399 --> 04:43.980
That changes the line that moves the cursor.

04:45.980 --> 04:49.060
1 1 1, 3 3 3, 4 4 4.

04:49.660 --> 04:54.629
Each process takes all its turns in a row, not interleaved with the others.

04:56.660 --> 05:02.357
With self.next = i, the next scan starts at the process that just ran.

05:03.857 --> 05:07.055
The loop marked it Runnable again, so it is picked again,

05:07.655 --> 05:12.085
until it retires as a Zombie. Only then does the scan move on.

05:14.100 --> 05:18.065
In this test, that is the same order as always scanning from 0,

05:18.665 --> 05:25.561
which also takes pid 1 until it retires, then pid 3, then pid 4. Neither is round robin.

05:26.561 --> 05:29.845
The interleaved order in the test catches both bugs.

05:30.960 --> 05:36.015
Each process does retire, after its 3 turns, so nothing runs forever.

05:36.615 --> 05:41.681
And nothing goes out of bounds: the index i is always a slot in the table.

05:42.281 --> 05:44.881
The bug is only where the next scan starts.

05:46.600 --> 05:51.725
Part (c). Suppose pid 3's code had an infinite loop before its first yield.

05:52.325 --> 05:56.101
What happens to pids 1 and 4, and why? What would fix it?

05:58.100 --> 06:01.352
They starve. Pid 1 got its first turn, and yielded,

06:02.352 --> 06:09.431
but once pid 3 is switched in, neither 1 nor 4 ever runs again. Pid 4 never runs at all.

06:10.560 --> 06:11.340
Why?

06:12.840 --> 06:20.539
Scheduling here is cooperative: the scheduler gets the CPU back only when a process calls swtch to yield.

06:21.139 --> 06:24.680
A process that never yields keeps the CPU forever.

06:26.700 --> 06:32.317
The fix: preemption. A timer interrupt fires periodically and traps into the kernel,

06:33.817 --> 06:38.753
and it forces a switch back to the scheduler, whether or not the process yields.

06:40.280 --> 06:44.311
The mechanism is still swtch; only what triggers it changes.

06:44.911 --> 06:48.227
As in 36k, the policy is still pick_next.

06:49.960 --> 06:51.431
That's Problem 3.

06:52.031 --> 06:55.471
Part (a): the cursor moves one past the slot it takes.

06:56.071 --> 07:02.330
Part (b): with self.next = i, the same process is picked until it retires.

07:02.930 --> 07:08.414
Part (c): without preemption, a process that never yields keeps the CPU.
