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.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os10-4-data-1.txt

4
fork P C
C write 1
C read 0
P write 1
P write 2
C write 3
P read 1
fork C G
G write 3
What your program must print for os10-4-data-1.txt
fork P -> C: 4 pages shared read-only, 0 copied
C write 1: COW fault, copy frame 1 -> frame 4
C read 0: frame 0
P write 1: frame 1 (private, no copy)
P write 2: COW fault, copy frame 2 -> frame 5
C write 3: COW fault, copy frame 3 -> frame 6
P read 1: frame 1
fork C -> G: 4 pages shared read-only, 0 copied
G write 3: COW fault, copy frame 6 -> frame 7
COW faults: 4, pages copied: 4, frames in use: 8
Eager fork would have copied: 8 pages (12 frames)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os10-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content os10-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.