WEBVTT

00:00.000 --> 00:05.566
Week 15: The Offset Remembers, for exercise 50k_file_descriptors.

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

00:13.080 --> 00:15.286
After the November 24 lecture:

00:15.886 --> 00:22.412
on Tuesday, December 1, you write the Inode arm of sys_read, what a read on a file does.

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

00:28.400 --> 00:33.593
The file here is ballad, made at the rv6$ prompt with the kernel shell's echo:

00:34.193 --> 00:40.248
92 characters and a newline, 93 bytes, in inode 2, the file's record.

00:40.760 --> 00:50.213
The given cat opens ballad read-only, and open answers 3: a descriptor, or fd, the number of a slot in cat's file table.

00:50.813 --> 00:53.545
Slot 3 now holds an entry for the file.

00:54.040 --> 00:56.085
The entry says what is open and how,

00:56.685 --> 01:02.583
and one number more: the offset, the byte the next read or write on this descriptor starts at.

01:03.080 --> 01:08.069
Slot 3 as open leaves it: inode 2, may be read, offset 0.

01:08.669 --> 01:14.448
The marker over ballad's strip, one cell per byte, is that offset, at the file's first byte.

01:15.560 --> 01:22.685
The lab traced run cat ballad on the reference kernel, the course's finished one, one system call per step.

01:23.285 --> 01:31.833
The given cat asks for 64 bytes a read, and writes to fd 1, the console, as many bytes as the read answered.

01:32.360 --> 01:39.707
The reference. Its console: these keys were typed into cargo run; cat's output is drawn at the last step.

01:40.307 --> 01:46.514
open answers 3. Slot 3 names inode 2, may be read, and its offset reads 0.

01:48.040 --> 01:54.362
A read: a7 holds 5, a0 the fd, 3, and a2 the count, 64.

01:54.962 --> 02:02.519
It answers 64: cat's buffer holds the file's first 64 bytes, and the offset reads 64.

02:04.040 --> 02:09.009
cat writes to fd 1 as many bytes as the read answered: 64.

02:09.609 --> 02:16.161
The offset still reads 64, in a file of 93 bytes. What will the next read answer?

02:17.661 --> 02:18.835
Keep your answer.

02:20.360 --> 02:27.762
The answer is 29: of the file's 93 bytes, only 29 lay past where the offset stood.

02:28.362 --> 02:34.185
They are now at the front of cat's buffer, and the offset reads 93, the end of the file.

02:35.720 --> 02:39.431
cat writes 29 bytes, as many as the read answered.

02:40.031 --> 02:45.876
The rest of its buffer still holds bytes from the first read, and this write does not send them.

02:47.400 --> 02:52.195
The third read. No byte lay past offset 93, the end of the file:

02:52.795 --> 02:57.971
it delivered 0, the answer is 0, and the offset still reads 93.

02:59.480 --> 03:04.951
close answers 0, and slot 3 is free again; cat exits with status 0.

03:05.551 --> 03:13.368
The console now shows what cat printed, the file's 93 bytes, then the line run printed when cat exited.

03:13.880 --> 03:24.290
The example program of the explainer A Slot in Your Table opens ballad twice: fd 3 with O_RDONLY, fd 5 with O_RDWR.

03:24.890 --> 03:31.173
Both entries name the same inode, each offset at 0. 6 reads take them in turn.

03:31.680 --> 03:36.926
The first read: fd 5 asks for 24, and the answer is 24.

03:37.526 --> 03:42.581
fd 5's offset reads 24; fd 3's still reads 0.

03:44.080 --> 03:53.501
fd 3 asks for 70, and the answer is 70: the file's first 70 bytes, the same ones fd 5 began with, and more.

03:54.101 --> 03:59.135
fd 3's offset reads 70; fd 5's still reads 24.

04:00.640 --> 04:12.302
fd 5 asks for 70 from offset 24. Only 69 lay past it: the answer is 69, and fd 5's offset reads 93.

04:13.800 --> 04:23.297
fd 3 asks for 70 from offset 70. The answer is 23, and fd 3's offset reads 93 as well.

04:24.297 --> 04:30.167
Both entries stand at 93, the end of the file: a read on either answers 0.

04:30.680 --> 04:32.695
A read that answers the wrong count.

04:33.295 --> 04:39.437
Kernel A is a broken sys_read, run under cargo run with the same two typed lines.

04:40.037 --> 04:44.815
Its offsets move as the reference's do: 64, then 93.

04:45.415 --> 04:47.931
Its answers read 64 each time.

04:48.440 --> 04:51.297
Kernel A, after the same first read and write.

04:52.297 --> 05:00.050
Its second read answers 64, though it delivered 29; the offset reads 93, as in the reference.

05:01.050 --> 05:06.430
cat writes 64: the new bytes, then the rest of its buffer, from the first read.

05:07.960 --> 05:14.678
The third read answers 64 again, though it delivered 0; the offset still reads 93.

05:15.678 --> 05:18.077
cat writes the same 64 bytes.

05:18.677 --> 05:26.002
These keys were typed into cargo run; the console runs on past this step, cut after its first lines.

05:26.520 --> 05:32.853
You write the Inode arm of sys_read, in syscall.rs: what a read on a file does.

05:33.453 --> 05:37.121
The rest of sys_read, open, write and close are given.

05:37.640 --> 05:40.903
When a read on a file returns, three things are true.

05:41.503 --> 05:44.340
Its answer is the number of bytes it delivered.

05:44.940 --> 05:49.088
Those bytes are the file's own, from where the entry's offset stood.

05:49.688 --> 05:53.421
And the entry's offset now stands that many bytes further on.

05:53.920 --> 05:56.959
The test runs its own programs in its own world.

05:57.559 --> 06:07.511
It runs cat on a file called poem, writes out.txt through a descriptor and reads it back, then runs cat on a name that is not there.

06:08.040 --> 06:14.886
The first step that fails prints one [fail] line, and the test stops there; kernel A stops it at cat poem.

06:15.486 --> 06:22.560
The reference passes: the test prints [ok] user programs can open, read, write, and close files.

06:23.080 --> 06:29.600
A descriptor's place in its file is one number in its entry, and every read on that entry starts from it.

06:30.200 --> 06:32.735
Next: Tuesday's session, December 1.

06:33.335 --> 06:36.853
Thursday's explainers start with One Fork, Two Paths.
