Skip to content
CurriculumLesson 27 ยท Files
Intermediate~12 min

Contiguous File Allocation

Contiguous allocation gives a file one run of blocks. It is the fastest layout for sequential and direct access, but it is brittle when files grow.

In plain English

Contiguous file allocation puts a file in one long row of blocks, like a train with no gaps.

By the end you will be able to

  • Map a file to a start block and length.
  • Compute the block address for a record.
  • Explain external fragmentation in file systems.
  • Know why growing a file is difficult.
01

The mapping

The directory entry stores the first block and the number of blocks. Block i of the file is start + i. Random access is a simple calculation, so direct access is fast. Sequential access is even faster because the disk head can stay near consecutive blocks.

lesson-27.osOS
file report.txt: start = 20, length = 5
block 0 -> 20
block 3 -> 23
block 4 -> 24
02

Fragmentation and growth

As files are created and deleted, free space becomes fragmented. A new file may not find a contiguous run large enough even when total free space is sufficient. Compaction can fix this but is expensive. If a file grows, it may need to be moved to a larger hole, and its directory entry must be updated.

03

When it works well

Contiguous allocation is excellent for read-only media, preallocated files, and workloads where file sizes are known in advance. It is less suitable for general-purpose file systems with many small, growing files.

Key takeaways

  • Contiguous allocation stores a start block and length.
  • Direct access is O(1).
  • External fragmentation makes allocation fail despite free space.
  • Growing a file may require relocation.
  • It works best for known-size or read-only files.

Common pitfalls

  • Forgetting that the file occupies exactly length blocks.
  • Assuming free space is automatically contiguous.
  • Ignoring the cost of moving a growing file.

Kernel notes

  • Compute block i from start + i.
  • Explain why a file cannot always grow in place.
  • Compare sequential and direct access cost.

Quick check

0 / 2 answered

1. A file starts at block 20 and has length 5. Which block holds file block 3?

2. What is the main allocation problem with contiguous files?