WEBVTT

00:00.000 --> 00:05.246
Week 15: One Fork, Two Paths, for exercise 51k_fork_wait.

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

00:12.760 --> 00:17.463
This follows the Week 15 reading on fork, wait, and the user shell.

00:18.063 --> 00:25.653
On Thursday, December 3, you write the rest of sys_fork, the kernel's side of fork: one call, two processes.

00:26.253 --> 00:29.419
No solution code here: only the machine's state.

00:29.920 --> 00:35.486
The example program on this page writes a line, calls fork once, and then branches.

00:36.086 --> 00:43.389
fork is the system call that makes a second process, the child, running the program of the caller, the parent.

00:43.920 --> 00:48.655
Its first instruction takes 64 bytes off sp, the stack pointer:

00:49.255 --> 00:50.947
room for two status words,

00:51.547 --> 00:56.964
which the parent's code hands to wait, the call that collects a child's exit status.

00:57.480 --> 01:02.770
fork is the ecall at 0x16, the instruction that traps into the kernel.

01:03.370 --> 01:07.257
Right after it, at 0x1A, the branch reads a0.

01:07.760 --> 01:15.869
When a0 is not zero, the branch jumps to the code labeled parent, which writes trunk, waits twice and exits.

01:16.469 --> 01:22.441
When a0 is zero, it falls through to the child's code, which writes sprout and exits.

01:22.960 --> 01:28.027
The stepper draws this program on the reference kernel, the case with nothing broken.

01:28.627 --> 01:32.097
On the left, the program, with a marker for each process;

01:32.697 --> 01:38.145
on the right, the CPU, two slots of the process table, and the calls so far.

01:38.680 --> 01:46.772
pid 8, its process id, is in slot 0 of the kernel's process table, and Running: it holds the CPU.

01:47.372 --> 01:48.746
Its write answered 5,

01:49.346 --> 01:55.036
and its saved a0, the copy of a0 the kernel keeps for it, holds 5.

01:56.560 --> 02:02.702
The ecall at 0x16 has taken the CPU into supervisor mode, the kernel's.

02:03.302 --> 02:05.594
a7 holds 1, fork's number.

02:06.194 --> 02:09.365
Slot 1 is Unused: no process is in it.

02:11.380 --> 02:13.736
fork has answered: two processes.

02:14.336 --> 02:17.942
pid 8 is Running; its saved a0 holds 9.

02:18.542 --> 02:24.794
pid 9, in slot 1, is Runnable, ready to run; its saved a0 holds 0.

02:25.394 --> 02:27.366
Both will resume at 0x1A.

02:28.880 --> 02:35.455
pid 8 resumed with 9 in a0, and the branch jumped to the parent's code, which wrote trunk.

02:36.055 --> 02:37.755
Its wait has no answer yet.

02:38.355 --> 02:43.677
pid 8 is Runnable, and the scheduler, which picks who runs, holds the CPU.

02:45.200 --> 02:54.179
Now pid 9, in slot 1, is Running in user mode, where programs run. It resumed at 0x1A with 0 in a0.

02:54.779 --> 02:58.287
The branch fell through to the child's code, which wrote sprout.

02:58.800 --> 03:00.721
Under the drawing is the console.

03:01.321 --> 03:09.610
For the reference, the test, running its own programs, prints [ok] fork / wait / exit and the scheduler all work,

03:10.210 --> 03:11.745
then OSLINGS:PASS.

03:12.280 --> 03:15.892
Now a broken case: the skeleton, the code you receive.

03:16.492 --> 03:20.405
The same program is at the same ecall, at 0x16.

03:21.405 --> 03:26.555
fork has answered, and slot 1 is still Unused: there is no second process.

03:27.080 --> 03:32.576
fork answered -1, and pid 8's saved a0 holds it, every bit set.

03:33.176 --> 03:35.959
pid 8 will resume at 0x1A, the branch.

03:36.559 --> 03:38.723
What does the program print after seed?

03:42.220 --> 03:48.343
trunk. With a0 at -1, not zero, the branch jumped to the parent's code.

03:48.943 --> 03:52.923
Both waits answered -1, and exit got -1.

03:53.523 --> 03:56.583
The run is over, and no process printed sprout.

03:57.080 --> 04:03.154
For the skeleton, the test prints: [fail] only the parent ran — fork() must create a second.

04:03.754 --> 04:07.113
Its hint lines are not shown; then OSLINGS:FAIL.

04:07.640 --> 04:11.277
This table is the reference's whole run, turn by turn.

04:11.877 --> 04:23.421
Turn 1 is pid 8's: it writes seed, fork answers 9, it writes trunk, and its wait finds no exited child and gives up the CPU. Both slots end Runnable.

04:23.920 --> 04:32.129
Turn 2 is pid 9's: it runs from 0x1A with 0 in a0, writes sprout, and exits with 19.

04:32.729 --> 04:37.905
pid 9 ends a Zombie: a process that has exited and never runs again.

04:38.400 --> 04:48.402
Turn 3 is pid 8's again. Its first wait reaps pid 9, collecting status 19, and answers 9; then it calls wait again, then exit.

04:49.002 --> 04:53.277
pid 8's slot ends a Zombie, and pid 9's slot is Unused.

04:53.800 --> 05:00.144
The skeleton and two other broken sys_forks, kernels A and B, leave three different machines.

05:00.744 --> 05:03.847
The skeleton's machine is the stepper's second case.

05:04.447 --> 05:11.627
Kernel A is drawn on the explainer Exit, Then Wait; kernel B is shown by its recorded line only.

05:12.160 --> 05:15.006
The test prints the same first line for all three:

05:15.606 --> 05:19.296
[fail] only the parent ran — fork() must create a second.

05:19.896 --> 05:21.656
Its hint lines are not shown.

05:22.160 --> 05:26.403
For kernel C, a fourth broken sys_fork, the test prints less.

05:27.003 --> 05:31.569
Its last line is the one that announces forktest, and nothing follows it:

05:32.169 --> 05:36.510
the harness, the test's runner, stops the run at its 10-second limit.

05:37.040 --> 05:40.707
You write the rest of sys_fork, in syscall.rs.

05:41.307 --> 05:49.227
When it returns, a second process exists and is Runnable, running a copy of the caller's program, from the same instruction.

05:49.760 --> 05:54.771
The test runs three programs of its own: hello, forktest and forks2.

05:55.371 --> 06:01.969
It stops at the first check that fails: that check's [fail] line, its hint lines, then OSLINGS:FAIL.

06:02.569 --> 06:04.903
A kernel that hangs prints nothing more.

06:05.400 --> 06:13.609
On forktest's row, the test prints the fail line for the skeleton and kernels A and B, and nothing more for kernel C.

06:14.209 --> 06:21.947
When all three of its programs pass, the test prints [ok] fork / wait / exit and the scheduler all work.

06:22.480 --> 06:27.951
After one fork, two processes resume at the same instruction, in the same program.

06:28.551 --> 06:31.024
Next: the explainer Exit, Then Wait.

06:31.624 --> 06:34.055
Then Thursday's session, December 3.
