WEBVTT

00:00.000 --> 00:05.519
Week 15: A Slot in Your Table, for exercise 50k_file_descriptors.

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

00:13.040 --> 00:15.246
After the November 24 lecture:

00:15.846 --> 00:22.821
on Tuesday, December 1, you write fdalloc, so that each open's entry gets a slot of its own.

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

00:28.800 --> 00:37.396
Every process carries its own file table, ofile, in its Proc, the process's record: NOFILE, 16 entries.

00:37.996 --> 00:39.520
Each entry is a File.

00:40.120 --> 00:46.774
A descriptor is a slot's number: descriptor 3 names entry 3 of the calling process's table.

00:47.280 --> 00:54.150
As a process starts, slots 0, 1 and 2 hold the console, R W: readable and writable.

00:54.750 --> 00:57.789
Slots 3 to 15 are free, their kind None.

00:58.389 --> 01:05.844
The root directory holds one file, ballad: inode 2, the file system's record of it, and 93 bytes.

01:06.360 --> 01:09.009
Under the table, one entry's five fields.

01:09.609 --> 01:12.947
Its kind: None, Console or Inode; which inode;

01:13.547 --> 01:16.663
off, the offset, where the next read or write starts;

01:17.263 --> 01:19.961
and whether the program may read it, and write it.

01:20.480 --> 01:27.573
The stepper draws this page's example program on the reference, the course's finished kernel, one call a step.

01:28.173 --> 01:31.509
Before the first call: the table as the process starts,

01:32.109 --> 01:35.590
and the root directory, inode 1, which holds ballad.

01:37.120 --> 01:44.153
The first call, open, number 15 in a7: the path ballad, the flags 0x000, O_RDONLY.

01:44.753 --> 01:50.327
The given sys_open builds a File for inode 2, offset 0, readable only.

01:50.927 --> 01:53.329
It answers 3: slot 3 holds it.

01:54.840 --> 02:00.185
Next, chores, with 0x602: O_TRUNC, O_CREATE and O_RDWR.

02:00.785 --> 02:04.431
The open creates chores, inode 3, 0 bytes.

02:05.031 --> 02:10.043
Slot 4 holds its entry, readable and writable, and the answer is 4.

02:11.560 --> 02:14.044
ballad again, with 0x002, O_RDWR.

02:14.644 --> 02:19.485
Slot 5 holds a second entry on inode 2, readable and writable,

02:20.085 --> 02:23.892
and slot 3 still holds the first. The answer is 5.

02:25.400 --> 02:28.429
chores again, with 0x000: O_RDONLY.

02:29.029 --> 02:33.783
Slot 6 holds inode 3, readable only, and the answer is 6.

02:34.383 --> 02:38.435
Slots 3 to 6 are in use; 7 to 15 are free.

02:39.960 --> 02:45.101
Now a close: 21 in a7, and in a0 the descriptor 4.

02:45.701 --> 02:50.355
Slot 4 is free again, its kind None, and the close answers 0.

02:50.955 --> 02:53.887
A close builds nothing: it empties a slot.

02:55.400 --> 02:58.983
The close of 6 frees slot 6, and answers 0.

02:59.583 --> 03:03.731
Now slots 4 and 6 are free, and 6 was freed last.

03:04.331 --> 03:06.315
What does the next open return?

03:07.815 --> 03:08.989
Keep your answer.

03:10.520 --> 03:12.631
The open of chores answers 4.

03:13.231 --> 03:18.746
Slot 4 holds its entry; slot 6 stays free, and so do 7 to 15.

03:19.346 --> 03:24.191
For the reference, the test prints four [ok] lines, then OSLINGS:PASS.

03:24.720 --> 03:29.848
open's second argument is one word of yes-or-no bits, the O_* constants.

03:30.448 --> 03:36.688
O_CREATE and O_TRUNC act on the file: one creates it if it is missing, the other empties it.

03:37.200 --> 03:41.986
0x602 has the O_TRUNC, O_CREATE and O_RDWR bits;

03:42.586 --> 03:44.878
0x000, O_RDONLY, has none.

03:45.478 --> 03:53.403
Readable and writable are what the given sys_open decides from the two access bits, O_RDWR and O_WRONLY.

03:53.920 --> 03:58.517
Now the skeleton, the code as you receive it, at the same first open.

03:59.517 --> 04:05.625
sys_open builds the same entry, but the answer is −1, and no slot changes.

04:06.225 --> 04:09.847
The test prints [fail] cat printed:, then empty quotes.

04:10.360 --> 04:13.353
Kernel A, a broken kernel, at the same open.

04:14.353 --> 04:17.097
It answers 3, but slot 3 is still free:

04:17.697 --> 04:20.117
the number names a slot that holds nothing.

04:20.717 --> 04:23.843
The test prints the skeleton's lines, byte for byte.

04:24.360 --> 04:26.048
One line, two machines.

04:26.648 --> 04:33.765
Side by side, the two answers: on the skeleton, −1; on kernel A, 3, naming a free slot.

04:34.365 --> 04:39.451
They fail differently, and the test prints one console for both: the same bytes.

04:39.960 --> 04:43.487
Kernel B, another broken kernel, at the same calls.

04:44.487 --> 04:48.357
Its first open builds the same entry, and answers 15.

04:48.957 --> 04:52.913
Slot 15 holds it, and slots 3 to 14 are free.

04:53.440 --> 04:55.528
After the same opens and closes,

04:56.528 --> 05:00.731
the open of chores answers 14, and slot 14 holds it.

05:01.331 --> 05:05.622
The test cannot see kernel B, and passes it, broken as it is.

05:06.120 --> 05:12.292
Of what this page draws, you write fdalloc, in syscall.rs; the rest is given.

05:12.892 --> 05:21.327
When fdalloc returns, the entry it was handed is in the calling process's table, at the slot whose number it returns.

05:21.840 --> 05:24.004
That number is what open answers.

05:24.604 --> 05:29.799
The slot is the lowest-numbered free one: 4 at the reopen, not 6 or 7.

05:30.399 --> 05:35.362
When no slot is free, it returns −1, and the table is unchanged.

05:35.880 --> 05:43.080
The course's test runs in three steps, in its own world of files, and checks what its programs read or wrote.

05:43.680 --> 05:51.870
Its first step needs both parts of 50k: fdalloc, and sys_read's Inode arm, which the next explainer draws.

05:52.400 --> 05:58.579
On the skeleton and on kernel A, the test prints [fail] cat printed:, then empty quotes.

05:59.179 --> 06:02.123
The picture to look at: the skeleton's last step.

06:02.723 --> 06:09.705
In the test's first step, cat poem, the skeleton's open also answered −1, and cat printed nothing.

06:10.240 --> 06:14.143
A descriptor is a slot's number in the calling process's table:

06:14.743 --> 06:19.085
open fills a slot and hands back its number, and close empties it.

06:19.685 --> 06:23.883
Next: the explainer The Offset Remembers, then Tuesday's session.
