Copy-on-write technique in Operating Systems - Time & Space Complexity
Analyzing time complexity helps us understand how the copy-on-write technique affects system performance as data size grows.
We want to know how the number of operations changes when copying memory using this technique.
Analyze the time complexity of the following simplified copy-on-write process.
function copyOnWrite(originalPage):
if originalPage is shared:
create newPage
copy data from originalPage to newPage
update reference to newPage
else:
use originalPage directly
return page
This code copies memory only when a shared page is modified, otherwise it reuses the original page.
Look for operations that repeat as input size grows.
- Primary operation: copying data from the original page to a new page.
- How many times: once per write to a shared page, and the copy size depends on the page size.
The time to copy grows with the size of the page being copied, but only when a write occurs.
| Input Size (page size in KB) | Approx. Operations (copying data) |
|---|---|
| 4 | 4 units of copy work |
| 16 | 16 units of copy work |
| 64 | 64 units of copy work |
Pattern observation: The copying work grows linearly with the page size, but only happens on writes, not on reads.
Time Complexity: O(n)
This means the time to copy data grows directly with the size of the memory page being copied.
[X] Wrong: "Copy-on-write always copies data immediately when a copy is made."
[OK] Correct: Copy-on-write delays copying until the data is actually modified, saving time and resources on simple copies.
Understanding copy-on-write time complexity shows you can reason about efficient memory use and performance, a valuable skill in system design discussions.
What if the system used smaller page sizes for copy-on-write? How would that affect the time complexity?
Practice
copy-on-write technique in operating systems?Solution
Step 1: Understand the purpose of copy-on-write
Copy-on-write delays copying data until a modification is made, saving resources.Step 2: Compare options with the concept
Only Data is copied only when it is modified, not when initially shared correctly states that copying happens only on modification, not immediately.Final Answer:
Data is copied only when it is modified, not when initially shared -> Option AQuick Check:
Copy-on-write = Copy on modification [OK]
- Thinking data copies immediately on sharing
- Believing data is never copied
- Assuming multiple copies improve speed
Solution
Step 1: Identify the trigger for copying in copy-on-write
Copy-on-write means copying happens only when data is changed or written to.Step 2: Match the correct event
Only When the data is modified correctly states copying occurs on modification.Final Answer:
When the data is modified -> Option BQuick Check:
Copy-on-write triggers on modification [OK]
- Confusing read with write triggers
- Thinking copy happens at system start
- Assuming copy on deletion
Solution
Step 1: Understand memory sharing in copy-on-write
Initially, both processes share the same memory pages without copying.Step 2: Calculate total memory usage
Since no modifications occur, memory is shared, so total used is 100MB, not doubled.Final Answer:
100MB -> Option DQuick Check:
Shared memory means total = original size [OK]
- Assuming memory doubles immediately
- Thinking child process uses zero memory
- Confusing copy-on-write with full copy
Solution
Step 1: Analyze snapshot behavior with copy-on-write
Snapshots should remain unchanged because copy-on-write protects original data by copying on modification.Step 2: Identify cause of snapshot changing
If snapshot changes, it means copy-on-write failed and data was shared incorrectly.Final Answer:
Copy-on-write was not properly implemented, causing shared data to change -> Option AQuick Check:
Snapshot changes mean copy-on-write failure [OK]
- Assuming snapshot created after modification
- Blaming unsupported file system without evidence
- Confusing deletion with modification
Solution
Step 1: Understand copy-on-write memory allocation on modification
Only the modified part is copied, so only 1MB is newly allocated.Step 2: Calculate total additional memory used
The rest of the 499MB remains shared, so additional memory is exactly 1MB.Final Answer:
1MB -> Option CQuick Check:
Copy-on-write copies only changed data [OK]
- Assuming entire memory is copied
- Adding modified size to total incorrectly
- Thinking no memory is used on modification
Ready to prove it?
Pass the test and this pattern moves unknown
