Files and input/output
Descriptors and three kernel tables, buffering, inodes/directories/links, VFS, mmap and fsync.
Descriptors and five calls
To a process, a file is a sequence of bytes. The process does not hold the inode; it holds a descriptor — a small non-negative integer indexing its descriptor table. By convention 0 / 1 / 2 are stdin, stdout and stderr. open returns the lowest free number; close frees it.
Five calls do most of the work: open, read, write, lseek, close. Each read/write transfers up to the requested count and returns how many bytes actually moved (0 from read means end of file). lseek moves the offset. Worked example. On a 10-byte file, lseek(-2, SEEK_END) leaves the offset at 8, so the next read of 2 bytes gets the last two.
Short counts are normal: read may return less than asked when the pipe has only that much ready; write to a pipe may accept less when the pipe buffer is nearly full. Robust code loops until the intended transfer finishes or an error/EOF stops it — the same discipline sockets needed for TCP streams.
Three tables
Three kernel tables stand behind a descriptor:
- The per-process descriptor table — maps fd → open-file entry.
- The system-wide open-file table — holds the offset, status flags and a pointer to the inode (or pipe/socket object).
- The inode (or equivalent) — owner, mode, size, data blocks.
fork and dup share the open-file entry, so they share the offset: one child’s read advances the position the sibling sees. Two separate opens of the same path get two open-file entries and independent offsets.
Buffering matters for performance. Reading bytes one at a time costs about syscalls (including the final EOF). With a user buffer of size , the number of read calls is about . On an Apple M1, one-byte reads cost on the order of 450 ns each — stdio’s few-KiB buffer amortizes that. fwrite/fread sit in libc; write/read are the syscalls underneath, exactly as printf sat on write in the system-calls topic.
Inodes, links, VFS
The name of a file lives in a directory; metadata and data live in the inode. A hard link is another directory entry pointing at the same inode (same link count); a symbolic link is a separate inode whose contents are a path. Data blocks are freed when the link count is 0 and no process still has the file open — which is why deleting an open log does not shrink the disk until the writer closes.
The VFS (virtual file system) is the common layer: every file system, pipe and device exposes the same calls through per-type operation tables. mmap maps page-cache pages into the address space — no explicit read, one fault per new page. Ordinary write reaches the page cache, not necessarily the disk; fsync (and on macOS F_FULLFSYNC) pushes durability when a crash must not lose data. Atomic replace is write-then-rename on the same file system. Next — Users and permissions: who may open that inode.
Descriptor hygiene shows up in servers: after fork, close the listening fd in the child and the connected fd in the parent, or you leak and block exits. fcntl can set O_NONBLOCK so read returns -1/EAGAIN instead of sleeping — the same switch event loops need for sockets.
Directories are files too: their “data” is name→inode mappings. Creating a file is a directory update plus an inode allocation; unlink removes a name. On ext4 and friends, metadata journaling protects structure more readily than file contents — another reason fsync on both the file and its directory matters for durable renames.
Compared with raw read/write, memory-mapped I/O shines for large random access: the kernel’s page cache and the process’s TLB do the work. Sequential streaming still often prefers a buffered read loop sized to a few multiples of the file-system block (commonly 4 KiB). Either way, the five calls and three tables are the model underneath libc, languages and databases.
Sparse files, holes and O_APPEND are further inode/open-file details: append mode forces each write to the end atomically enough for concurrent loggers sharing one open-file entry. pread/pwrite take an explicit offset and leave the shared offset alone — useful after fork when siblings must not disturb each other’s cursors. Knowing which table an operation touches predicts whether sharing is a feature or a bug.
Permission bits and ownership live in the inode and are checked on open, not on every read — once you hold the fd, the capability continues until close, even if mode bits change underneath. That is why long-lived servers notice permission updates only on the next open. Combined with the three tables, you can predict sharing, durability and access control without reading filesystem source.
Worked path: create, write 13 bytes, fsync, close, reopen, lseek to 0, read 13 — each step a syscall or a libc wrapper that becomes one. strace makes the sequence visible the same way it did for write(1,buf,13) in hello world.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.