A fork() in the road
While fork() might be sub-optimal for launching different programs (fork() + exec() vs. posix_spawn()), it&#x27;s absolutely essential in several types of common systems that don&#x27;t use it to launch different programs.<p>Fork-requiring program class 1:<p>The biggest example where fork() is needed are webservers&#x2F;long-running programs with significant unchanging memory overhead and&#x2F;or startup time.<p>Many large applications written in a language or framework that prefers the single-process&#x2F;single-thread model for executing requests (e.g. Python&#x2F;gunicorn, Perl, a lot of Ruby, NodeJS with ‘cluster’ for multicore, etc.) are basically dependent on fork(). Such applications often have a huge amount of memory required at startup (due to loading libraries and initializing frameworks&#x2F;constant state). Creating workers that can execute requests in parallel but <i>don’t require any additional memory overhead</i> (just what they consume per request) is essential for them. fork()ing <i>without</i> exec()ing a new program facilitates this memory sharing; everything is copy-on-write, and most big webapps don’t need to write most of the startup-initialized memory they have, though they may need to read it.<p>Additionally, starting up such programs can take a long time due to costly initialization (seconds or minutes in the worst cases); using fork() allows them to quickly replace failed or aged-out subprocesses without having to pay that overhead (which also typically pegs a CPU core) to change their parallelism. “Quickly” might not be quick enough if a program needs to continually launch new subprocesses, but for periodically forking (or just forking-at-startup) long-running servers with a big footprint, it’s far better than re-initializing the whole runtime. For better or worse, we’ve come far enough from old-school process-per-request CGI that it is no longer feasible in most production deployments.<p>Anticipated rebuttals:<p>Q: Wouldn&#x27;t it be nice if everyone wrote apps small enough that startup time was minimized and memory footprint was low?<p>A: Sure, but they won’t.<p>Q: People should just write their big, long-running services in a framework that starts fast, has low memory requirements, and uses threads instead of fork()s.<p>A: See previous answer. Also see zzzcpan’s response.<p>Q: Can you access some of those benefits with careful use of shared memory?<p>A: Yes, but it’s <i>much</i> harder to do than it is to use fork() in most cases (caveat Windows, but it’s still hard).<p>Q: Do tools exist in single-proc&#x2F;single-thread forking frameworks&#x2F;languages which switch from forking to hybrid async&#x2F;threaded paradigms (like gevent) instead?<p>A: Yes, but they’re not  nearly as mature, capable, or useful (especially when you need to utilize multiple cores).<p>Fork-requiring program class 2:<p>Programs which fork infrequently in order to parallelize uncommon tasks over shared memory. Redis does this to great effect; it doesn’t exec(), it just forks off a child process which keeps the memory image at the time of fork from the parent, and writes most of that memory state to disk so that the parent can keep handling requests while the child snapshots.<p>Python’s multiprocessing excels at these kinds of cases as well. If you’re launching and destroying multiprocessing pools multiple times a second, then sure, you’re holding it wrong, but many people get huge wins from using multiprocessing to do parallel operations on big data sets that were present in memory at the time multiprocessing fork()ed off processes. While this isn’t cross-platform, it can be a really massive performance advantage: no need to serialize data and pass it to a multiprocessing child (this is what apply_async does under the covers) if the data is already accessible in memory when the child starts. Node&#x27;s &#x27;cluster&#x27; module will do this too, if you ask nicely. Many other languages and frameworks support similar patterns: the common thread is making fork()ing parallelism &quot;easy enough&quot; with the option of spending a little extra effort to make it <i>really really cheap</i> to get pre-fork memory state into children for processing. Oh, and you basically don&#x27;t have to worry about corrupting anyone else&#x27;s in-memory state if you do this (not so with threads).<p>Anticipated Rebuttals:<p>Q: $language provides a really accessible way to use true threads that isn’t nearly as tricky as e.g. multiprocessing or knowing all the gotchas (e.g. accidental file descriptor sharing between non-fork-safe libraries) of fork(); why not use that?<p>A: Many people still prefer languages with primarily-forking parallelism[1] constructs for reasons besides their fork-based concurrency capabilities--nobody’s claiming multiprocessing beats goroutines for API friendliness--so fork() remains useful in much more than a legacy capacity.<p>Q: Why not use $tool which does this via threads or why not bind $threaded_language to $scripting_language and use threads on the other side of the FFI boundary?<p>A: People won’t switch. They won’t switch because it’s hard (don&#x27;t tell me threaded Rust is as easy to pick up as multiprocessing--Rust has a lot of advantages in this space, but that ain&#x27;t one of them) and because there’s a positive benefit to staying within a given platform, even if some infrequent tasks (hopefully your Python doesn’t invoke multiprocessing <i>too</i> much) are a bit more cumbersome than usual. Also, “Friendly, easy-to-use concurrency with threads” is often a very false promise. There’s a reason Antirez is resistant to threading.<p>--------------<p>TL;DR perhaps using fork() <i>and exec()</i> for launching new programs needs to stop. But fork() itself is absolutely essential for common real-world use cases.<p>[1] References to parallelism via fork() above assume you have more than one core to schedule processes onto. Otherwise it’s not that parallel.<p>EDITs: grammar. There will be several because essay. I won&#x27;t change the substance.