← Interview Prep

Linux Kernel — Accessing Files

From read()/write() to disk: the fd→file→inode→address_space chain (and the shared offset), buffered reads/writes through the page cache with readahead & writeback/fsync durability, and the alternatives — O_DIRECT, mmap, O_SYNC, sendfile/splice — plus sharing/locking.

What actually happens between read()/write() and the disk — the descriptor chain, the page cache in the middle, and the choices (buffered, direct, mmap, sync) that change the path. Original, interview-focused notes; builds on the VFS and the page cache.

A file descriptor is a per-process index → a file object (offset, flags) → a dentry/inode → an address_space (the file's page cache). A normal read/write flows through the page cache; the interesting variations are the ones that don't.

The descriptor chain

fd (int)  →  fd table  →  struct file {offset, flags}  →  dentry → inode → address_space
                              (shared by dup/fork)              (the file's cached pages)

Buffered read & write (the default)

The other ways to access a file

MethodPathUse
BufferedThrough the page cacheDefault; benefits from caching & readahead.
Direct (O_DIRECT)Bypasses the page cache, DMA to/from user buffers (aligned)Databases with their own cache; avoids double-buffering.
mmapMaps page-cache pages into the address spaceFile access = memory access; shared read-only text/libs; random access.
Synchronous (O_SYNC)Each write waits for stable storageWhen you can't call fsync yourself; slower.
sendfile/spliceKernel-to-kernel, no user copyZero-copy file→socket (web/file servers).

Sharing & consistency

Likely interview questions

Kernel Internals series. Related: The VFS · Page Cache.