Prepare private rather than shared memory
Run a standalone, single-threaded Python program on Linux. It creates a private anonymous mapping and writes one byte in each of 256 pages before fork. The child inherits the mapping contents, but writes through this private mapping are not intended to update the parent’s data. A shared mapping would test a different contract. Avoid embedding the example inside an already multithreaded application, where inherited lock state and restrictions after fork introduce unrelated correctness concerns.
Coordinate the two observations
The child changes the first byte in every mapped page to two and sends its own observed value through a pipe. The parent waits for the child’s exit and checks that its mapping still contains one. Pipe communication and waitpid establish the observation order without depending on a guessed sleep duration. The exit status is checked so that a failed child is not mistaken for a successful memory experiment. Only a small message passes through the pipe.
import mmap, os, resource
page = os.sysconf("SC_PAGE_SIZE")
m = mmap.mmap(-1, page * 256,
flags=mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,
prot=mmap.PROT_READ | mmap.PROT_WRITE)
for i in range(0, len(m), page): m[i] = 1
read_fd, write_fd = os.pipe()
pid = os.fork()
if pid == 0:
os.close(read_fd)
before = resource.getrusage(resource.RUSAGE_SELF).ru_minflt
for i in range(0, len(m), page): m[i] = 2
delta = resource.getrusage(resource.RUSAGE_SELF).ru_minflt - before
os.write(write_fd, f"child={m[0]}, minor_delta={delta}".encode())
os.close(write_fd)
os._exit(0)
os.close(write_fd)
print(os.read(read_fd, 4096).decode())
os.close(read_fd)
_, status = os.waitpid(pid, 0)
assert os.waitstatus_to_exitcode(status) == 0
print("parent=", m[0])
assert m[0] == 1
m.close()State what the evidence establishes
Seeing child value two and parent value one demonstrates independent visible contents after the writes. An increase in child minor faults is consistent with page handling during copy-on-write, but the counter alone is not a precise measurement of physically shared pages. The interpreter modifies other memory too. Combine the observed isolation with Linux’s documented fork behavior rather than claiming that this small program directly inspects every physical page or proves an exact sharing ratio.
Relate the example to real process lifetimes
Low memory cost immediately after fork does not imply low cost after a child mutates large amounts of state. Long-lived children and runtime bookkeeping can reduce sharing, while large read-only regions may retain more benefit. Additional observations such as proportional set size and private dirty memory can help, but page state changes during measurement must be considered. Reap the child and close both pipe ends and the mapping. Do not extrapolate a small first-byte test into a production memory-capacity estimate.
If the application wants the child’s changed data to reach the parent, it needs explicit IPC or a deliberately shared mapping. The same variable name after fork is not a shared mutable object contract. In this example, the text reporting the child’s value crosses the pipe as communication. That message should not be confused with the child’s private memory modification becoming visible in the parent.