←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Directory Structures & File Allocation Methods (Contiguous, Linked, Indexed)

Study material and explanations for Unit 5

Directory Structures & File Allocation Methods (Contiguous, Linked, Indexed)

1. Directory Structure in Operating Systems

A modern computer hard disk stores ratusan of thousands of files. To organize these files cleanly, the OS uses Directories (Folders).

  • What is a Directory? A Directory can be viewed as a Symbol Table that translates human-readable file names into their corresponding directory entries (file metadata and disk locations).

Your DAVV Unit 5 syllabus lists 5 types of Directory Structures:

Figure 5.3: The 5 Types of Directory Structures in Operating Systems
Type 1Single-Level Directory
All files of all users are placed inside ONE single root folder (Causes naming collisions!)
Type 2Two-Level Directory
Master File Directory (MFD) gives each User their own separate User File Directory (UFD)
Type 3Tree-Structured Directory
Users can create unlimited Subdirectories (folders inside folders) forming an inverted tree
Type 4Acyclic-Graph Directory
Allows two users to SHARE the same subdirectory/file via Links (No cycles allowed)
Type 5General Graph Directory
Allows arbitrary links including Cycles (Requires Garbage Collection & Cycle Detection)

1. Single-Level Directory

  • Structure: The simplest directory structure. All files are contained in the exact same single directory, regardless of how many users or files exist.
  • Drawback (Naming Problem & Grouping Problem): Since all files are in one folder, no two files can have the same name (if User A creates test.c, User B cannot create a file named test.c!), and users cannot group their files by topic.

2. Two-Level Directory

  • Structure: Solves the naming collision between different users by creating a separate directory for each user!
    • At the top sits the MFD (Master File Directory), indexed by Username/Account Number.
    • Each MFD entry points to that user’s private UFD (User File Directory).
  • Benefit: Two different users can now both have a file named notes.txt (User1/notes.txt and User2/notes.txt).
  • Drawback: A user still cannot create subfolders inside their own UFD to organize their projects.

3. Tree-Structured Directory (Used in Windows/Linux!)

  • Structure: Extends the two-level directory into a Tree of arbitrary height! Starting from the Root Directory (/ or C:\), users can create their own Subdirectories (folders inside folders) to organize files logically by subject or project.
  • Path Names: Supports Absolute Path (starting from Root, e.g., C:\BCA\Sem6\os.pdf) and Relative Path (starting from Current Working Directory).
  • Drawback: Pure trees do not allow sharing of files or folders between two parent directories.

4. Acyclic-Graph Directory

  • Structure: A generalization of the Tree-Structured Directory that allows directories to share subdirectories and files! The exact same file or folder can appear in two different places at once using a Shared Link (Symbolic Link or Hard Link), provided there are NO cycles (loops) in the graph.
  • Benefit: Perfect for team projects where two programmers need direct access to the same shared project folder.

5. General Graph Directory

  • Structure: When links are added freely to an existing tree-structured directory, Cycles (closed loops) can accidentally form, turning it into a General Graph Directory.
  • Drawback: Needs Cycle-Detection algorithms (so a file-search program doesn’t get trapped in an infinite loop!) and Garbage Collection to know when a file can be safely deleted.

2. File Allocation Methods (How Files Are Stored on Disk Blocks)

A hard disk is divided into thousands of equal-sized physical Disk Blocks (Block 0, Block 1, Block 2...). When you save a file that needs several disk blocks, how does the OS allocate disk blocks to that file?

There are 3 major Disk File Allocation Methods (this is one of the most frequently asked 8-mark questions in Unit 5!):

Figure 5.4: The 3 Disk File Allocation Methods in Operating Systems
1. Contiguous Allocation
File occupies a continuous sequence of adjacent blocks (e.g., Blocks 10, 11, 12, 13)
2. Linked Allocation
File blocks are scattered anywhere; each block holds a pointer to the next block (`9 → 16 → 1 → 10`)
3. Indexed Allocation
All block pointers of a file are collected together inside one special Index Block!

Method 1: Contiguous Allocation

  • How It Works: Each file occupies a set of contiguous (adjacent) blocks on the disk. If a file is 4 blocks long and starts at Block 10, it will occupy Blocks 10, 11, 12, and 13.
  • Directory Entry Stores Only 2 Numbers:
    1. Starting Block Address (Start = 10)
    2. Length in Blocks (Length = 4)
  • Advantages:
    • Fastest performance! Minimal disk head movement (seek time) because all blocks are right next to each other.
    • Supports both Sequential and Direct (Random) Access effortlessly (to read the k-th block of a file starting at b, immediately go to disk block b + k!).
  • Disadvantages:
    • Suffers severely from External Fragmentation on the disk (requires disk defragmentation/compaction).
    • File Growth Problem: It is hard to know how big a file will grow when creating it; if the adjacent block is already occupied by another file, the file cannot grow in place!

Method 2: Linked Allocation

  • How It Works: Solves all problems of Contiguous Allocation! A file is stored as a Linked List of scattered disk blocks. The blocks may be scattered anywhere on the disk!
  • Directory Entry Stores: A pointer to the First Block (Start) and the Last Block (End) of the file.
  • Each disk block reserves a few bytes at the end as a Pointer to the Next Disk Block (e.g., Block 9 → Block 16 → Block 1 → Block 10 [EOF = -1]).
  • Advantages:
    • Zero External Fragmentation! Any free block on the disk can be used.
    • Files can grow dynamically as long as a single free block exists on the disk.
  • Disadvantages:
    • Does NOT support efficient Direct (Random) Access! To reach the 50-th block of a file, the disk head must slowly read and follow pointers through blocks 1, 2, 3 … 49 sequentially!
    • Pointer Overhead & Reliability Risk: Pointers waste space inside every block, and if even ONE pointer gets corrupted, the entire remainder of the file is lost! (Note: MS-DOS FAT — File Allocation Table improves this by storing all block links in one separate table at the start of the disk).

Method 3: Indexed Allocation

  • How It Works: Combines the best of both worlds (supports Direct Access without External Fragmentation)!
  • Instead of scattering pointers across every block like Linked Allocation, Indexed Allocation brings ALL the pointers of a file together into one single location called the file’s Index Block (or Inode in UNIX/Linux)!
  • Directory Entry Stores: Only the address of the Index Block (e.g., Index Block = 19).
  • Inside Block 19 (the Index Block) is an ordered array of disk block numbers ([9, 16, 1, 10, 25]). The i-th entry in the Index Block points directly to the i-th data block of the file!
Figure 5.5: How Indexed Allocation Works Using an Index Block
Directory EntryFile: notes.pdf | Index Block = 19
→ Points to →
Index Block (Block 19)Array of Block Pointers: [ 9 | 16 | 1 | 10 ]
→ Direct Access to →
Scattered Data BlocksBlocks 9, 16, 1, and 10 on Disk
  • Advantages of Indexed Allocation:
    • Supports fast Direct (Random) Access!
    • Zero External Fragmentation (data blocks can be placed anywhere on disk).
  • Disadvantage: For a tiny 1-block file, dedicating an entire extra Index Block wastes pointer space.

3. Master Comparison: Contiguous vs Linked vs Indexed Allocation

Basis Contiguous Allocation Linked Allocation Indexed Allocation
1. Storage Method Stored in continuous adjacent disk blocks. Stored in scattered blocks linked via pointers in each block. Stored in scattered blocks whose addresses are listed in an Index Block.
2. Directory Entry Stores Start Block + Length. Stores Start Block + End Block. Stores Index Block Address.
3. Direct (Random) Access? ✅ Yes (Fastest) ❌ No (Sequential traversal only) ✅ Yes (Fast via Index Block)
4. External Fragmentation? ❌ Yes (Needs Defragmentation) ✅ No (Zero fragmentation) ✅ No (Zero fragmentation)
5. Dynamic File Growth? Difficult (cannot expand if next block is full). Easy (just append a new free block to the end). Easy (up to the capacity of the Index Block / multi-level index).

💡 Exam Preparation Tips for DAVV BCA

  • Always draw the 3-column comparison table (Contiguous vs Linked vs Indexed Allocation) above—this question appears in almost every DAVV BCA OS paper!
  • For Directory Structures, draw small node hierarchies for Single-Level, Two-Level (MFD → UFD), and Tree-Structured directories.