1. Gyanastra
  2. Operating Systems
  3. File Allocation Methods
  4. File Allocation Methods and Contiguous Allocation
Free lesson, no account needed 6 min read 15 practice questions after it
Asked in past papers15 times
  • GATE CSE20222019201720122008200520042002
  • ISRO2013
  • UGC NET202020172016201420132010

Operating Systems · File Allocation Methods

File Allocation Methods and Contiguous Allocation

Overview

The file system must decide which physical disk blocks hold each file's data, how those blocks are placed, and how they are retrieved later.
Contiguous allocation is the simplest placement strategy: each file occupies consecutive physical blocks, giving excellent read performance but suffering from fragmentation.

Key concepts

  • Allocation: the file system's responsibility for deciding which physical blocks of the hard disk store the blocks of a file, how those blocks are placed, and how they are retrieved later.
    For a file needing 4 blocks, the file system must choose which 4 physical disk blocks are used.
  • Contiguous allocation: a file is given a set of consecutive physical blocks on the disk.
  • Directory entry in contiguous allocation: stores the file name, the starting block, and the length in blocks; from these, every physical block of the file is known.
  • External fragmentation: enough total free space exists, but it is scattered in non-contiguous holes, so a file needing contiguous blocks cannot be allocated.
  • Internal fragmentation: the allocated block space is larger than the file's exact requirement, so part of the last block is wasted.

How it works

  1. The file system assigns a file a run of consecutive physical blocks.
  2. The directory entry records the file name, starting block, and length in blocks.
  3. Each file block is placed in the next physical block after the previous one: file block 1 in the starting block, file block 2 in the following block, and so on.
  4. To read the file, the disk head seeks once to the required track, then the disk rotates so the consecutive blocks are read in sequence.

Formulas

  • Physical blocks of a file: start, start+1, …, start+length−1\text{start},\ \text{start}+1,\ \dots,\ \text{start}+\text{length}-1
    • start\text{start} = starting block recorded in the directory entry
    • length\text{length} = number of blocks the file occupies

Worked examples

Example 1: Mapping files to contiguous disk blocks. Hard disk blocks are numbered 0 to 11.

FileStartLengthCalculationPhysical blocks
temp020, 0 + 10, 1
tr545, 6, 7, 85, 6, 7, 8
list10210, 10 + 110, 11

The remaining free blocks in the 0–11 range are 2, 3, 4, and 9.

Example 2: External fragmentation.

  • A file allocated blocks 2, 3, and 4 is deleted, so blocks 2, 3, 4 become free.
  • Another block elsewhere is also free, giving 4 free blocks in total.
  • A new file needs 4 blocks.
  • The total free space is enough, but the blocks are not contiguous, so the file cannot be allocated under contiguous allocation.
  • This is external fragmentation.

Example 3: Internal fragmentation.

  • A file requires space equal to 2½ blocks.
  • The file system allocates 3 blocks.
  • The last block is only half used, so ½ block is wasted.
  • This is internal fragmentation.

Comparison

PointExternal fragmentationInternal fragmentation
MeaningEnough total free space exists, but it is scattered in non-contiguous holesAllocated block space is larger than the file's exact requirement
Where it occurs in the exampleFree blocks 2, 3, 4 plus another free block, but not contiguousFile needs 2½ blocks but gets 3 blocks
EffectA new file needing 4 contiguous blocks cannot be allocatedHalf of the last block is wasted
Can it be avoided?Yes, by non-contiguous allocationNo, it cannot be avoided by any method
Contiguous allocationDetails
AdvantageVery simple
AdvantageExcellent read performance because blocks are sequential
AdvantageWhole file can be read with one seek to the track, then sequential rotation of the disk
DisadvantageCauses external fragmentation
DisadvantageInternal fragmentation is also possible

Revision summary

  • In contiguous allocation, all blocks of a file are consecutive on disk; the directory entry needs only the file name, starting block, and length.
  • Read performance is excellent: only one seek is needed to reach the track, then the disk rotates to read consecutive blocks; files are usually read from beginning to end, so this placement saves time.
  • External fragmentation: free blocks exist in sufficient total number but are not contiguous, so a file needing contiguous blocks cannot be allocated.
  • Internal fragmentation: the last allocated block is partially wasted (a file needing 2½ blocks gets 3, wasting ½ block).
  • Internal fragmentation cannot be avoided by any allocation method; external fragmentation can be avoided by using non-contiguous allocation.
  • Contiguous allocation is subject to both external and internal fragmentation.

Previous-year exam insights

  • In indexed allocation the maximum file size is determined by the number of block addresses held in the index block(s) times the block size;
    with an inode having d direct, one single-indirect, one double-indirect and one triple-indirect pointer, and n = block size / address size addresses per block, max size = (d+n+n2+n3)(d + n + n^{2} + n^{3}) × block size.
    Asked inGATE 2019GATE 2004UGC NET 2020UGC NET 2013
  • Inserting a block in the middle of a file: contiguous allocation requires shifting all subsequent blocks (many disk I/Os), linked allocation requires only rewriting the predecessor's pointer (plus writing the new block), and indexed allocation requires only updating the index entry (plus writing the new block) — so indexed/linked need far fewer I/Os than contiguous.
    Asked inGATE 2022UGC NET 2016
  • External fragmentation occurs in contiguous allocation only; linked and indexed allocation do not suffer external fragmentation (any free block can be used), though they may suffer internal fragmentation from partially filled blocks.
    Asked inGATE 2017
  • Space accounting with bookkeeping: total space for a file = (number of blocks used) × (block size + per-block bookkeeping bytes), where number of blocks = ceil(file size / block size);
    compare totals for different block sizes to choose the better one.
    Asked inGATE 2005
  • Unix-style inodes use indexed allocation extended with single, double and triple indirect blocks (a multilevel index), which is how very large files are supported; the direct pointers cover small files cheaply.
    Asked inGATE 2008GATE 2004
  • For appending a block at the end of a file: in linked allocation the new data block is written and the last block's pointer is updated, which needs the address of the last block (a tail pointer in the directory entry; otherwise the whole chain must be traversed first); in indexed allocation the new data block is written and one index entry is filled (the index block is rewritten); contiguous allocation may need to relocate/copy the whole file if the next block is occupied.
    Asked inUGC NET 2016
Pages 1–2 of 2