Sub topic 12
Protection, Shared Pages and Copy-on-Write
Prerequisites
This chapter assumes that you understand virtual addresses, page tables, page-table entries (PTEs), frames, and page faults. A PTE is a page-table record that maps one virtual page to one physical frame.
Protection Bits
An operating system stores permission bits with each PTE. Common bits include:
- Valid: the mapping can be used.
- Read: the process may read the page.
- Write: the process may modify the page.
- Execute: the CPU may fetch instructions from the page.
- User or supervisor: user-mode code may access the page, or only the kernel may access it.
- Dirty: the page has been modified since it was loaded.
When a process accesses a virtual address, the memory-management unit checks these bits after translation. A read from a non-readable page or an instruction fetch from a non-executable page causes a protection fault. A write to a read-only page also causes a fault.
The kernel then decides whether the access is valid. It may terminate the process, report a segmentation fault, or handle the access as a copy-on-write event.
Sharing Physical Pages
Two processes can map different virtual addresses to the same physical frame. This allows them to share read-only program code, shared libraries, or explicitly shared data.
The physical frame is safe to share when every mapping forbids writes. Each process can read the same bytes, but neither process can change them through its protected mapping.
| Mapping type | Same physical frame? | Writes visible to other process? |
|---|---|---|
| Read-only shared code | Usually yes | Not allowed |
| Shared writable mapping | Yes | Yes |
| Private copy-on-write mapping | Initially yes | No, after copying |
The operating system must also account for the frame's reference count. The frame can be released only after all mappings to it have disappeared.
Copy-on-Write
Copy-on-write, or COW, delays copying until a write actually occurs. It is useful after fork, which creates a child process with a memory image initially similar to the parent.
A typical fork implementation works as follows:
- The kernel copies the parent's page tables for the child.
- Parent and child PTEs point to the same physical frames.
- The kernel clears the write bit in both PTEs.
- The kernel marks those mappings as COW internally.
- A read proceeds without copying.
- A write causes a protection fault.
- The kernel allocates a new frame, copies the old contents, changes the writer's PTE to writable, and retries the instruction.
If the original frame has only one remaining mapping, the kernel can often make that mapping writable without copying. Otherwise, it must preserve the other process's view by copying.
This small model shows the key idea:
class Page:
def __init__(self, data):
self.data = data
self.references = 1
def cow_write(page, index, value):
if page.references > 1:
page.references -= 1
page = Page(page.data.copy())
page.data[index] = value
return page
original = Page(["A", "B"])
child_page = original
original.references += 1
child_page = cow_write(child_page, 0, "X")
print(original.data) # ['A', 'B']
print(child_page.data) # ['X', 'B']Shared and Private Mappings
A shared writable mapping intentionally exposes changes. For example, two processes can use a shared memory region for communication. COW must not be applied to such a mapping because copying would hide writes that should be visible.
A private mapping may initially share frames, but each process receives a private copy when it writes. This distinction is important in memory-mapped files and after fork.
Exercise
After fork, parent and child share a COW page. The child writes to it, and the parent later writes to it. How many physical frames are needed after both writes?
Show answer
Two frames are needed. The first child write creates a private child frame. The parent can then write the original frame without another copy because it is no longer shared.