Operating systems course, 2025
xv6 Shared Memory
Shared memory between processes in xv6, built at the level of page tables, and a logger on top of it where eight processes write into one 4 KB buffer at the same time without a single lock.

- Two new system calls that map one process's physical pages into another's address space, with a spare page-table bit marking which process owns each page.
- A lock-free log: writers claim space with one atomic compare-and-swap, and a failed swap already tells them how far to skip.
- Hardened against untrusted input: bad pids and addresses, half-finished mappings, fork after sharing and holes in the address space, each case tested in QEMU.
What it is
In xv6, as in Unix, every process has its own private address space. A pointer in one process means nothing in another, and that isolation is the point. This project adds a deliberate way through it: one process can make part of its memory visible to another. On top of that, eight processes log into a single shared buffer at the same time, with no locks.
It was the third and last assignment of the Operating Systems course at Ben-Gurion University in 2025, done with a partner. The first two are in the xv6 Kernel case study.
Sharing a page
Every memory access a process makes goes through its page table. RISC-V walks three levels of it to turn a virtual address into a physical page. Two processes share memory when their page tables point at the same physical page, even if each sees it at a different address.
map_shared_pages(src_pid, dst_pid, addr, size) does exactly that. It walks the source’s page table to find the physical page behind each virtual page in the range and installs the same pages in the destination, just above the top of its heap. The permission bits come along unchanged. The call returns the address in the destination that corresponds to addr, with the same offset into the page, because the source address doesn’t have to start on a page boundary.

Who frees the page. xv6 assumes that every page in a page table belongs to that process, so when a process exits, it frees all of them. With sharing, the same page would be freed twice: once by its owner and once by the borrower, possibly after it had already been handed to someone else. Bit 8 of a RISC-V page-table entry is ignored by the hardware, so it became PTE_S, “shared”. The borrower’s entries carry it, and the kernel’s unmap routine always removes the mapping but frees the page only when the bit is clear.
Keeping the size honest. The kernel tracks each process’s size and uses it to know what to free on exit and what to copy on fork. Mapping grows it by whole pages and unmapping shrinks it back. The test program prints the child’s size at every step: one page more after mapping, back to exactly where it started after unmapping, and a normal malloc afterwards still lands in the right place.
Logging without locks
The log is a single 4 KB buffer that the parent maps into eight children. It’s a run of records, each a 32-bit header followed by the message. The header packs the writer’s index and the message length, and a header of zero means free space.
A child claims a record with one atomic compare-and-swap on its header: “if this is still zero, make it mine”. Only one writer can win a given header. A loser doesn’t need to look again, because the value the swap returns is the winner’s header, and it holds the winner’s length. The loser skips exactly that far and tries the next header. Records are claimed strictly in order from the start, so they can never overlap, whatever their lengths, and the first zero header marks the end of the log. Every header sits on a 4-byte boundary, which the atomic instruction requires.
A child stops when its next message no longer fits. The parent waits for all eight, then walks the records and prints each message with the index of the child that wrote it. A typical run fits 114 messages of different lengths into 4,072 of the 4,096 bytes, split almost evenly: every child gets 14 or 15.
Rules for the reader. A few details keep the protocol honest:
- The reader never clears a header it has read. A zeroed header looks free, and a child with a longer message could claim it and overwrite the start of the next record. Messages deliberately vary in length so that this kind of overlap would show up.
- The parent reads only after every child has exited. A child claims its header before it copies in the text, so a reader running alongside could see half a message. The parent also owns the buffer, so it has to outlive every process borrowing it.
- Each child receives its buffer address from the parent through a pipe. The parent passes on the exact address the mapping call returned, so no child has to work it out from timing.
Making it robust
A system call accepts whatever a user program passes it, and a kernel that trusts its arguments can be crashed by any process. Beyond the basic mapping, most of the work went into the places where sharing meets the rest of the kernel:
- Untrusted arguments. Both pids are checked, and only live processes are accepted, never one that’s still being created or has already exited. Addresses past the top of the address space and sizes that overflow are rejected before the page-table walk, which would otherwise panic.
- All or nothing. If mapping fails partway through, the call removes the pages it has already mapped before it returns an error. Otherwise entries would be left above the process’s recorded size, and the kernel would trip over them when the process exits.
forkafter sharing.forkcopies page-table flags as they are. A child’s private copy of a borrowed page must not inheritPTE_S, or nothing would ever free it, so the copy clears the bit.- Holes in the address space. Unmapping a shared region that isn’t the topmost one leaves an unmapped page below the process’s size.
exitandforknormally expect every page below the size to exist, so they skip such holes.
Testing
Everything runs on the RISC-V kernel inside QEMU. The shared-memory test runs several times in a row on the same boot, which proves no page is lost or freed twice, and once more with a flag that makes the child exit without unmapping. Its parent can still read the page afterwards. A separate run throws the bad cases at the kernel: unknown pids, addresses past the end of the address space, a mapping that fails halfway, a fork after mapping, and unmapping the lower of two regions. Each one gets a clean error or just works, and the kernel keeps running. xv6’s own usertests suite still passes in full.