Free space management in Operating Systems - Time & Space Complexity
When managing free space in an operating system, it's important to know how the time to find or update free space changes as the disk size grows.
We want to understand how the system's work increases when handling more free blocks.
Analyze the time complexity of the following code snippet.
// Simple free space search using a bitmap
for (int i = 0; i < total_blocks; i++) {
if (bitmap[i] == 0) { // block is free
allocate_block(i);
break;
}
}
This code scans a bitmap to find the first free block and allocates it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop scanning the bitmap array to find a free block.
- How many times: Up to the total number of blocks on the disk.
As the number of blocks increases, the time to find a free block grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows linearly as the number of blocks increases.
Time Complexity: O(n)
This means the time to find a free block grows directly with the number of blocks on the disk.
[X] Wrong: "Finding a free block always takes the same time no matter how big the disk is."
[OK] Correct: Because the search may need to check many blocks, the time depends on how many blocks there are and where the free block is located.
Understanding how free space management scales helps you reason about system efficiency and design better storage solutions.
"What if we used a linked list to track free blocks instead of scanning a bitmap? How would the time complexity change?"