OS/Lesson 9 of 9/6 min read
File Systems, Disk Scheduling and I/O
Files, directories, allocation methods, inodes, free space management, journaling, disk scheduling algorithms, RAID, and I/O techniques.
On this page
File System
A file system organizes data on storage into files and directories and controls how it is stored and retrieved.
A file is a named collection of related data. Attributes: name, type, size, location, owner, permissions, timestamps.
File operations: create, open, read, write, seek, close, delete, truncate. The OS keeps an open file table per process (file descriptors) and a system-wide table.
Access methods
- Sequential: read in order (logs, tapes).
- Direct (random): jump to any block (databases).
- Indexed: an index points to blocks (ISAM).
Directory structures
- Single-level: one directory for everyone; name collisions.
- Two-level: one directory per user.
- Tree-structured: hierarchical directories with absolute and relative paths (most common).
- Acyclic graph: allows sharing files via links.
- General graph: allows cycles; needs garbage collection.
Allocation methods
How file blocks are placed on disk.
Contiguous File Allocation
Each file occupies consecutive blocks; the directory stores start block and length.
- Fast sequential and random access.
- External fragmentation; hard to grow files.
Linked Allocation
Each block points to the next block; the directory stores the first and last block.
- No external fragmentation; files grow easily.
- Random access is slow (must follow pointers); a broken pointer loses the rest of the file.
- FAT (File Allocation Table) is a variant that keeps all next-pointers in one table at the start of the disk.
Indexed Allocation
Each file has an index block holding pointers to all its data blocks.
- Fast random access, no external fragmentation.
- Overhead of the index block; large files need linked or multilevel index blocks.
Inode
Unix file systems (ext4) use an inode per file. It stores metadata (owner, permissions, size, timestamps, link count) and block pointers, but not the file name. Names live in directory entries that map name to inode number.
Inode block pointers (ext2/ext3 style):
- 12 direct pointers
- 1 single indirect (points to a block of pointers)
- 1 double indirect
- 1 triple indirect
This gives fast access to small files and still supports huge ones.
Hard Link
A hard link is another directory entry pointing to the same inode. The file is deleted only when the link count reaches 0. Cannot cross file systems or link directories.
Symbolic Link
A symbolic (soft) link is a separate file containing a path to the target. Can cross file systems; breaks (dangles) if the target is deleted.
Free space management
- Bit vector (bitmap): one bit per block, 1 = free. Easy to find contiguous free space.
- Linked list: free blocks linked together.
- Grouping / counting: store runs of free blocks as
(start, count).
Journaling
A crash in the middle of an update (e.g. while creating a file) can leave the file system inconsistent. Journaling file systems (ext4, NTFS, XFS) first write the intended changes to a log (journal), then apply them. After a crash, the journal is replayed or discarded, so recovery is fast and does not need a full fsck. This is the same idea as write-ahead logging in databases.
Disk structure
A hard disk (HDD) has platters, tracks, sectors and cylinders. Access time:
access time = seek time + rotational latency + transfer timeSeek time (moving the arm) dominates, so disk scheduling minimizes head movement. SSDs have no moving parts, so seek-based scheduling matters much less there.
Disk Scheduling
Example: cylinders 0 to 199, head at 53, request queue 98, 183, 37, 122, 14, 124, 65, 67.
| Algorithm | Idea | Total head movement |
|---|---|---|
| FCFS | Serve in arrival order | 640 |
| SSTF | Nearest request next | 236 |
| SCAN | Move to one end serving requests, then reverse (elevator) | 236 (toward 0 first) |
| C-SCAN | Move one way to the end, jump back to start, continue | More uniform wait |
| LOOK | SCAN but reverse at the last request, not the disk end | Less than SCAN |
| C-LOOK | C-SCAN but only to the last request | Less than C-SCAN |
SSTF
Shortest Seek Time First: pick the closest request. Good throughput, but far requests can starve (like SJF).
SCAN
SCAN (elevator algorithm): the head sweeps in one direction serving requests, then reverses. No starvation.
C-SCAN
Circular SCAN: serves only in one direction; on reaching the end it returns to the start without serving. Gives more uniform waiting time than SCAN.
LOOK
LOOK and C-LOOK: like SCAN and C-SCAN, but the head only goes as far as the last request in that direction.
RAID
RAID (Redundant Array of Independent Disks) combines disks for performance and/or reliability.
| Level | Technique | Min disks | Fault tolerance | Notes |
|---|---|---|---|---|
| RAID 0 | Striping | 2 | None | Fast, any disk failure loses everything |
| RAID 1 | Mirroring | 2 | 1 disk | 50% capacity, fast reads |
| RAID 5 | Striping + distributed parity | 3 | 1 disk | Good balance, slow writes (parity update) |
| RAID 6 | Striping + double parity | 4 | 2 disks | Safer for large arrays |
| RAID 10 | Mirroring + striping | 4 | 1 per mirror pair | Fast and safe, 50% capacity |
RAID is not a backup: deleting a file or ransomware affects all copies.
I/O techniques
- Programmed I/O: CPU polls the device; wastes cycles.
- Interrupt-driven I/O: device interrupts when ready; CPU still moves each word.
- DMA (Direct Memory Access): a DMA controller transfers whole blocks between device and memory; the CPU is interrupted only when done. Used for disks, network cards, GPUs. Details in I/O and parallelism.
Buffering, caching, spooling
- Buffering: temporary storage to handle speed mismatches between producer and consumer (single, double, circular buffers).
- Caching: keep copies of frequently used data in faster storage (page cache).
- Spooling: Simultaneous Peripheral Operations On-Line. Output for a device that cannot interleave (a printer) is queued on disk and sent one job at a time.
Device drivers
A device driver is kernel code that knows how to talk to one device type, presenting a standard interface (block devices, character devices, network devices) to the rest of the OS.
Quick revision
- Allocation: contiguous (fast, fragments), linked (no random access), indexed (inode).
- Inode stores metadata and block pointers, not the name.
- Hard link = same inode; soft link = path file.
- SSTF can starve; SCAN/LOOK are fair; C-SCAN gives uniform wait.
- RAID 5 survives one disk, RAID 6 two; RAID is not backup.