WEBVTT

00:00.000 --> 00:06.205
Week 15: From Keypress to Exit, one command, end to end, in 53k's kernel.

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

00:13.720 --> 00:24.109
After the Week 15 reading on fork, wait, and the user shell, for Friday, December 4, where 53k runs your September commands on your kernel.

00:24.709 --> 00:29.591
No solution code here: the machine's state, not the lines that make it so.

00:30.120 --> 00:36.209
The kernel is the 53k reference, with the course's own September commands shipped in.

00:36.809 --> 00:41.501
Under cargo run, the session made pantry, a file of 38 bytes.

00:42.101 --> 00:47.593
At $, the prompt of sh, the user-mode shell, it typed mygrep salt pantry.

00:48.120 --> 00:56.724
Three lanes: devices, the kernel in supervisor mode, programs in user mode. The thick line is where the CPU is.

00:57.324 --> 01:00.654
An ecall enters the kernel; an sret leaves it.

01:01.254 --> 01:05.149
sh, then its child, then mygrep in the same process.

01:05.680 --> 01:16.423
sh, pid 3, its process id, waits in a one-byte read. The hart, the processor, waits in getc, the console's reader, in supervisor mode.

01:17.023 --> 01:22.962
HEAD and TAIL, the two counters of the ring of typed bytes, both read 116.

01:24.480 --> 01:31.627
Enter arrives: the UART, the serial port, holds 0D, and its interrupt source, 10, is Pending.

01:32.227 --> 01:37.555
The hart traps to kernelvec, the kernel's trap entry, still in supervisor mode.

01:39.080 --> 01:49.240
The console's handler claims source 10, taking its interrupt, and 0D is in ring slot 116: TAIL reads 117.

01:49.840 --> 01:53.702
The completion wrote 10 back, and the hart is back in getc.

01:55.200 --> 02:06.130
The read answers 1, in sh's saved a0, the copy of a0 the kernel keeps for it, and Enter's byte, 0D, is in sh's buffer.

02:06.730 --> 02:10.622
The hart is back in user mode, on sh's page table.

02:12.120 --> 02:19.885
sh echoes the byte: its write traps into supervisor mode, and a7, the call's number, holds 16.

02:20.485 --> 02:24.565
scause, the trap's cause, reads 8, and the write answers 1.

02:26.080 --> 02:33.157
Back in user mode. The buffer reads mygrep, salt and pantry, each ended by a NUL, a zero byte.

02:33.757 --> 02:36.367
An array holds their addresses, then 0.

02:37.880 --> 02:50.343
sh's fork traps into supervisor mode: a7 holds 1. The child is pid 4, in slot 1 of the process table, Runnable, ready to run, its parent slot 0.

02:50.943 --> 02:54.493
fork answers 4 in sh, and 0 in the child.

02:56.000 --> 03:09.175
sh's wait traps into supervisor mode: a7 holds 3. No child is a Zombie, an exited process, so sh gives up the CPU to the scheduler, which picks who runs.

03:10.175 --> 03:12.389
It runs the child, in user mode.

03:13.920 --> 03:22.005
The child's exec traps into supervisor mode: a7 holds 7, and a0 points at mygrep in the buffer.

03:22.605 --> 03:27.909
The lookup finds no mygrep among the kernel's 12 programs; the shipped list has it.

03:29.440 --> 03:37.047
Pid 4 runs on mygrep's new page table: an image page at 0, unmapped pages, the stack page.

03:37.647 --> 03:43.986
sepc, where the sret returns, is 0: the hart is in user mode at 0.

03:45.520 --> 03:57.885
mygrep's open traps into supervisor mode: a7 holds 15. It answers 3: file descriptor 3 holds inode 2, pantry's file record, at offset 0.

03:58.885 --> 04:05.251
The read of fd 3 also traps: it answers 38; its offset reads 38.

04:06.760 --> 04:18.359
Each call traps into supervisor mode: mygrep writes 37 bytes to fd 1, the console, and the answer is 37. mygrep's line is on the console.

04:19.359 --> 04:22.546
Then its newline: 1 byte, and the answer is 1.

04:24.080 --> 04:30.041
The next read of fd 3 answers 0: nothing is left past offset 38.

04:31.041 --> 04:38.388
mygrep closes fd 3: a7 holds 21, the answer is 0, and fd 3 is free.

04:39.920 --> 04:48.409
mygrep calls exit with 0, and a7 holds 2: slot 1 is a Zombie, xstate, its exit status, 0.

04:49.409 --> 04:55.402
sh's wait answers 4, slot 1 is Unused, and sh is back in user mode.

04:56.920 --> 05:03.088
sh's prompt went out, and sh is in read again; the hart waits in supervisor mode.

05:03.688 --> 05:09.467
These keys were typed into cargo run; the console shows mygrep's line, then the prompt.

05:10.000 --> 05:18.284
The second case: the same command, typed earlier at rv6$, the kernel's own prompt. No process is running.

05:19.284 --> 05:25.565
In supervisor mode, the lookup finds no mygrep among the kernel's 12; the shipped list has it.

05:27.080 --> 05:34.223
The kernel's shell built mygrep's process, pid 2 in slot 0, and it runs in user mode at 0.

05:35.223 --> 05:40.417
Its calls get the same answers as at $, from open 3 to close 0.

05:41.920 --> 05:48.169
mygrep's exit traps into supervisor mode: slot 0 is a Zombie, xstate 0.

05:49.169 --> 05:57.939
The kernel's shell prints run: pid 2 (mygrep) exited with status 0, then its prompt. These keys were typed into cargo run.

05:58.440 --> 06:02.299
Both consoles are one session, typed into cargo run.

06:02.899 --> 06:07.185
At $, mygrep's line is followed by sh's next prompt.

06:07.785 --> 06:13.329
At rv6$, the kernel's shell prints a run line, with the pid and the status.

06:13.840 --> 06:18.467
Nothing new to write: every crossing runs code from the exercises.

06:19.067 --> 06:30.199
The 53k test runs mygrep in its own world. On the reference kernel, the test prints: [ok] mygrep ran on your kernel and printed what it does on your laptop

06:30.720 --> 06:38.694
From one Enter to the next prompt, at each crossing, a register or a table decides where the CPU goes next.

06:39.294 --> 06:46.009
Next: the extra-credit explainers Headers Say Where and The Timer Takes It Back, then Friday's session.
