1. Introduction
In many situations, operating systems need to duplicate a process's memory, especially during process creation using system calls such as fork() in UNIX-like systems.
A straightforward approach would copy the entire address space immediately.
However, this is inefficient because:
Copying large memory regions is expensive
Most pages are never modified
The child process may immediately execute another program (
exec())
To avoid unnecessary copying, operating systems use Copy-on-Write (COW).
Copy-on-Write is one of the most important optimizations used in modern virtual memory systems.
2. What is Copy-on-Write (COW)?
Copy-on-Write is a memory management technique in which multiple processes share the same physical memory pages until one of them attempts to modify a page.
Only then is a separate copy created.
Key Idea
Share Memory First
↓
Copy Only When Writing
3. Why Copy-on-Write is Needed
Problem Without COW
Suppose a process contains:
500 MB Memory
When fork() is called:
Parent Process
↓
Copy Entire 500 MB
↓
Child Process
Problems:
Large memory overhead
Slow process creation
Wasted copying
4. Observation Behind COW
After fork():
Parent usually continues execution
Child often executes
exec()Most memory pages remain unchanged
Therefore:
Copying Everything Immediately
↓
Unnecessary Work
5. Basic Concept of COW
Instead of duplicating memory immediately:
Parent and child share pages
Shared pages are marked read-only
Copying is postponed
Actual copying happens only when a write occurs.
Conceptual Flow
fork()
↓
Share Pages
↓
Read Operations
↓
No Copy
↓
Write Operation
↓
Create Copy
6. Memory State Before fork()
Suppose Parent Process contains:
Page A
Page B
Page C
Memory:
Parent
↓
A
B
C
Each page has its own frame in RAM.
7. Memory State After fork()
Without COW:
Parent Pages
↓
Copy Everything
↓
Child Pages
With COW:
Parent
↓
Shared Pages
↑
Child
Both processes reference the same physical pages.
8. How Sharing is Possible
The operating system:
Updates page tables
Points both processes to same frames
Marks pages as read-only
Example:
Frame 10 ← Parent Page A
Frame 10 ← Child Page A
Both use the same frame.
9. Read Operations Under COW
Suppose Parent reads Page A.
Read Page A
↓
No Modification
↓
Continue
No copy is created.
Similarly:
Child Reads Page A
↓
Continue
Still no copying.
10. Why Read Operations are Safe
Reading does not change memory contents.
Therefore:
Multiple Processes
↓
Read Same Page
↓
No Conflict
Shared pages remain valid.
11. What Happens During a Write?
Suppose Child modifies Page A.
Example:
A = 100;
The page is marked read-only.
Therefore:
Write Attempt
↓
Protection Violation
↓
Page Fault
12. Role of Page Fault in COW
Unlike normal demand paging:
Page Present in RAM
Yet a page fault still occurs.
Reason:
Write Attempt on Read-Only Page
This fault is intentional.
13. COW Page Fault Handling
When the page fault occurs:
OS performs the following steps.
Step 1
Identify shared page.
Step 2
Allocate a new free frame.
Step 3
Copy contents of old page.
Step 4
Update page table of writing process.
Step 5
Grant write permission.
Step 6
Restart instruction.
14. COW Flow
Write Attempt
↓
Page Fault
↓
Allocate New Frame
↓
Copy Old Page
↓
Update Page Table
↓
Resume Execution
15. Memory State After Copy
Initially:
Parent
↓
Frame 10
↑
Child
After Child writes:
Parent
↓
Frame 10
Child
↓
Frame 25
Now:
Parent retains original page
Child receives private copy
16. Example
Suppose:
int x = 5;
After fork():
Both Parent and Child share page.
Parent
printf("%d", x);
Output:
5
Child
x = 10;
Write occurs.
OS creates new page.
Final values:
Parent → x = 5
Child → x = 10
17. Why COW is Efficient
Without COW:
fork()
↓
Copy Entire Address Space
With COW:
fork()
↓
Copy Nothing Initially
Memory duplication occurs only when needed.
18. Memory Protection Mechanism
Copy-on-Write depends on memory protection.
Pages are initially:
Read = Allowed
Write = Not Allowed
Any write generates a fault.
This enables the OS to detect modification attempts.
19. Advantages of Copy-on-Write
19.1 Reduced Memory Usage
Shared pages avoid unnecessary duplication.
19.2 Faster Process Creation
fork() becomes extremely fast.
19.3 Better System Performance
Less copying means less overhead.
19.4 Efficient Resource Utilization
RAM is used only when necessary.
19.5 Supports Multiprogramming
More processes fit into memory.
20. Example of Memory Savings
Suppose:
Parent Process = 1 GB
Without COW:
fork()
↓
Additional 1 GB Required
Total:
2 GB RAM
With COW:
fork()
↓
Almost No Additional Memory
Only modified pages consume extra memory.
21. Disadvantages of Copy-on-Write
21.1 Page Fault Overhead
First write triggers a page fault.
21.2 Additional Complexity
OS must manage:
Shared pages
Reference counts
Protection bits
21.3 Less Effective for Write-Heavy Workloads
If most pages are modified:
Eventually
↓
All Pages Copied
Benefits reduce significantly.
22. Reference Counting in COW
Operating systems typically maintain:
Reference Count
for each shared page.
Example:
Page A
Reference Count = 2
Meaning:
Parent + Child
share the page.
When one process gets a private copy:
Reference Count Decreases
23. Copy-on-Write and fork()
One of the most important uses of COW is:
fork()
Modern UNIX/Linux systems use COW extensively.
Reason:
fork()
↓
Child Often Executes exec()
↓
Memory Never Modified
↓
No Copy Needed
This makes process creation highly efficient.
24. Copy-on-Write vs Normal Copying
| Feature | Normal Copy | Copy-on-Write |
|---|---|---|
| Memory Copied | Immediately | On First Write |
| Process Creation | Slow | Fast |
| Memory Usage | High | Low |
| Overhead | High Initially | Deferred |
| Efficiency | Lower | Higher |
25. Real-World Analogy
Imagine two people sharing a document.
Initially:
One Document
↓
Shared by Both
As long as they only read:
No Copy Needed
When one person edits:
Create Personal Copy
↓
Modify Copy
The original remains unchanged.
This is exactly how Copy-on-Write works.
26. Relationship with Page Faults
Normal Demand Paging:
Page Absent
↓
Page Fault
Copy-on-Write:
Page Present
↓
Write Attempt
↓
Page Fault
Thus page faults are used for two purposes:
Loading missing pages
Detecting writes to shared pages
Copy-on-Write delays memory duplication until a process actually modifies a shared page, making process creation faster and significantly reducing memory consumption.