ON THE DISKKERNELMEMORYetc3home7ada19bob20notes31todo42inode420m1i2l3k4↵5e6g7g8s9↵1fd = open("/home/ada/todo",2 O_RDONLY);3n = read(fd, buf, 5);4n = read(fd, buf, 5);5close(fd);offsetmodeinodefd 0ttyfd 1ttyfd 2ttyfd 3buf719423milk↵eggs↵
Back: on the disk (directories, the inode, the file's bytes)Front: kernel memory (open file record, descriptor table) and your programTokens: inode numbers found on the walk, read requests, bytes copied, the offset markerAt the back, on the disk: three directories, each a list of names and inode numbers, the file's inode 42, and its 10 bytes. At the front: the program's code and buffer, and in kernel memory the open file record and the program's descriptor table. open() walks /home/ada/todo one directory at a time to inode 42, fills an open file record and points slot 3 at it. Each read(3, buf, 5) follows slot 3 to the record, copies 5 bytes from the offset and moves the offset on. Inode 2 is the root directory on ext2, ext3 and ext4. The other inode numbers (7, 19, 42 and so on) are made up for the example; on a real disk, ls -i shows them.

Files and directories

  1. A file is a numbered row of bytes. Our file holds 10: milk, a newline, eggs, a newline. The disk knows it by a number, inode 42, not by its name. An inode is the record that says where the bytes are.

Opening a file

Reading and closing

What happens

;; step 1/10 · file
byte   0 1 2 3 4  5 6 7 8 9
value  m i l k \n e g g s \n     (10 bytes)

low-level name   inode 42
the inode        size, owner, permissions,
                 where the bytes sit on the disk
                 (the next lesson opens it up)

Inode 2 is the root directory on ext2, ext3 and ext4. The other inode numbers (7, 19, 42 and so on) are made up for the example; on a real disk, ls -i shows them. The ↵ mark stands for the newline byte (10 in ASCII) that ends each line of the file. Linux calls the open file record an open file description; textbooks call the table of them the open file table. The descriptor table is private to each process, while the open file records live in one table for the whole system.