r/cpp • u/SatadeepDasgupta • Jul 03 '26
Windows doesn't have fork(), so I faked copy-on-write with VirtualProtect + exception handling to snapshot an in-memory KV store
Was messing around trying to figure out how Redis does BGSAVE without blocking, and it comes down to fork() + COW on Linux — the OS shares pages between parent/child and only copies a page when someone writes to it. Windows has no fork(), so there's no free lunch here.
Ended up building a small toy project (Veyr) to see if I could fake the same behavior in user space on Windows. Wrote it up here if anyone's interested: https://satadeepdasgupta.medium.com/the-windows-machine-doesnt-have-fork-so-we-built-one-b10e1357aa4f
Short version of how it works:
• put the whole dataset in one big VirtualAlloc'd arena
• when snapshotting starts, VirtualProtect the arena to PAGE_READONLY
• any write thread that touches it now takes a hardware access violation
• catch that in a Vectored Exception Handler, copy the 4KB page to a shadow buffer before it changes, flip that page back to RW, resume execution
• background thread walks the arena and writes shadow pages for anything that got touched, live pages for anything that didn't
The first version I tried leaked memory into deadlocks because my handler was calling malloc for the shadow page allocation, and if the frozen thread happened to be holding the heap lock when it faulted, it just... never came back. Fixed by only calling VirtualAlloc directly inside the handler, never touching the normal allocator. Kind of an obvious fix in hindsight but took me a minute to figure out why it was randomly hanging.
Also built a lock-free hash map for it (atomic pointer swap per slot, immutable entries so there's no torn reads) since the snapshotting trick doesn't matter much if the map itself is the bottleneck.
Ran redis-benchmark against it and against Memurai (Redis-compatible Windows server) on the same box, same command. Veyr came out ahead by a decent margin but it's only doing GET/SET, no persistence format, no ACLs, none of the actual feature surface Memurai has, so take that as "minimal special-purpose thing beats general-purpose thing" rather than any real claim about which engine is better. Numbers are in the post if curious, didn't want to just paste a screenshot and let it become the whole conversation.
Known gap I haven't fixed: the map doesn't free old entries on overwrite, it just orphans them in a bump arena. Works fine for a benchmark, would leak forever under real sustained traffic. Need to add epoch-based reclamation or hazard pointers or something before this is anything other than a toy.
Source's up if anyone wants to poke holes in it: github.com/satadeep3927/veyr
Happy to be told I reinvented something that already exists, wouldn't be the first time.
4
u/jedwardsol const & Jul 03 '26
I haven't looked at exactly what you're doing; but Windows has a way to create a snap shot of virtual memory : https://learn.microsoft.com/en-us/previous-versions/windows/desktop/proc_snap/process-snapshotting-portal
So you could create a child which snapshots the parent and then saves the db. The big disadvantage compared to your approach is that you don't have code running in the snapshot - you have to read the memory out
4
u/Tringi github.com/tringi Jul 04 '26
Many many years back I read an article where someone managed to implement
fork()through NT API, but A LOT of things broke.Native stuff worked well, data got CoW shared, .text/.rdata sections are already, kernel handles duplicated properly, they got even Console I/O working IIRC. But GUI breaks completely, and so does anything that holds any sockets. Way too many things are expected to be unique by default in each process.
26
u/drbomb Jul 03 '26
I'm highly suspect of these kinds of posts because they just read of "I used AI to write this highly specific and potentially sought after feature for this language". While if this was something this important i would've expected it to be integrated into standard libraries or compilers.
2
u/RogerV Jul 05 '26
The lack of a fork() equivalent on Windows has basically meant that some popular Unix tools end up being more of a pain to port over. One would prefer that a Posix compatibility layer for Windows could do the trick for the most part, but then there ends up being the pesky fork() call...
But frankly, my answer has been to just abandon Windows altogether as a place to do programming. Am not going to write a GUI app that I expect to run on Windows, and would not in a million years consider writing any server-side service that targets Windows. So it's not much of a sacrifice to merely ignore this OS.
1
u/Chaosvex Jul 04 '26
Microsoft used to maintain a Redis fork for Windows. Perhaps it'd be worth your time digging into the techniques they used*.
*possibly.
1
u/Responsible-Bar7165 Jul 04 '26
How is this different from what Cygwin does?
1
u/Loren_SL Sep 13 '26
VirtualAlloc/VirtualProtect are for private per-process memory only, no cross-process sharing possible. You want a section object opened with FILE_MAP_COPY:
B's view starts out backed by the same physical pages as A's. First write from B triggers a real page-fault-driven COW in the memory manager — private page-file-backed copy for B, invisible to A, no VEH or manual shadow-copy bookkeeping required. It's the same mechanism Windows uses for SEC_IMAGE sections (writable DLL globals shared read-only across processes until one writes).