WEBVTT

00:00.000 --> 00:05.674
Week 15: New Program, Same Process, for exercise 52k_userland.

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

00:13.200 --> 00:15.365
This follows the Week 15 reading.

00:15.965 --> 00:21.096
On Thursday, December 3, you write exec_into, the kernel's side of exec.

00:21.696 --> 00:26.494
exec is the system call that keeps a process and gives it a new program.

00:27.094 --> 00:30.261
No solution code here: only the machine's state.

00:30.760 --> 00:33.553
A process is a slot in the process table.

00:34.153 --> 00:41.039
The slot holds the pid, its number; its parent, the slot that made it; and xstate, its exit status.

00:41.639 --> 00:46.890
ofile, its open files, has one entry per fd, a file descriptor.

00:47.400 --> 00:56.396
The slot points at a kernel stack, the page the kernel runs on for it; at a trapframe page, where the program's registers wait in the kernel;

00:56.996 --> 01:01.763
and at a page table, whose leaves map the image at USER_CODE and the stack.

01:02.280 --> 01:04.690
The example is one recorded session.

01:05.290 --> 01:10.693
Typing run sh starts sh, the user shell: pid 1, in slot 0.

01:11.293 --> 01:19.059
At its $ prompt, two lines are typed: echo tide pools glow softly, and ehco tide, a mistyped name.

01:19.560 --> 01:24.113
sh forks a child: fork is the call that makes a new process.

01:24.713 --> 01:29.545
The child makes an exec call with the line's first word as the program's name.

01:30.145 --> 01:35.501
In the kernel, that call runs exec_into, the function you write. Watch slot 1.

01:36.600 --> 01:43.294
The echo line is in sh's buffer, split in place: each word ends in a NUL, a zero byte.

01:43.894 --> 01:47.435
sh's array holds each word's address, then 0.

01:48.035 --> 01:52.129
sh is Running in slot 0; slot 1 is Unused.

01:53.640 --> 01:59.061
sh's fork made pid 2, in slot 1, with slot 0 as its parent.

01:59.661 --> 02:04.323
Slot 1 holds the child; its parts are drawn at its exec call.

02:05.840 --> 02:12.089
Pid 2 made its exec call; the hart, the processor, is in supervisor mode, the kernel's.

02:12.689 --> 02:23.637
In its trapframe, a0, the first argument register, holds the address where the word echo starts; a1, the second, points at sh's array.

02:24.160 --> 02:28.937
Here is pid 2 at its exec call; its physical pages are examples.

02:29.537 --> 02:38.863
Its table's root is 0x87BC_1000, and the leaf at 0, USER_CODE, names a page that holds sh.

02:40.860 --> 02:49.948
Pid 2, the same process, is in user mode, the program's mode; its pc, the program counter, is 0: echo's first instruction.

02:50.548 --> 02:58.041
Its table's root is, for example, 0x87BC_8000; the page at 0 holds echo.

02:58.560 --> 03:04.797
In its trapframe, epc, the saved pc, holds 0, and a0 holds 5.

03:05.397 --> 03:13.528
sp, the stack pointer, holds 0x1_0FA0, and a1 holds 0x1_0FA0.

03:16.040 --> 03:33.284
Pid 2 wrote its words and a newline, then called exit with 0. It became a Zombie, an exited process that never runs again, with xstate 0. sh's wait, the call that waits for a child's exit, answered 2; slot 1 is Unused.

03:34.400 --> 03:39.819
Now the mistyped line, ehco tide, in sh's buffer, split in place.

03:40.819 --> 03:45.032
This time sh's fork made pid 3, again in slot 1.

03:46.560 --> 03:52.757
Pid 3 has made the same exec call, but the word in sh's buffer is ehco.

03:53.357 --> 03:57.067
Its parts are drawn by name, with no example numbers.

03:59.100 --> 04:04.849
The exec call has come back to pid 3, and its a0 slot holds -1.

04:05.449 --> 04:10.611
Pid 3 is in user mode, just past its exec ecall, the trap instruction.

04:12.120 --> 04:13.985
The console shows nothing new.

04:14.985 --> 04:17.000
What happens to this child next?

04:20.500 --> 04:24.490
Pid 3 wrote exec: not found, then called exit with 1.

04:25.090 --> 04:27.042
sh's wait answered 3.

04:27.642 --> 04:35.912
This console's keys were typed into cargo run, which boots the kernel; it shows the not-found line, then sh's $ prompt.

04:36.440 --> 04:44.417
The console is the recorded session, up to the case's typed line, and at the case's last step, up to that line's answer.

04:45.017 --> 04:51.383
sh echoes Enter as a carriage return; the console draws each one as a line break.

04:51.880 --> 04:54.897
This is sh's given code for one command.

04:55.497 --> 05:05.653
Its marked lines are the ones the ehco tide child ran after its exec came back: the write of the not-found line, then exit with 1.

05:06.160 --> 05:12.629
These keys were typed into cargo run: run sh, the two lines, then exit, each with Enter.

05:13.229 --> 05:16.427
The mistyped line and its not-found line are marked.

05:16.960 --> 05:19.337
Kernel A has a broken exec_into.

05:19.937 --> 05:27.857
On the reference, the course's finished kernel, the echo line typed into cargo run is answered with tide pools glow softly;

05:28.457 --> 05:33.563
on kernel A, the same line is answered with no words before the next $ prompt.

05:34.080 --> 05:41.393
The test, which checks the exercise with its own programs, stops kernel A at its first check, execself.

05:41.993 --> 05:47.843
The test prints a fail line that begins: execself exited 0, expected 2.

05:48.843 --> 05:52.576
The page draws only these recorded lines for kernel A.

05:53.080 --> 05:58.700
You write exec_into, in exec.rs; everything else on this page is given.

05:59.300 --> 06:06.452
When it succeeds, the process resumes at the new program's first instruction, in user mode, with its arguments.

06:07.052 --> 06:09.798
When it fails, its caller sees -1.

06:10.320 --> 06:12.399
Each row is one of the test's checks.

06:12.999 --> 06:21.673
Under the skeleton, the code as you receive it, the test prints: [fail] exec RETURNED to the caller — on success it must replace.

06:22.200 --> 06:30.814
The last column names the picture to look at: for execfail, which tries to exec a missing program, the mistyped line at step 4.

06:31.414 --> 06:39.103
When every check passes, the test prints: [ok] exec works — fork + exec + wait, the whole shell pattern.

06:39.600 --> 06:44.836
exec gives a running process a new program, and the process carries on in it.

06:45.436 --> 06:48.053
Next: Thursday's session, December 3.

06:48.653 --> 06:51.665
Friday's explainers start with Shipped Without an Edit.
