Operating System Interview Questions · Question 25

What happens inside the OS when a program opens and reads a file?

Interview preparation resource from Gate Smashers.

Interview-ready answer

open() makes the kernel resolve the path, check permissions and create an open-file object, returning a file descriptor. read() then copies requested bytes from the page cache or obtains them through storage I/O.

Understand it clearly

The virtual file system walks path components from the starting directory, consults cached metadata or the concrete file system, follows mount points and symbolic links when allowed, and verifies access permissions. The returned descriptor is an index in the process's descriptor table referencing a kernel open-file description with flags and a current offset.

A read first checks the page cache. If data is present, the kernel copies it to the validated user buffer and advances the file offset. On a cache miss, the file system maps the logical offset to storage blocks, submits I/O through a device driver, blocks the caller if necessary and completes the copy when data arrives.

The pathname lookup is only the start. The kernel must also enforce permissions and maintain a per-process file descriptor that refers to a system open-file entry. Later reads and writes use that descriptor, avoiding a complete path lookup on every operation.