THINK FIRST·CODE LATER

← All labs

Copy-on-write fork simulator

Problem

Simulate copy-on-write. Input: the number of pages of process P (frames 0 … n−1, one per page), then operations:

  • fork A B — process B becomes a child of A: B's page table is a copy of A's (frames are shared, their reference counts increase). Print fork A -> B: n pages shared read-only, 0 copied.
  • X read k — print X read k: frame f.
  • X write k — if X's frame for page k is shared (reference count > 1), copy it to a new frame (the next unused frame number), decrement the old frame's count: X write k: COW fault, copy frame f -> frame g; otherwise X write k: frame f (private, no copy).

At the end print COW faults: c, pages copied: c, frames in use: u (frames with a count > 0) and Eager fork would have copied: e pages (t frames), where each fork would copy all n pages and t = number of processes × n.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.