WEBVTT

00:00.000 --> 00:05.016
Week 15: Exit, Then Wait, for exercise 51k_fork_wait.

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

00:12.560 --> 00:15.563
After the reading on fork, wait, and the user shell:

00:16.163 --> 00:26.241
on Thursday, December 3, in 51k_fork_wait, you write the rest of fork, the call that makes a child process; exit and wait are given.

00:26.841 --> 00:30.008
No solution code here: only the machine's state.

00:30.520 --> 00:32.108
A life in four states.

00:32.708 --> 00:38.807
A process lives in one slot of the process table, and the slot's state says what it is doing.

00:39.407 --> 00:41.851
The scheduler, wait and exit change it.

00:42.451 --> 00:46.312
The scheduler is the kernel's loop that picks which slot runs.

00:46.840 --> 00:51.082
Proc, a slot's record, has two new fields in 51k.

00:51.682 --> 01:00.144
The field parent is the parent link: the slot of the process that forked this one, or none, for a process the kernel started.

01:00.744 --> 01:03.562
xstate holds what the process passed to exit.

01:04.080 --> 01:10.906
The scheduler picks a Runnable slot, one waiting its turn, and it is Running: it holds the CPU.

01:11.506 --> 01:18.962
A running process that has to wait for a child gives the CPU back, through proc_yield, and is Runnable again.

01:19.480 --> 01:29.044
A process that calls exit becomes a Zombie: it never runs again, and keeps its xstate, its pages and its pid, its process ID.

01:29.644 --> 01:35.458
Its parent's wait reaps it, collecting them: the slot is Unused, holding no process.

01:35.960 --> 01:36.865
Waiting.

01:37.465 --> 01:46.925
This is the example program from One Fork, Two Paths, picked up where its parent, pid 8 in slot 0, has written "trunk⏎" and calls wait.

01:47.525 --> 01:51.127
Its child, pid 9 in slot 1, has not run yet.

01:51.640 --> 01:56.738
The child's code writes "sprout⏎", then calls exit with 19.

01:57.338 --> 02:01.791
The parent keeps a status word for each wait, to hold a child's status:

02:02.391 --> 02:06.901
0x1_0FA0 and 0x1_0FA8.

02:07.400 --> 02:09.517
The case called correct, step 1.

02:10.117 --> 02:15.523
The drawing holds slots 0 and 1 of the process table; the rest are Unused.

02:16.123 --> 02:20.773
Slot 1's parent field names slot 0: it is the parent's child.

02:21.280 --> 02:24.138
pid 8 calls wait with its first status word.

02:24.738 --> 02:29.390
wait finds slot 1, its child, still Runnable: it has not exited.

02:29.990 --> 02:35.387
So the parent gives up the CPU, with no answer yet: both slots are Runnable.

02:36.920 --> 02:40.984
The scheduler picks slot 1: the child runs for the first time.

02:41.584 --> 02:46.543
Its write of "sprout⏎" answers 7, while the parent waits, Runnable.

02:48.040 --> 02:50.323
The child calls exit with 19.

02:50.923 --> 02:57.615
Slot 1 is a Zombie, xstate 19: it will never run again, and it still holds its pages.

02:58.215 --> 03:00.709
The CPU goes back to the scheduler.

03:01.240 --> 03:03.310
The parent is still in its first wait.

03:04.310 --> 03:09.122
The scheduler picks slot 0, and that wait goes on: it reaps slot 1.

03:09.722 --> 03:17.833
The first status word reads 13 00 00 00: its first byte, 13, is the child's status in hex.

03:18.360 --> 03:20.780
wait answers 9, the child's pid.

03:21.380 --> 03:25.007
Slot 1 is Unused, and its pages are given back.

03:26.007 --> 03:28.777
The parent calls wait again. What comes back?

03:30.277 --> 03:31.451
Keep your answer.

03:32.960 --> 03:34.473
-1, at once.

03:35.073 --> 03:40.650
No slot names slot 0 as its parent, so the second wait keeps the CPU.

03:41.250 --> 03:44.502
Nothing is written at 0x1_0FA8.

03:46.000 --> 03:51.650
The parent calls exit with 18: the child's status, plus the second wait's answer.

03:52.250 --> 03:55.205
Slot 0 is a Zombie, xstate 18.

03:55.805 --> 04:01.563
The console is a separate run of the course's test, and the test prints OSLINGS:PASS.

04:02.080 --> 04:03.989
Three outcomes, one program.

04:04.589 --> 04:10.663
wait looked at the table three times, and each look found a different table, so each ended differently:

04:11.263 --> 04:15.503
none yet, then 9, the child's pid, then -1, at once.

04:16.000 --> 04:21.748
The given code that made those answers: sys_exit hands the status to exit_current.

04:22.348 --> 04:25.995
sys_wait looks for a Zombie whose parent link names the caller;

04:26.595 --> 04:31.671
if there is none, has_children asks whether any slot still in use names it at all.

04:32.200 --> 04:33.960
When the parent link is missing.

04:34.560 --> 04:40.525
Kernel A, a broken kernel, builds the child as the correct one does, with one difference:

04:41.125 --> 04:43.642
slot 1's parent field names no slot.

04:44.160 --> 04:48.857
Kernel A, step 1: slot 1's parent field is marked no parent.

04:49.457 --> 04:54.853
The first wait answers -1, without giving up the CPU, and writes nothing.

04:55.853 --> 04:58.871
The second answers the same way: -1.

05:00.400 --> 05:07.013
The parent exits with -1: its first status word still reads 00 00 00 00.

05:07.613 --> 05:10.823
Slot 1 is still Runnable: the child never ran.

05:11.823 --> 05:16.675
The test prints: [fail] only the parent ran — fork() must create a second.

05:17.200 --> 05:25.771
For kernel A, the test prints the same first line as for the skeleton, the code you receive, whose fork makes no child at all.

05:26.371 --> 05:28.225
One line, different machines.

05:28.760 --> 05:32.077
You write the rest of fork; exit and wait are given.

05:32.677 --> 05:40.037
wait finds a child only through the child's parent field, so the child your fork makes must name its parent there.

05:40.637 --> 05:44.497
In the correct case, slot 1's parent field names slot 0.

05:45.000 --> 05:50.375
The given test runs three programs of its own: hello, forktest and forks2.

05:50.975 --> 05:56.776
It stops at the first check that fails and prints its [fail] message, then OSLINGS:FAIL.

05:57.376 --> 06:00.234
This page draws only the message's first line.

06:00.760 --> 06:04.397
Check 2: forktest's child runs and prints its own line.

06:04.997 --> 06:11.019
On kernel A, the test prints: [fail] only the parent ran — fork() must create a second.

06:11.619 --> 06:16.458
Look at kernel A's last step: the child is in the table, but never runs.

06:16.960 --> 06:23.615
wait acts on what the table holds when it looks, and it finds a child only through the child's parent link.

06:24.215 --> 06:27.251
Next: the explainer New Program, Same Process.

06:27.851 --> 06:30.283
Then Thursday's session, December 3.
