Sub topic 15
Contiguous allocation
This chapter covers how an operating system can store each file in one unbroken run of disk blocks. It assumes you know what a disk block is, how the file system layout records files (the previous chapters on disk structure and file system layout), and basic arithmetic with powers of two.
The idea
In contiguous allocation, a file of n blocks occupies n consecutive blocks on the disk. The directory entry needs only two numbers:
- the starting block
b - the length
n, in blocks
The file's blocks are b, b+1, ..., b+n-1. To find logical block i of the file, the file system computes b + i. No pointers are read and no index is searched.
Why it is fast
Because the blocks are adjacent, the disk head seeks once to reach the start and then reads the rest with little or no further seeking. Sequential reads run at close to the disk's transfer rate. Direct access is also cheap: to read block i, compute b + i and issue one request. This makes contiguous allocation efficient for both sequential and direct access (the access methods from the earlier chapter).
Recall the access-time model: seek + rotational latency + transfer. For a contiguous file, the seek and rotational delay are paid roughly once per file. For a scattered file, they can be paid once per block.
The costs
External fragmentation
Files are created and deleted over time. Deleting a file leaves a hole. After many operations, free space is split into many small holes. The total free space may be enough for a new file, but no single hole is large enough. This is external fragmentation (wasted space outside allocated units).
Compaction copies files together to merge the holes into one. It restores a large free region, but it is slow, because it moves a lot of data. It is often done offline.
Hole selection
When a new file needs n blocks, the system picks a hole by one of the usual dynamic-storage strategies:
| Strategy | Rule | Typical behavior |
|---|---|---|
| First fit | Take the first hole that is big enough | Fast, and usually good in practice |
| Best fit | Take the smallest hole that is big enough | Leaves tiny, unusable leftovers |
| Worst fit | Take the largest hole | Tends to break up large holes; usually performs worst |
First fit and best fit are about equally good on utilization. First fit is usually faster.
Knowing the file size in advance
A file must be given its space when it is created, but its final size is often unknown. Two problems follow:
- If the user over-estimates, space is wasted. This is internal waste, because the reserved blocks sit unused inside the file's extent.
- If the file outgrows its extent, the system must find a larger hole and copy the file there, or fail. If no larger hole exists, the write fails even when the disk has plenty of free blocks in total.
Growth is awkward
Appending to a file only works if the blocks right after the file are free. Otherwise the file has to move. Files that shrink leave a tail that is hard to reuse.
Extent-based allocation
Many modern file systems use a compromise. A file is stored in one or more contiguous chunks called extents. The directory entry or inode holds a list of (start, length) pairs. If a file outgrows its extent, the system allocates another extent and links to it. This keeps most of the speed of contiguous layout while avoiding the strict size-in-advance rule. Examples include ext4 and NTFS. Extents also shrink metadata: one pair can describe thousands of blocks, where a per-block pointer scheme would need thousands of pointers.
Worked example: finding a block and a hole
A disk has 1,000 blocks of 1 KB each. A file has start block 200 and length 50.
- Logical block 0 is at physical block 200.
- Logical block 37 is at physical block 237.
- Logical block 50 does not exist. Valid indexes are 0 to 49, so the last block is 249.
Now suppose the free holes, in disk order, have sizes 10, 4, 20, 18, 7, 9, 12, 15 blocks. A request needs 12 blocks.
- First fit picks the 20-block hole (the first with size at least 12). Leftover: 8.
- Best fit picks the 12-block hole. Leftover: 0.
- Worst fit picks the 20-block hole. Leftover: 8.
Now a second request arrives for 10 blocks, after the first one.
- First fit: the 10-block hole at the front is big enough and is taken. Leftover: 0.
- Best fit: the 10-block hole is the smallest that fits. Leftover: 0.
- Worst fit: the largest remaining hole is the 18-block one. Leftover: 8.
GATE-style questions often ask which requests can or cannot be satisfied under each strategy, so trace the hole list step by step and update it after each allocation.
A short simulation
This program applies the three strategies to a list of holes and reports which hole each request uses. It returns the hole index, or -1 if no hole fits.
def allocate(holes, size, strategy):
# holes: list of free-hole sizes in disk order (modified in place)
candidates = [i for i, h in enumerate(holes) if h >= size]
if not candidates:
return -1
if strategy == "first":
i = candidates[0]
elif strategy == "best":
i = min(candidates, key=lambda k: holes[k])
elif strategy == "worst":
i = max(candidates, key=lambda k: holes[k])
else:
raise ValueError("unknown strategy")
holes[i] -= size
return i
for strategy in ("first", "best", "worst"):
holes = [10, 4, 20, 18, 7, 9, 12, 15]
chosen = [allocate(holes, s, strategy) for s in (12, 10)]
print(strategy, chosen, holes)The output lists the hole index chosen for each request, followed by the remaining hole sizes. A hole of size 0 is simply unusable.
Common errors
- Treating the number of blocks as the last block number. A file with start
band lengthnends atb + n - 1. - Confusing external fragmentation (free space split into small holes, a problem of contiguous allocation) with internal fragmentation (unused space in the last block of a file, which affects every scheme).
- Forgetting that the free-space total can exceed the request while no single hole does.
- Assuming compaction is free. It copies data and may need the file system to be idle.
Summary of trade-offs
| Property | Contiguous allocation |
|---|---|
| Sequential access | Excellent |
| Direct access | Excellent, one addition |
| Metadata per file | Very small (start, length) |
| External fragmentation | Yes |
| File growth | Hard; may require moving the file |
| Size known at creation | Usually required |
Exercise
A disk has free holes of 30, 8, 15, 22 and 11 blocks, in that order. Requests for 10 blocks, then 20 blocks, then 12 blocks arrive. Under best fit, which requests succeed, and what are the remaining hole sizes?
Show answer
Best fit for 10 blocks takes the 11-block hole (smallest that fits), leaving 1. Holes: 30, 8, 15, 22, 1. For 20 blocks, the smallest hole that fits is 22, leaving 2. Holes: 30, 8, 15, 2, 1. For 12 blocks, the smallest hole that fits is 15, leaving 3. Holes: 30, 8, 3, 2, 1. All three requests succeed.
Exercise
A file has start block 500 and length 64 in a contiguous scheme. Which physical block holds logical block 63, and why would a request for logical block 64 be rejected?
Show answer
Logical block 63 is at 500 + 63 = 563, the last block of the file. Logical block 64 would be at 564, but the file only has blocks with logical indexes 0 to 63, so index 64 is past the end and is rejected.