December Code

File system interview questions, with answers

The file system is how an operating system turns a disk's numbered blocks into named files and directories. Interviews test the allocation methods and their trade-offs, the structures that describe files, such as inodes, and how file systems survive crashes.

The answers below cover what a file system does, the allocation methods, inodes, free-space management, journaling and what happens when a program opens a file. Then take the free Operating Systems diagnostic — ten questions across every OS topic in the bank — to see which of these you can explain but not yet apply.

The questions, with answers

  1. 1.What does a file system do?

    In short: It organises storage into named files and directories, tracks which blocks hold each file, and manages free space and permissions.

    A disk offers only numbered fixed-size blocks. The file system provides the abstraction on top: named files with metadata such as size, owner, permissions and timestamps, a hierarchy of directories, the mapping from each file to the blocks that hold its data, and management of free blocks. It also enforces access control and aims to keep its structures consistent after crashes. Examples include ext4 and XFS on Linux, NTFS on Windows, APFS on Apple systems and FAT32 on removable drives. The operating system's virtual file system layer lets programs use them all through the same calls.

  2. 2.What are contiguous, linked and indexed allocation?

    In short: Ways of placing a file's blocks: consecutive blocks, a chain of blocks with pointers, or an index block listing all block addresses.

    Contiguous allocation stores a file in consecutive blocks, recorded by start and length: reading is fast and direct access is a simple calculation, but free space fragments and files are hard to grow. Linked allocation chains blocks with a pointer in each, so any free block can be used and files grow easily, but reaching block i means following i pointers, and a damaged pointer loses the rest of the file; FAT moves the pointers into a table. Indexed allocation gives each file an index block listing its blocks, supporting direct access without external fragmentation, at the cost of the index block's space.

  3. 3.What is an inode?

    In short: In Unix file systems, the structure holding a file's metadata and pointers to its data blocks; names live in directories.

    An inode stores everything about a file except its name: type, permissions, owner, size, timestamps, link count and the addresses of its data blocks. The block addresses are a mix of direct pointers to data blocks, which serve small files quickly, and indirect pointers to blocks that themselves hold pointers, which let the same fixed-size inode describe very large files. Directories are files that map names to inode numbers, which is why a file can have several names. ext4 replaces most indirect pointers with extents, ranges of contiguous blocks, to describe large files more compactly.

  4. 4.How does a file system keep track of free space?

    In short: With a bitmap of one bit per block, a free list, grouping or counting, or extent trees in modern file systems.

    A bitmap, or bit vector, keeps one bit per block: it is compact and makes finding runs of contiguous free blocks easy, which is why many file systems use it. A free list links free blocks together, needing no extra space but making contiguous allocation hard. Grouping stores the addresses of many free blocks in one block, and counting records runs as a start address and a length. Modern file systems such as XFS and ext4 track free space as extents or in B-trees, so large allocations can be found quickly on very large disks.

  5. 5.What is journaling in a file system?

    In short: Writing intended metadata changes to a log first, so after a crash the file system replays or discards them instead of scanning the disk.

    Creating or deleting a file updates several structures, such as a directory entry, an inode and the free-space bitmap, and a crash between those writes leaves the file system inconsistent. A journaling file system first writes the whole set of changes as a transaction to a log, the journal, then applies them to their real locations and marks the transaction complete. After a crash, it replays committed transactions and discards incomplete ones, which takes seconds instead of a full consistency check. ext4 and NTFS journal metadata by default; journaling data as well is safer but slower.

  6. 6.What happens when a program opens a file?

    In short: The OS resolves the path to the file's metadata, checks permissions, and returns a descriptor pointing into the open-file tables.

    The open system call walks the path one directory at a time, looking up each name to find the next inode, and checks the caller's permissions. It then creates an entry in the system-wide open-file table, holding the current offset and access mode and pointing to the in-memory copy of the inode, and adds a pointer to it in the process's file descriptor table. The call returns the index of that pointer, a small integer file descriptor, which later read, write and close calls use. Closing the last descriptor for an entry releases it.

How the diagnostic asks it

One question from the Operating Systems bank, exactly as a sitting would show it. The bank has 3 on file systems and 30 across Operating Systems.

File Systems · easyOS-021

Which file allocation method gives the fastest direct access to any block of a file but suffers from external fragmentation?

  1. 1Contiguous allocationcorrect
  2. 2Linked allocation
  3. 3Indexed allocation
  4. 4Linked allocation with a file allocation table

Contiguous allocation stores a file in consecutive blocks, so block i is simply start + i: direct access is one calculation, and sequential reads need little seeking. Its costs are external fragmentation, since free space breaks into holes, and difficulty growing a file. Linked allocation has no external fragmentation but must follow pointers to reach block i. A FAT keeps those pointers in a table, which is faster but still a chain. Indexed allocation reaches any block through an index block, with no external fragmentation.

Measure it

Reading answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Operating Systems questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

What the readiness test measures · how the score is computed

By Harshit · updated