Directory structures
Directory structures
Recorded by Dr. Krithika L.B. Slot m4-directories
Organising files into single-level, two-level, tree and graph directories; implementing directories with lists and hash tables; and placing file blocks with contiguous, linked and indexed allocation.
By the end of this session you should be able to:
A directory is a symbol table that translates file names into file-control-block entries. Operations: search, create, delete, list, rename a file, and traverse the file system. The organisation should be efficient (locate a file quickly), allow convenient naming (same name for different users' files), and support grouping.
All files in one directory. Simple, but names must be unique across all users and there is no grouping.
A master file directory (MFD) with one user file directory (UFD) per user. Different users can use the same name; path = user/name. No grouping within a user.
Tree-structured — directories contain files and subdirectories to any depth. Absolute path from the root (/home/ravi/os/lab1.sh) or relative to the current directory (os/lab1.sh). Efficient searching and grouping. Used by every modern OS.
Lets directories share subdirectories or files (a team folder) via links: hard links (another directory entry for the same inode) or symbolic links (a file containing a path). Problems: a file has several names (traversal may count it twice) and deletion — keep a reference count and delete only when it reaches 0.
Allowing links to any directory may create cycles — a search could loop forever and a self-referencing directory may never reach count 0. Solutions: forbid links to directories, use garbage collection, or detect cycles when a link is added.
File names with pointers to data blocks. Simple to program; finding a file needs a linear search — slow for large directories (a sorted list or tree helps).
A linear list plus a hash table keyed on the file name: lookup in roughly O(1). Must handle collisions (chained overflow) and a fixed table size.
Each file occupies a set of contiguous blocks; the directory stores start and length.
✔ Both sequential and direct access are fast (block b of file = start + b), minimal seeks. ✘ External fragmentation (as in Session 4.2), and the file size must be known in advance — growing a file may require moving it.
Each file is a linked list of blocks scattered anywhere; the directory stores the first and last block; each block holds a pointer to the next.
✔ No external fragmentation, files grow easily. ✘ Only sequential access is efficient (reaching block i needs i reads); pointers use space; one damaged pointer loses the rest of the file.
FAT (File-Allocation Table), used by MS-DOS and USB drives, keeps all the pointers in one table at the start of the volume — it can be cached in memory, making random access much faster.
All pointers of a file are brought together in an index block; entry i points to the i-th block. The directory stores the index block's address.
✔ Direct access without external fragmentation. ✘ An index block is wasted for tiny files; large files need several: linked index blocks, a multilevel index, or the combined scheme.
The combined scheme of the UNIX inode: 12 direct pointers (small files need no extra blocks), then a single indirect, a double indirect and a triple indirect pointer.
With 4 KB blocks and 4-byte pointers (1024 pointers per block): direct = 48 KB, single indirect adds 4 MB, double indirect 4 GB, triple indirect 4 TB.
| Method | Sequential access | Direct access | Fragmentation | Growth |
|---|---|---|---|---|
| Contiguous | Excellent | Excellent | External | Hard |
| Linked (FAT) | Good | Poor (good with cached FAT) | None | Easy |
| Indexed (inode) | Good | Good | None (index overhead) | Easy |
The file system must also track free blocks: a bit vector (1 bit per block — easy to find contiguous runs), a linked list of free blocks, grouping (first free block stores addresses of n free blocks) or counting (address + run length).
Directory structures
Recorded by Dr. Krithika L.B. Slot m4-directories
File allocation methods
Recorded by Dr. Krithika L.B. Slot m4-allocation
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0Two-level directory.
To reach block i you must follow i pointers, each needing a disk read.
14 + 4 = block 18.
4096 / 4 = 1024 pointers × 4 KB = 4 MB.
It keeps a reference (link) count and frees the file when the count reaches zero.
Files end up as blocks on a disk. How is that disk organised, how are requests to it ordered, and how do we survive a disk failure? Module 5 starts with devices.
Go to Module 5 — Session 5.1: Disk structure and RAID →