1. Home
  2. Operating Systems
  3. File Allocation Methods

File Allocation Methods

How does the OS remember which disk blocks belong to a file? Compare contiguous, linked and indexed allocation on the same scenario.

Interactive 3DBeginner12 min readOSUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A file is stored in disk blocks. The operating system must remember which blocks belong to which file. Pick a method and press Run scenario.

Pseudocode

  1. contiguous: find n free blocks in a row; file = (start, length)
  2. linked: take any n free blocks; every block stores a pointer to the next
  3. indexed: take 1 index block + n data blocks; the index lists all data blocks
  4. free(file): mark its blocks free again
  5. read block k: contiguous = 1 access, linked = k accesses, indexed = 2 accesses
Read this model as text · 8 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A file is stored in disk blocks. The operating system must remember which blocks belong to which file. Pick a method and press Run scenario.
  2. A disk of 16 blocks, managed with contiguous allocation. We will create files A, B and C, delete B, then create a bigger file D.
  3. Create file A (3 blocks): blocks 0–2, stored as (start 0, length 3).
  4. Create file B (4 blocks): blocks 3–6, stored as (start 3, length 4).
  5. Create file C (3 blocks): blocks 7–9, stored as (start 7, length 3).
  6. Delete file B. Its blocks are free again, but they sit between A and C. The free space is now split into runs of 4 and 6 blocks (10 free in total).
  7. Create file D (7 blocks) fails: 10 blocks are free, but the longest run is only 6. Contiguous allocation needs one unbroken stretch: external fragmentation.
  8. Read the 3rd block of C. Start (7) + 2 = block 9: a direct jump, 1 disk access. Contiguous files give fast random access.

Try this in the 3D model

  • Run Contiguous. After B is deleted, why can't D (7 blocks) be created although 10 blocks are free?
  • Run Linked. Which blocks does D use, and how many disk reads does it take to get the 3rd block of C?
  • Run Indexed. Find the index block of D and count the extra block it costs.
  • Which method gives the fastest random access, and which wastes the least space?

What the OS has to remember

A disk is divided into fixed-size blocks. When you save a file, the operating system chooses blocks for it and must later find them again. The three classic strategies make different trade-offs.

Contiguous allocation

The file occupies consecutive blocks. The directory stores only the start block and the length.

  • Fast: reading block k is start + k, a single access, and sequential reads need little disk-head movement.
  • Problem: as files are created and deleted, free space breaks into small holes: external fragmentation. Files also cannot easily grow. This is the same issue as in memory allocation.

Linked allocation

Each block stores a pointer to the next block, so a file can be scattered anywhere.

  • No external fragmentation, and files can grow easily.
  • Slow random access: to reach block k you must follow k pointers, k disk reads. A single lost pointer breaks the rest of the file. FAT keeps all pointers in one table to speed this up.

It is a linked list stored on disk.

Indexed allocation

Each file has an index block holding the addresses of all its data blocks.

  • Direct access in two reads: the index block, then the data block.
  • No external fragmentation.
  • Overhead: one extra block per file. Large files use several index blocks, in a multi-level scheme like the Unix inode.

The scenario in the visualization

16 blocks. Create A (3), B (4), C (3), delete B, then create D (7 blocks).

Method After deleting B Creating D
Contiguous free runs of 4 and 6 blocks fails: needs 7 in a row, though 10 are free
Linked the same 10 free blocks works: takes blocks 3, 4, 5, 6, 10, 11, 12 chained by pointers
Indexed 8 free blocks works: 1 index block plus 7 data blocks

Comparison

Contiguous Linked Indexed
Random access fast (1 read) slow (k reads) fast (2 reads)
External fragmentation yes no no
File growth hard easy easy
Extra space none pointer per block index block per file

Code

def read_block(method, file, k):
    if method == "contiguous":
        return 1, file.start + k                 # 1 disk access
    if method == "linked":
        block = file.first
        for _ in range(k):                       # k pointer hops
            block = disk[block].next
        return k + 1, block
    if method == "indexed":
        return 2, disk[file.index_block].entries[k]

Common mistakes

  • Saying linked allocation has no overhead. Every block gives up some space for the pointer.
  • Mixing up internal fragmentation (unused space inside the last block of a file) with external fragmentation (holes between files).
  • Forgetting that indexed allocation needs the index block read first, so it takes 2 accesses, not 1.

Complexity at a glance

Case / operationTimeWhy
Read block k (contiguous)O(1)address = start + k
Read block k (linked)O(k)Follow k pointers, one disk read each.
Read block k (indexed)O(1)Index block first, then the data block (2 reads).
Extra spacePointers or an index per file

Quick check

Test yourself — pick an answer to see if you got it.

1. What problem does contiguous allocation suffer from?

2. How many disk accesses does it take to read the 5th block of a linked file?

3. Which method supports efficient direct (random) access without external fragmentation?

4. What is the main cost of indexed allocation?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in File Allocation Methods. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.