4.2.34 · D3Operating Systems

Worked examples — File allocation — contiguous, linked, indexed (inode)

3,861 words18 min readBack to topic

This page is the "throw everything at it" drill for File allocation — contiguous, linked, indexed (inode). We will not learn new theory — we will exercise the three schemes across every kind of input they can face: normal cases, the boundary between two index levels, a zero-size file, a file that is exactly one block, a growth request that fails, and an exam twist. Before any example, we build a map of all the cases.

Two numbers appear in almost every example, so we pin them down once, in plain words:


The scenario matrix

Every cell below is a distinct situation. The Ex column says which worked example covers it.

Case class Contiguous Linked Indexed (inode)
Normal byte-to-block lookup Ex 1 Ex 3 Ex 5
Boundary between levels/blocks Ex 2 (block edge) Ex 6 (direct↔indirect edge), Ex 12 (single↔double↔triple)
Zero-size / degenerate input Ex 8 (empty & 1-block) Ex 8 Ex 8
Limiting value (max reach) Ex 11 (pointer-width limit) Ex 7 (max file size)
Growth request (can it grow?) Ex 4 (fails) Ex 4 (succeeds) Ex 4 (succeeds)
Real-world word problem Ex 3 Ex 9 (photo library)
Exam-style twist Ex 11 (width vs count) Ex 10 (reads for 3 blocks)

Related boundaries live in External vs Internal Fragmentation (Ex 4), Free Space Management (bitmap, free list) (Ex 4 growth), and File Systems (ext4, FAT, NTFS) (Ex 3 FAT note).


Two tools we reuse (defined before first use)

The picture below (Figure s01) shows bytes as a long ruler and blocks as the tick-marks every bytes; the amber arrow marks byte 9000 landing inside block 2 at offset 808.

Figure — File allocation — contiguous, linked, indexed (inode)

Ex 1 — Contiguous, normal lookup (matrix: Contiguous / normal)


Ex 2 — Contiguous, block-edge boundary (matrix: Contiguous / boundary)


Ex 3 — Linked, word problem + random access (matrix: Linked / normal + word)

The chain and its four reads are drawn in Figure s02: each cyan box is a data block, the amber slot inside it is the next-pointer, and the amber arrows are the links you must follow one after another to reach logical block 3.

Figure — File allocation — contiguous, linked, indexed (inode)

Ex 4 — Growth request, all three schemes (matrix: growth row)


Ex 5 — Indexed, normal lookup (matrix: Indexed / normal)


Ex 6 — Inode, the direct↔indirect boundary (matrix: Indexed / boundary)

Figure s03 draws exactly this boundary: the in-memory inode on the left, a cyan single-read arrow to data block 11 (direct), and an amber two-hop path (indirect block → data block) to block 12.

Figure — File allocation — contiguous, linked, indexed (inode)

Ex 7 — Inode, limiting value: maximum file size (matrix: Indexed / limiting)


Ex 8 — Degenerate inputs: empty file & one-block file (matrix: zero/degenerate)


Ex 9 — Word problem: a photo library (matrix: Indexed / word)


Ex 10 — Exam twist: reads to fetch a range (matrix: Indexed / exam twist)


Ex 11 — Linked, limiting value + width-vs-count twist (matrix: Linked / limiting + exam twist)


Ex 12 — Inode, single↔double↔triple boundaries (matrix: Indexed / deep boundary)

Figure s04 stacks the four regions as nested pointer-block ladders; the amber count beside each shows reads climbing 1 → 2 → 3 → 4 as you descend into deeper indirection.

Figure — File allocation — contiguous, linked, indexed (inode)

Recall Quick self-test

Contiguous phys of byte 9000, start block 50, B=4096? ::: block 52, offset 808. Linked: reads to reach logical block 3 of chain 7→3→90→12? ::: 4 reads. Inode with B=4096, p=4, k=1024 — max file size? ::: about 4 TB (49152 + 4 MB + 4 GB + 4 TB bytes). Reads to fetch logical block 12 in an inode (inode in RAM)? ::: 2 (single-indirect block + data block). Reads to fetch the first triple-indirect block (uncached)? ::: 4 (triple + double + single + data). Blocks a 3 MB photo needs at B=4096? ::: 733 blocks. Does a 0-byte file need any data blocks? ::: No — all pointers null, but the inode still exists. Largest disk a 4-byte linked pointer can name? ::: 2^32 blocks × 4096 B = 16 TiB.