Bird
Raised Fist0
Operating Systemsknowledge~6 mins

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

Choose your learning style10 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-write → A technique where copying of data is deferred until it is modified.
Shared Memory → Memory area accessed by multiple processes before any write occurs.
Write Detection → Mechanism to detect when a process attempts to modify shared data.
Memory Efficiency → Reduced 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.

Practice

(1/5)
1. What is the main idea behind the copy-on-write technique in operating systems?
easy
A. Data is copied only when it is modified, not when initially shared
B. Data is copied immediately when a process is created
C. Data is never copied, only referenced
D. Data is copied multiple times to improve speed

Solution

  1. Step 1: Understand the purpose of copy-on-write

    Copy-on-write delays copying data until a modification is made, saving resources.
  2. 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.
  3. Final Answer:

    Data is copied only when it is modified, not when initially shared -> Option A
  4. Quick Check:

    Copy-on-write = Copy on modification [OK]
Hint: Copy happens only when data changes, not before [OK]
Common Mistakes:
  • Thinking data copies immediately on sharing
  • Believing data is never copied
  • Assuming multiple copies improve speed
2. Which of the following best describes when a copy is made in a copy-on-write system?
easy
A. When the original data is read
B. When the data is modified
C. When the system boots
D. When the data is deleted

Solution

  1. 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.
  2. Step 2: Match the correct event

    Only When the data is modified correctly states copying occurs on modification.
  3. Final Answer:

    When the data is modified -> Option B
  4. Quick Check:

    Copy-on-write triggers on modification [OK]
Hint: Copy only on write, not on read [OK]
Common Mistakes:
  • Confusing read with write triggers
  • Thinking copy happens at system start
  • Assuming copy on deletion
3. Consider a process creation using copy-on-write. If the parent process has 100MB of memory, and the child process is created but does not modify any data, how much memory is used by both processes together?
medium
A. 0MB
B. 200MB
C. 50MB
D. 100MB

Solution

  1. Step 1: Understand memory sharing in copy-on-write

    Initially, both processes share the same memory pages without copying.
  2. Step 2: Calculate total memory usage

    Since no modifications occur, memory is shared, so total used is 100MB, not doubled.
  3. Final Answer:

    100MB -> Option D
  4. Quick Check:

    Shared memory means total = original size [OK]
Hint: No changes mean no extra memory used [OK]
Common Mistakes:
  • Assuming memory doubles immediately
  • Thinking child process uses zero memory
  • Confusing copy-on-write with full copy
4. A system uses copy-on-write for file snapshots. A snapshot is taken, but later the original file is modified. What is the likely error if the snapshot also changes unexpectedly?
medium
A. Copy-on-write was not properly implemented, causing shared data to change
B. Snapshot was created after modification
C. The file system does not support snapshots
D. The snapshot was deleted accidentally

Solution

  1. Step 1: Analyze snapshot behavior with copy-on-write

    Snapshots should remain unchanged because copy-on-write protects original data by copying on modification.
  2. Step 2: Identify cause of snapshot changing

    If snapshot changes, it means copy-on-write failed and data was shared incorrectly.
  3. Final Answer:

    Copy-on-write was not properly implemented, causing shared data to change -> Option A
  4. Quick Check:

    Snapshot changes mean copy-on-write failure [OK]
Hint: Snapshot changes mean copy-on-write error [OK]
Common Mistakes:
  • Assuming snapshot created after modification
  • Blaming unsupported file system without evidence
  • Confusing deletion with modification
5. In a system using copy-on-write, a parent process forks a child. The child modifies a small 1MB part of a 500MB memory space. How much additional memory is used after the modification?
hard
A. 500MB
B. 501MB
C. 1MB
D. 0MB

Solution

  1. Step 1: Understand copy-on-write memory allocation on modification

    Only the modified part is copied, so only 1MB is newly allocated.
  2. Step 2: Calculate total additional memory used

    The rest of the 499MB remains shared, so additional memory is exactly 1MB.
  3. Final Answer:

    1MB -> Option C
  4. Quick Check:

    Copy-on-write copies only changed data [OK]
Hint: Only changed data uses extra memory [OK]
Common Mistakes:
  • Assuming entire memory is copied
  • Adding modified size to total incorrectly
  • Thinking no memory is used on modification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard