File Systems

A File System is the method an operating system uses to control how data is stored, organized, and retrieved on a storage device, like a hard disk or SSD. It acts like a librarian for your data, keeping everything in order so you can find it easily. 🗂️

Without a file system, a storage disk would just be a massive, jumbled block of data. You wouldn’t know where one file ends and another begins. The file system creates a structured hierarchy of files and directories, making data management possible.


Components of a File System

1. Files

A file is a collection of related information that is recorded on secondary storage. From the OS’s perspective, it’s the smallest logical unit of storage.

  • File Attributes: The file system stores metadata about each file, such as:
    • Name: The symbolic file name.
    • Type: The file extension (e.g., .txt, .jpg, .exe).
    • Location: A pointer to the device and the file’s location on the device.
    • Size: The current size of the file.
    • Protection: Controls who can read, write, or execute the file.
    • Timestamps: Dates and times of creation, last modification, and last access.
  • File Operations: The OS provides system calls to perform operations like:
    • Creating a file
    • Writing to a file
    • Reading from a file
    • Deleting a file
    • Renaming a file

2. Directories

A directory (or folder) is a container that holds a group of files and other directories. Directories are used to organize files in a logical, hierarchical structure.

  • Single-Level Directory: The simplest structure. All files are in one single directory. This is easy to implement but becomes chaotic as the number of files grows.
  • Two-Level Directory: Each user gets their own main directory (a Master File Directory), and all their files are placed within it. This solves the naming conflict problem between users.
  • Tree-Structured Directory: This is the most common structure used in modern operating systems like Windows and Linux. It allows users to create directories inside other directories, forming a tree. This provides maximum flexibility for organizing files.

File Allocation Methods

This describes how the blocks of a file are physically placed on the storage disk.

1. Contiguous Allocation

Each file occupies a contiguous set of blocks on the disk. It’s like writing an entire chapter of a book on consecutive pages.

  • Advantage: Fast read performance because the disk head doesn’t have to move much to read the entire file.
  • Disadvantage: Suffers from external fragmentation. It can be hard to find a large enough continuous block of free space for a new file.

2. Linked Allocation

Each file is a linked list of disk blocks, which can be scattered anywhere on the disk. The directory entry contains a pointer to the first block, and each block contains a pointer to the next one.

  • Adv.ntage: Solves the external fragmentation problem. A file can grow as long as there are free blocks.
  • Disadvantage: Slow random access. To access the Nth block of a file, you have to traverse the first N-1 blocks.

3. Indexed Allocation

This method brings all the pointers for a file’s blocks together into one location called an index block. The directory entry points to this index block.

  • Advantage: Solves the external fragmentation problem and provides fast random access. You can find any block’s location directly from the index block.
  • Disadvantage: Wastes space if the file is very small, as an entire block is used for the index. For very large files, a single index block may not be enough.