File allocation methods (contiguous, linked, indexed) in Operating Systems - Time & Space Complexity
When managing files on a disk, the way files are stored affects how long it takes to access them.
We want to understand how the time to find or read a file grows as the file size or disk size increases.
Analyze the time complexity of accessing a file using different allocation methods.
// Example: Accessing all blocks of a file
for (int i = 0; i < file_size; i++) {
read_block(block_address[i]);
}
This code reads each block of a file one by one using the block addresses stored according to the allocation method.
We look at how many times the system must find and read blocks.
- Primary operation: Reading each block of the file.
- How many times: Once per block, repeated for all blocks in the file.
As the file size grows, the number of blocks to read grows too.
| Input Size (blocks) | Approx. Operations (block reads) |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The number of operations grows directly with the file size.
Time Complexity: O(n)
This means the time to access the whole file grows linearly with the number of blocks in the file.
[X] Wrong: "Accessing any block in a file always takes the same time regardless of allocation method."
[OK] Correct: Different allocation methods affect how quickly the system finds each block, so access time can vary.
Understanding how file storage methods affect access time helps you explain system design choices clearly and confidently.
What if we used an indexed allocation with a multi-level index? How would that change the time complexity of accessing a block?
Practice
Solution
Step 1: Understand file storage methods
Contiguous allocation stores all file blocks one after another in a continuous space on disk.Step 2: Compare with other methods
Linked allocation stores blocks scattered with pointers; indexed uses an index block. Only contiguous is continuous.Final Answer:
Contiguous allocation -> Option DQuick Check:
Continuous blocks = Contiguous allocation [OK]
- Confusing linked allocation with contiguous
- Thinking indexed allocation stores blocks continuously
- Assuming hybrid is a standard method
Solution
Step 1: Recall linked allocation structure
In linked allocation, each file block stores a pointer to the next block, forming a chain.Step 2: Eliminate other options
Continuous sequence is contiguous; index block is indexed allocation; fixed-size table is unrelated.Final Answer:
Each file block contains a pointer to the next block -> Option BQuick Check:
Pointers in blocks = Linked allocation [OK]
- Mixing up index block with linked pointers
- Thinking linked allocation stores blocks continuously
- Confusing fixed-size table with file allocation
Solution
Step 1: Understand indexed allocation access
The system first reads the index block to find pointers to all data blocks.Step 2: Identify the first block read
Before accessing data blocks 5, 9, or 12, the index block must be read to know their locations.Final Answer:
The index block -> Option AQuick Check:
Index block read first in indexed allocation [OK]
- Choosing a data block instead of the index block
- Confusing linked allocation with indexed
- Assuming direct access without index
Solution
Step 1: Understand linked allocation requirements
Linked allocation requires each block to have a pointer to the next block for sequential access.Step 2: Identify the problem without pointers
Without pointers, the system cannot follow the chain, so blocks cannot be accessed in order.Final Answer:
Pointers are missing, so file blocks cannot be accessed sequentially -> Option CQuick Check:
Missing pointers break linked allocation chain [OK]
- Assuming fragmentation is not a problem
- Confusing contiguous storage with linked allocation
- Thinking index block is involved in linked allocation
Solution
Step 1: Recall contiguous allocation properties
Files are stored in continuous blocks; any corruption breaks the sequence.Step 2: Analyze impact of corrupted block 25
Since block 25 is in the middle, the file cannot be read beyond that point, making the entire file inaccessible.Final Answer:
The entire file becomes inaccessible due to break in continuity -> Option AQuick Check:
Corruption breaks contiguous file access [OK]
- Assuming only corrupted block is lost
- Thinking pointers or index block help in contiguous allocation
- Confusing linked or indexed allocation behavior
Ready to prove it?
Pass the test and this pattern moves unknown
