Module 9 · 16 min read · Free chapter
File systems and disk scheduling
Indexed allocation gives direct access without external fragmentation, and an inode with 4 KB blocks and 4-byte addresses reaches about 4 TB. Disk access time is seek time plus rotational latency plus transfer time. SSTF cuts head movement but can starve requests, SCAN and C-SCAN sweep the disk, and RAID 5 survives one disk failure.
Start from zero: the office record room
Walk into the record room of a government office. Thousands of files sit in numbered almirahs, and on the clerk's desk is an index register that says, for each file, which almirah, which shelf and which position it is in. Without that register, nobody could find anything, and new files would be pushed wherever a gap was left.
A disk is that record room, and the file system is the clerk with the register. It gives every file a name, remembers which disk blocks hold it, keeps track of the free blocks, and arranges files in folders so that people can find them.
There is one more problem. The disk has a single moving arm, like a clerk who must walk to each almirah, so the order in which requests are served decides how much walking is done. In this chapter we study files, directories, the ways blocks are allocated, the inside of a hard disk, disk scheduling and RAID.
Why it matters. Your photos, exam forms and bank statements all live in files. A good file system keeps them safe and quick to reach, and a poor one loses space, time or even data.
Files and directories
A file is a named collection of related data on secondary storage. The OS keeps its attributes: name, a unique identifier, type, location on disk, size, protection (who may read, write or execute) and timestamps. The basic operations are create, open, read, write, reposition (seek), delete and truncate.
Opening a file is a separate step for speed. The OS finds the file once, copies its details into an open-file table in memory, and returns a small handle, so later reads need no directory search. Files are read either by sequential access, one record after another like a tape, or by direct access, jumping straight to record number n, as a reservation system does.
A directory maps names to files, and its shape has changed over the years.
| Structure | Idea | Limitation |
|---|---|---|
| Single-level | one directory for everyone | names clash; no grouping |
| Two-level | one directory per user | users cannot easily share files |
| Tree | folders inside folders, with path names | a file has exactly one parent |
| Acyclic graph | a file or folder can appear in several places through links | deletion and cycles need care |
In a tree, an absolute path starts from the root, such as /home/asha/notes.txt, and a relative path starts from the current directory. Sharing is done with links. A hard link is a second directory entry pointing to the same inode, and the file is removed only when its link count drops to zero. A symbolic (soft) link is a tiny file that stores a path, so it can cross file systems, but it dangles if the target is deleted.
Why it matters. Every command that copies, moves or shares a file works on these structures. The hard-link and soft-link difference is a common one-mark question.
Allocation methods: contiguous, linked and indexed
A disk is divided into fixed-size blocks, and the file system must decide which blocks each file gets. There are three basic methods.
Contiguous allocation gives a file a run of consecutive blocks, so the directory stores only the start block and the length. Reading is very fast and direct access is easy: block i of a file starting at b is simply block . But it suffers external fragmentation, as in memory, and a file cannot grow if the next block is taken.
Linked allocation lets a file's blocks lie anywhere, with each block holding a pointer to the next. There is no external fragmentation and files grow freely. But reaching block i needs i disk reads, so only sequential access is efficient, some space in each block goes to the pointer, and one damaged pointer loses the rest of the file.
A file-allocation table (FAT) improves linked allocation by moving all the pointers into one table at the start of the disk, with one entry per block. The table can be cached in memory, so following the chain becomes fast.
Indexed allocation collects all the block addresses of a file into one index block. The directory points to the index block, and block i is found by reading entry i, so direct access is fast and there is no external fragmentation. The cost is a whole index block even for a tiny file, and a large file needs more than one index block.
| Method | Direct access | External fragmentation | File growth |
|---|---|---|---|
| Contiguous | fast | yes | hard |
| Linked | slow: follow the chain | no | easy |
| Indexed | fast | no | easy, up to the index size |
Why it matters. The choice decides how fast a database can jump to a record and how well a disk copes with files that grow. Real file systems mix these ideas, using runs of contiguous blocks inside an indexed scheme.
Inodes and free-space management
Unix-like systems use a smart form of indexed allocation. Each file has an inode holding its attributes and 15 block addresses: 12 direct addresses, one single indirect address pointing to a block full of addresses, one double indirect address and one triple indirect address. Small files use only the direct addresses, while huge files use the indirect ones.
Worked example 1 (maximum file size). Let blocks be 4 KB and addresses 4 bytes, so one index block holds addresses. The inode can then reach
Multiplying by 4 KB gives 48 KB from direct blocks, 4 MB from single indirect, 4 GB from double indirect and 4 TB from triple indirect. So the largest file is a little over 4 TB, and the triple indirect part dominates.
Worked example 2 (index blocks for one file). A 100 MB file has data blocks. The direct and single indirect addresses cover blocks, leaving blocks for the double indirect part.
Those need second-level index blocks plus the one double indirect block itself. With the single indirect block, the file uses index blocks besides the inode.
Free-space management. The file system must also know which blocks are free, and there are four common ways.
- A bit vector keeps one bit per block, so finding a run of free blocks is fast with bit operations.
- A linked list chains free blocks together, which wastes no extra space but is slow to scan.
- Grouping stores the addresses of n free blocks in the first free block, so many free blocks are found with one read.
- Counting stores a start address and a count for each run of free blocks, which suits contiguous allocation.
Worked example 3. A 1 TB disk with 4 KB blocks has blocks. Its bit vector needs bits, which is bytes, or 32 MB.
Why it matters. Inode numericals appear in GATE almost every year, usually as maximum file size or as the count of index blocks. The method is always to find the number of addresses per block first.
Inside a hard disk: structure and access time
A hard disk drive stacks several metal platters on a spindle that spins them at a constant speed, such as 7200 rpm. Each platter has two surfaces, and each surface has its own read/write head on an arm; all arms move together on one actuator. The heads fly a tiny distance above the surface on a cushion of air and never touch it in normal use.
Each surface is divided into concentric rings called tracks, and each track into sectors, traditionally 512 bytes each. The set of tracks at the same arm position on all surfaces is a cylinder, and data in one cylinder can be read without moving the arm.
Worked example 4 (capacity). A disk has 4 platters (8 surfaces), 10,000 tracks per surface, 500 sectors per track and 512 bytes per sector. Its capacity is
which is 20.48 GB in decimal units.
Reading a sector takes three steps. The arm must seek to the right cylinder, the head must wait for the sector to rotate underneath, which is rotational latency, and then the data passes under the head, which is transfer time. So access time is seek time plus rotational latency plus transfer time.
Worked example 5 (access time). At 7200 rpm, one rotation takes s, which is 8.33 ms, so the average rotational latency is half of that, about 4.17 ms. With an average seek of 5 ms and a 4 KB transfer at 100 MB/s, which takes about 0.04 ms, the average access time is about ms.
Seek time dominates, and it depends on how far the arm moves. That is why the OS orders disk requests to reduce total arm movement.
Why it matters. A disk access takes milliseconds while a memory access takes nanoseconds, a gap of about a million times. This gap explains page-fault costs, disk scheduling and the value of caching.
Disk scheduling: FCFS, SSTF, SCAN, C-SCAN and LOOK
Many processes send requests to one disk, and the OS chooses the order. The aim is to reduce total head movement, measured in cylinders, while still being fair. We use one example throughout: cylinders 0 to 199, the head at 50 and moving towards higher numbers, and the queue 82, 170, 43, 140, 24, 16, 190.
FCFS serves requests in arrival order: 50 → 82 → 170 → 43 → 140 → 24 → 16 → 190. The movement is cylinders. It is fair but wild, swinging back and forth across the disk.
SSTF (shortest seek time first) always goes to the nearest pending request. From 50 the nearest is 43, then 24, 16, 82, 140, 170 and 190, giving cylinders. It is much better, but like SJF it can starve requests far from the head if new nearby ones keep arriving.
SCAN (the elevator method) moves the head in one direction to the end of the disk, serving requests on the way, then reverses. Going up: 50 → 82 → 140 → 170 → 190 → 199, then down to 43, 24, 16, giving cylinders.
LOOK is SCAN that turns at the last request instead of the disk end. It goes up to 190 and back down to 16, giving cylinders.
C-SCAN (circular SCAN) serves requests in one direction only. It goes up to 199, jumps back to 0 without serving anything, and goes up again to 43. Counting the return jump, the movement is ; without it, . C-LOOK turns at 190, jumps to 16 and goes up to 43: with the jump, or without it.
| Method | Order served | Head movement |
|---|---|---|
| FCFS | 82, 170, 43, 140, 24, 16, 190 | 642 |
| SSTF | 43, 24, 16, 82, 140, 170, 190 | 208 |
| SCAN | 82, 140, 170, 190, (199), 43, 24, 16 | 332 |
| LOOK | 82, 140, 170, 190, 43, 24, 16 | 314 |
| C-SCAN | 82, 140, 170, 190, (199, 0), 16, 24, 43 | 391, or 192 without the jump |
| C-LOOK | 82, 140, 170, 190, 16, 24, 43 | 341, or 167 without the jump |
Why prefer C-SCAN over SCAN? Just after SCAN passes a cylinder, a new request there must wait for a full trip out and back, while requests at the far end wait less. C-SCAN treats the disk as a circle, so waiting times are more uniform.
Why it matters. Disk scheduling numericals are short, exact and frequent in GATE and ISRO papers. The only real trap is the convention: read the direction of movement and whether the return jump is counted.
RAID levels: the basics
A single disk can fail and lose everything, and it can deliver only so much speed. RAID (redundant array of independent disks) combines several disks so that they act as one, faster or safer or both. Two ideas are used: striping, which spreads consecutive blocks over several disks so they can be read in parallel, and redundancy, which stores extra copies or parity so that a failed disk can be rebuilt.
Parity is the key trick. For a stripe of data blocks, the parity block is their bitwise XOR. If any one block is lost, XOR of all the survivors, including the parity, gives it back.
| Level | Idea | Usable capacity with n disks | Survives |
|---|---|---|---|
| RAID 0 | block striping, no redundancy | n disks | no failure |
| RAID 1 | mirroring: every block written twice | half of the raw capacity | one disk of each pair |
| RAID 4 | striping with parity on one dedicated disk | disks | one failure |
| RAID 5 | striping with parity spread over all disks | disks | one failure |
| RAID 6 | striping with two independent parities | disks | two failures |
Worked example 6. Five 2 TB disks give 10 TB under RAID 0. Under RAID 1, four of them form two mirrored pairs and give 4 TB, with one disk left as a spare. Under RAID 5 they give TB, and under RAID 6 they give TB.
RAID 4 has a bottleneck: every write must update the single parity disk. RAID 5 spreads the parity blocks over all disks, removing that bottleneck, which is why it is the common choice for file servers. RAID 0 is fastest but the least safe, since losing any one disk loses the whole array.
Why it matters. Bank and railway servers run on RAID so that a dead disk does not stop service. RAID is not a backup, though; a file deleted by mistake is deleted on every disk at once.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about file systems and disks in four ways:
- Compare the allocation methods, or the directory structures, with their merits and drawbacks.
- Compute inode limits, disk capacity, access time or bit-vector size from given figures.
- Trace disk-scheduling methods on a request queue and compare the head movements.
- Explain RAID levels and recommend one for a stated need such as speed or safety.
Practice questions (our own, not previous-year questions).
- Compare contiguous, linked and indexed allocation of disk blocks with a diagram of each, and state which supports direct access efficiently. (15 marks)
- A disk has cylinders 0 to 299 and the head is at 120 moving outward. For the queue 45, 260, 130, 10, 190, 75, 285, find the total head movement under FCFS, SSTF, SCAN and C-LOOK. (15 marks)
- Explain RAID levels 0, 1 and 5 and recommend a level for a bank's transaction server, giving reasons. (10 marks)
Answer plan for question 3.
- Define RAID as the use of several disks together for speed through striping and for safety through redundancy.
- Explain that RAID 0 stripes blocks across all disks, gives the full capacity and the best speed, but loses everything if one disk fails.
- Explain that RAID 1 mirrors every disk, survives a disk failure and gives fast reads, but uses half the raw capacity.
- Explain that RAID 5 stripes data with parity spread across the disks, gives disks of capacity and survives one failure.
- Recommend RAID 5, or RAID 6 for very large arrays, for a bank server, because it balances safety, capacity and read speed.
- Add that RAID must be combined with regular backups, because it does not protect against deletion or corruption.
Quick revision
- A file is a named collection of data with attributes, operations and sequential or direct access.
- A hard link shares the inode, while a soft link stores a path and breaks if the target goes.
- Contiguous allocation is fast but fragments, linked allocation has no direct access, and indexed allocation gives direct access through an index block.
- With 4 KB blocks and 4-byte addresses, an index block holds 1024 addresses and an inode reaches a little over 4 TB.
- A bit vector needs one bit per block, so a 1 TB disk with 4 KB blocks needs 32 MB.
- Capacity is surfaces × tracks × sectors × bytes per sector, and a cylinder is the same track on all surfaces.
- Access time is seek time plus rotational latency plus transfer time, and average latency is half a rotation.
- For the chapter's queue the movements are FCFS 642, SSTF 208, SCAN 332, LOOK 314, C-SCAN 391 and C-LOOK 341.
- SSTF can starve requests, and C-SCAN gives the most uniform waiting.
- RAID 0 stripes, RAID 1 mirrors, RAID 5 has distributed parity with capacity, and RAID 6 survives two failures.