r/cpp • • 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

0 Upvotes

33 comments sorted by

View all comments

Show parent comment

2

u/RogerV Jul 07 '26

there is ton of truth on that one - in the child process right after fork there are a vast number of things that a program might do before doing an ex3c of the.target executable. might prepare a pseudo terminal PTY master-slave and dup2 all the stdio descriptors onto the slave descriptor. Or might apply a CPU mask to where the child process will be allowed to execute on some subset of CPU cores. Or set a network namespace context and create a veth pair network interface where the child process will be executing in a network sandbox and only permitted the Beth interface for its networking connectivity. And there is the choice of whether to execute as the real or effective UID/GID.

I could go on and on listing the kinds of techniques that are commonly used in launching child processes via fork()

Far from being a relic of a bygone era, fork() is still one of the crucial API of Linux/Unixiy programming. It’s bread and butter for architecting multi-tenancy sandboxing applications. But clone(), which fork is built on per Linux, is likewise crucial as its even more versatile for dialing in what is actually inherited by the child - I.e., how heavy weight it is.

3

u/wyrn Jul 07 '26

These techniques were developed on top of fork because fork was what was available. But it doesn't make fork a good idea. In particular, designing a core OS api just to simplify terminals/shells is completely backwards. It made sense in the 70s if you're hacking an OS together with spit and duct tape. It doesn't make sense anymore.

2

u/RogerV Jul 08 '26

And yet of course nobody is building anything to run on back-end servers other than flavors of Unix and Unix-like OS (e.g., Linux)

Which means fork() is being routinely used because instead of fiddly API flags that are extremely limited in capability, fork() is the ultimate in flexibility to make the child process be dialed into do exactly what is wanted. One actually gets to write arbitrary code that is part of ones own program to configure and tailor the child process - or even be the child process (naturally don't have to exec another separate program).

Look at major software on Unixy platforms - database architectures (both PostgreSQL and Oracle) are based on a parent process orchestrating forked child processes, yet are all just one executable binary. Because fork() makes it trivial to have a program take on extremely different personalities to suit special situations - yet not have to bother and go write a different program to do that.

There is nothing more untrue under the sun than to say something such as fork() is obsolete. Building multi-tenancy software at a major telcom and fork() is the heart of hosting the networked sandboxed tenets - this is so prolific a pattern that its really ridiculous to even have fork() brought into question as to its current utility in contemporary programming.

And once again in the age of AI where am creating a multi-tenancy host where agents, and local AI inference, will be tenants in network sandboxes - but where the networking is very high performance (both based on DPDK and eBPF), and yet has exquisite control over traffic monitoring for security and policy enforcement - fork() is once again front and center in making that possible.

Not everything revolves around the granularity of threads or is best served by that. In my world, its more about pinned CPU cores and pinned hugepages memory (gigabytes of it - not mere megabytes). Threads are what gets used for control plane stuff where things aren't nearly as demanding.

4

u/wyrn Jul 08 '26

And yet of course nobody is building anything to run on back-end servers other than flavors of Unix and Unix-like OS (e.g., Linux)

Which of course proves absolutely nothing about whether fork is a good design or not (and it's a terrible design).

If the only bad consequence of fork were that it encourages overcommit, that by itself would be enough reason to deprecate it, because overcommit means the kernel can't keep track of memory, can't fulfill its own contracts, and makes it impossible to write correct software. But that's far from the only problem with fork: https://www.microsoft.com/en-us/research/wp-content/uploads/2019/04/fork-hotos19.pdf

But I developed all this software on top of fork

That's why it needs to be deprecated so this software can be replaced with an API that actually works.

https://en.wikipedia.org/wiki/Sunk_cost#Fallacy_effect