WEBVTT

00:00.000 --> 00:06.266
Week 15: The Timer Takes It Back, for the extra-credit exercise 60k_preemption.

00:06.866 --> 00:13.281
A script steps this explainer, it was built with Claude, and its numbers are examples, not the lecture's.

00:13.800 --> 00:17.479
This follows the reading on fork, wait, and the user shell.

00:18.079 --> 00:23.099
Friday, December 4, extra credit, once the day's exercise passes.

00:23.699 --> 00:28.647
You write preempt and its call: a timer tick can take the CPU back.

00:29.247 --> 00:32.414
No solution code here: only the machine's state.

00:32.920 --> 00:38.307
A program that spins and makes no system call never hands the CPU back.

00:38.907 --> 00:40.507
The timer still interrupts it:

00:41.107 --> 00:48.398
the hart, the processor, traps from the program's user mode into usertrap, in the kernel's supervisor mode.

00:48.920 --> 00:52.478
usertrap, given, knows the trap is a timer tick.

00:53.078 --> 00:55.581
Then one box is yours, drawn empty.

00:56.181 --> 01:05.048
From it, the CPU goes back to the program, or to proc_yield, and the scheduler picks the next Runnable process, one ready to run.

01:05.560 --> 01:10.156
proc_yield, given, marks the process Runnable and switches to the scheduler.

01:10.756 --> 01:17.613
QUANTUM, 2, is the time slice, in ticks: how long one turn on the CPU is meant to last.

01:18.120 --> 01:28.570
The stepper's example: pid 26 forked pids 27, 28 and 29 before the first tick, then all four spin without another system call.

01:29.170 --> 01:32.765
Each has a slice counter, and every counter reads 0.

01:33.280 --> 01:37.087
The stepper opens on the reference, the course's finished kernel.

01:37.687 --> 01:42.631
Pid 26, in slot 0, is Running: it holds the CPU.

01:43.231 --> 01:44.927
The other three are Runnable.

01:46.440 --> 01:50.883
T1, a timer tick, interrupts pid 26 in user mode.

01:51.483 --> 01:54.779
Its counter reads 1, and it keeps the CPU.

01:55.379 --> 01:58.056
The strip on top shows who ran at each tick.

01:59.560 --> 02:02.077
T2 interrupts pid 26 again.

02:02.677 --> 02:06.623
Its counter reads 0, and it has given up the CPU.

02:07.623 --> 02:09.395
A bar in the strip marks that.

02:09.995 --> 02:14.644
Pid 26 is Runnable, and the scheduler holds the CPU.

02:16.160 --> 02:24.415
Round robin, which takes the slots in turn, picked pid 27, in slot 1. At T3 its counter reads 1.

02:25.415 --> 02:30.833
At T4 it reads 0, and pid 27 has given up the CPU.

02:32.360 --> 02:37.241
Then pid 28, in slot 2: at T5 its counter reads 1.

02:38.241 --> 02:43.453
At T6 it reads 0, and pid 28 has given up the CPU.

02:44.960 --> 02:49.728
Pid 29, the last in use: at T7 its counter reads 1.

02:50.728 --> 02:54.087
At T8 it reads 0. All four are Runnable.

02:54.687 --> 02:59.330
Who runs at T9: 26, 27, or 29 again?

03:02.360 --> 03:04.563
Pid 26, in slot 0.

03:05.163 --> 03:11.155
Slots 4 to 63 are Unused, so round robin came around to slot 0.

03:11.755 --> 03:15.861
T9 interrupts pid 26, and its counter reads 1.

03:17.360 --> 03:21.371
At T10, pid 26 has given up the CPU again.

03:21.971 --> 03:26.941
Its second turn, T9 and T10, is two ticks long, like its first.

03:28.440 --> 03:30.807
T11: pid 27 again.

03:31.807 --> 03:34.967
T12: its turn ends. The order repeats.

03:35.480 --> 03:39.835
Under the drawing: what the course's test printed, on its own programs.

03:40.435 --> 03:46.045
The test prints: [ok] the spinning child was preempted, so the worker beside it finished

03:46.560 --> 03:49.409
Now the skeleton, the kernel as you receive it.

03:50.409 --> 03:53.821
T1 interrupts pid 26, as on the reference.

03:54.421 --> 03:59.547
But every counter still reads 0, and pid 26 keeps the CPU.

04:01.080 --> 04:08.511
T4: pid 26 keeps the CPU. The ticks reach the kernel, but every counter still reads 0.

04:09.511 --> 04:14.289
By T8, pids 27, 28 and 29 have never run.

04:15.800 --> 04:20.642
At T10 the steps stop: pid 26 still holds the CPU.

04:21.242 --> 04:26.405
The test prints: [fail] hog never finished: its spinning child kept the CPU

04:26.920 --> 04:33.715
The given program rr makes turns visible under cargo run, which boots the kernel you type into.

04:34.315 --> 04:41.337
It forks three children, and each prints its own letter, A, B or C, spinning on the clock between letters.

04:41.840 --> 04:46.735
These keys were typed into cargo run, under four kernels: run rr.

04:47.335 --> 04:49.947
Each line is one of three recorded runs.

04:50.547 --> 04:57.399
In the skeleton's line, the letters come one child at a time; in the other three, the children take turns.

04:57.920 --> 05:01.562
Two pieces are yours: the body of preempt, and its call.

05:02.162 --> 05:12.923
When both are done, a process that keeps running gives up the CPU after QUANTUM ticks of every turn, stays Runnable, and runs again when its turn comes around.

05:13.440 --> 05:15.594
The test runs programs of its own.

05:16.194 --> 05:22.911
First, a spinning child beside a worker, which can finish only if the spinner loses the CPU.

05:23.440 --> 05:31.957
Then three spinners, A, B and C: the test prints each slice as its process and its ticks, and checks their order and length.

05:32.557 --> 05:36.704
It stops at its first [fail] line, and OSLINGS:FAIL follows.

05:37.200 --> 05:45.243
For the reference, the schedule line reads three rounds of A:2 B:2 C:2, and the test prints four [ok] lines.

05:45.843 --> 05:51.265
For the skeleton, before you write anything, the test prints: [fail] hog never finished.

05:51.800 --> 05:56.521
Kernels A and B, two other broken kernels, pass the first two checks.

05:57.121 --> 06:02.014
For kernel A, the schedule reads three rounds of A:3 B:3 C:3,

06:02.614 --> 06:08.748
and the test prints: [fail] slice 1 (process A) lasted 3 ticks, but QUANTUM is 2.

06:09.280 --> 06:17.376
For kernel B, the schedule reads one round of A:2 B:2 C:2, then two rounds of A:1 B:1 C:1,

06:17.976 --> 06:24.013
and the test prints: [fail] slice 4 (process A) lasted 1 tick, but QUANTUM is 2.

06:24.520 --> 06:33.991
A tick reaches the kernel whether or not the running process is willing to stop; with 60k done, that tick can take the CPU back.

06:34.591 --> 06:37.147
Next: Friday's session, December 4.
