So what should you use?

Obviously, if you're porting existing code, you'll want to use whatever it uses. For new code, you should avoid fork() if at all possible.

Here's why:

  • Although fork() works with multiple threads, you need to register a pthread_atfork() handler and lock every single mutex before you fork, complicating the design.
  • The child of fork() duplicates all open file descriptors. As we'll see in the Resource Manager chapter later, this causes a lot of work—most of which will be unnecessary if the child then immediately does an exec() and closes all the file descriptors anyway.

Your best choice for portability is posix_spawn().

Page updated: