0
0
Operating Systemsknowledge~6 mins

Copy-on-write technique in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to share a document with a friend without making a full copy right away. Copy-on-write solves the problem of saving memory and time by delaying copying until absolutely necessary.
Explanation
Initial Sharing
When two processes need the same data, the system lets them share one copy instead of making duplicates. Both processes point to the same memory area, saving space and speeding up setup.
Copy-on-write starts by sharing one copy to save resources.
Write Detection
The system watches for when a process tries to change the shared data. It uses special markers to know if the data is about to be modified.
The system detects when a write is about to happen on shared data.
Copying on Write
Only when a process tries to change the data does the system make a separate copy for that process. This way, the original shared data stays unchanged for others.
Actual copying happens only when data is modified.
Memory Efficiency
By delaying copying, the system uses less memory and reduces unnecessary work. This is especially helpful when many processes start with the same data but change little or nothing.
Copy-on-write improves memory use by avoiding needless copies.
Real World Analogy

Imagine two friends sharing a single printed book. They both read the same pages without making copies. Only if one friend wants to write notes in the book does she make her own copy to write on, leaving the original book clean for the other.

Initial Sharing → Both friends reading the same original book without copies
Write Detection → Noticing when a friend wants to write notes in the book
Copying on Write → Making a personal copy of the book only when writing notes
Memory Efficiency → Saving paper and effort by sharing the book until writing is needed
Diagram
Diagram
┌───────────────┐       ┌───────────────┐
│ Shared Memory │◄──────│ Process A     │
└───────────────┘       └───────────────┘
        ▲                      ▲
        │                      │
        │                      │
┌───────────────┐       ┌───────────────┐
│ Shared Memory │◄──────│ Process B     │
└───────────────┘       └───────────────┘

When Process A writes:

Process A copies shared memory → gets own copy
Process B still uses original shared memory
Diagram showing two processes sharing memory initially and one making a copy only when writing.
Key Facts
Copy-on-writeA technique where copying of data is deferred until it is modified.
Shared MemoryMemory area accessed by multiple processes before any write occurs.
Write DetectionMechanism to detect when a process attempts to modify shared data.
Memory EfficiencyReduced memory use by avoiding unnecessary data duplication.
Common Confusions
Copy-on-write means data is copied immediately when shared.
Copy-on-write means data is copied immediately when shared. Copy-on-write delays copying until a write happens, so data is shared initially without duplication.
All processes get separate copies from the start.
All processes get separate copies from the start. Processes share one copy until one modifies it, then only that process gets a new copy.
Summary
Copy-on-write saves memory by sharing data until a change is needed.
Data copying happens only when a process writes to the shared data.
This technique improves efficiency in systems where many processes start with the same data.