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?"
Practice
Solution
Step 1: Understand the role of free space management
Free space management is about tracking storage space availability.Step 2: Identify the correct purpose
It helps the system know which storage parts are free or occupied.Final Answer:
To keep track of which parts of storage are free or used -> Option CQuick Check:
Free space management = tracking free/used storage [OK]
- Confusing free space management with file permissions
- Thinking it manages CPU or network tasks
- Assuming it deletes files automatically
Solution
Step 1: Recall common free space management methods
Common methods include bitmaps and linked lists to track free blocks.Step 2: Identify the correct method
Linked lists are widely used to keep track of free space blocks.Final Answer:
Linked list -> Option AQuick Check:
Common method = linked list [OK]
- Choosing data structures not typically used for free space
- Confusing stacks or trees with free space tracking
- Assuming hash tables are used for free space management
10110010. Which blocks are free?Solution
Step 1: Understand bitmap indexing
Bitmap bits index blocks starting at 0 from left to right: bit 0 = block 0.Step 2: Identify bits with value 1
Bitmap: 1(0),0(1),1(2),1(3),0(4),0(5),1(6),0(7). Blocks with 1 are 0, 2, 3, and 6.Step 3: Verify bit 7
Bit 7 is 0, so block 7 is used, not free.Final Answer:
Blocks 0, 2, 3, and 6 -> Option BQuick Check:
Bits 1 = free blocks 0,2,3,6 [OK]
- Starting index from 1 instead of 0
- Mixing up bit positions and block numbers
- Confusing 0 and 1 meanings in bitmap
Solution
Step 1: Understand linked list free space tracking
Linked lists track free blocks by pointers linking them in order.Step 2: Identify cause of skipping blocks
If pointers are not updated after allocation or freeing, some free blocks may be skipped.Final Answer:
The linked list pointers are not updated correctly after allocation -> Option DQuick Check:
Incorrect pointer updates cause skipped free blocks [OK]
- Assuming disk is full without checking pointers
- Blaming file system support instead of pointer errors
- Confusing hardware issues with software pointer bugs
Solution
Step 1: Understand bitmap allocation
Bitmap shows which blocks are free (1) or used (0).Step 2: Efficient allocation method
Scanning bitmap from start to find first 5 free blocks is efficient and safe.Final Answer:
Scan the bitmap from start and pick the first 5 free blocks -> Option AQuick Check:
Scan bitmap for free blocks to allocate efficiently [OK]
- Picking blocks randomly without checking free status
- Assuming only end blocks can be allocated
- Deleting files unnecessarily before allocation
Ready to prove it?
Pass the test and this pattern moves unknown
