File attributes and operations in Operating Systems - Time & Space Complexity
When working with file attributes and operations, it is important to understand how the time taken changes as the number of files or operations grows.
We want to know how the cost of checking or modifying file attributes scales with more files or repeated actions.
Analyze the time complexity of the following code snippet.
for each file in directory:
read file attributes
if file is writable:
update last modified time
end if
end for
This code goes through every file in a directory, reads its attributes, and updates the last modified time if the file can be written to.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each file in the directory.
- How many times: Once for every file present in the directory.
As the number of files increases, the total time grows proportionally because each file requires reading attributes and possibly updating.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 attribute reads and checks |
| 100 | About 100 attribute reads and checks |
| 1000 | About 1000 attribute reads and checks |
Pattern observation: The work grows steadily and directly with the number of files.
Time Complexity: O(n)
This means the time needed increases in direct proportion to the number of files being processed.
[X] Wrong: "Updating file attributes takes the same time no matter how many files there are."
[OK] Correct: Each file requires separate operations, so more files mean more total time.
Understanding how file operations scale helps you reason about system performance and resource use in real tasks.
"What if the code also recursively processes files in subdirectories? How would the time complexity change?"