1.7 Fork, Spawn, and Platform Differences

WORKLOADSSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18

1.7.1 The Unix-Windows split

Unix systems offer fork(), and Windows does not. The difference runs through parallel R design. Fork creates a child with the parent’s memory state in milliseconds. Spawn starts from scratch and takes from a fraction of a second to many seconds, depending on what the new process must load. The two operations inherit differently and break different assumptions.

1.7.2 The fundamental difference

Fork duplicates the calling process. The child process begins with the same memory contents as the parent. The child is a complete copy at the fork point. It inherited the R interpreter state, all loaded packages, every variable in every environment, and the complete runtime state. The child continues from the same instruction.

Spawn starts a new process by running an executable file. On Windows, CreateProcess starts a fresh executable. On Unix, execve() starts a fresh program. Either way, the new process begins with an empty R heap. It must load R itself, run the startup files, load packages, and rebuild any state the parent had. It does inherit the environment variables and the working directory, but no R object reaches it unless the parent sends it.

The fork child costs milliseconds. The spawned process costs seconds once it has large packages to load.

# Unix only: forks four children that start with the parent's R state.
# On Windows this stops with "fork clusters are not supported on Windows".
cl <- parallel::makeForkCluster(4)

# Any platform: starts four fresh Rscript processes. On my Windows desktop
# this took about 0.15 s with base R; packages each worker loads come on top.
cl2 <- parallel::makePSOCKcluster(4)

1.7.3 Inheritance across fork

When a Unix process forks, the child inherits:

  1. Memory contents. Every byte from the parent’s heap. Every variable, data frame, matrix, and function object. The child starts with identical data structures.

  2. Open file descriptors. Any file handles the parent opened, the child can read or write. Sockets, pipes, and connections are inherited.

  3. Environment variables. The child inherits the parent’s PATH, HOME, and any custom variables.

  4. Signal handlers. The child inherits the parent’s response to signals like SIGINT.

  5. Stack state. Local variables in the parent’s call stack are copied to the child. The child resumes from the same instruction the parent called fork().

The child does NOT inherit:

  1. Process ID. The child gets a new PID.

  2. Pending signals and timers. The child starts with no pending signals and no timers. It does stay in the parent’s process group, so pressing Ctrl+C in a terminal sends SIGINT to the forked children as well. A signal sent to the parent’s process ID alone reaches only the parent.

  3. File locks. Advisory record locks set with fcntl do not carry across. Mutexes do, because they sit in memory the child copies, so a mutex that was locked at the moment of the fork is still locked in the child.

1.7.4 The starting state after spawn

A spawned process starts with an empty R session. On Windows and Unix alike:

  1. Fresh R interpreter. A new R process initializes. Global variables are empty. Only R’s default packages are attached; the user’s packages are not loaded.

  2. Fresh heap. Allocated memory belongs only to this process.

  3. No inherited data. The child must receive all input data via serialization or message passing.

  4. Few open files. Standard input, output and error are open, plus any handles the launcher chose to pass on. Connections the parent opened in R are not.

The parent must explicitly send the spawned worker any data it needs. This happens through serialization: convert R objects to bytes, send bytes over a socket or pipe, deserialize on the receiving side.

fork (Unix-like) parent R process the child starts with loaded packages global objects open connections threads of parent (absent) child inherits, pages shared until written spawn (all platforms) parent R process the child starts with loaded packages (absent) global objects (absent) open connections (absent) threads of parent (absent) child starts empty, everything reloaded

A forked child inherits the parent’s state. A spawned child starts empty and reloads everything.

1.7.5 Windows and fork

Windows process creation uses a different model than Unix. The CreateProcess API creates a new address space, loads an executable, and starts the process from that executable’s entry point. The Win32 API has no call that duplicates a running process the way fork() does.

Cygwin emulates fork on top of the Windows API by starting a new process and copying the parent’s memory into it, which is far slower than a real fork. WSL2 is different: it runs a real Linux kernel in a virtual machine, so R inside WSL2 is Linux R and forks natively.

1.7.6 The cost of copy-on-write

Modern Unix systems do not copy memory on fork. The kernel marks all pages read-only in both parent and child. When either process writes to a page, the CPU raises a page fault. The kernel handles it by:

  1. Allocating a new physical page.
  2. Copying the faulted page to the new page.
  3. Remapping the writer to the new page.
  4. Resuming the write.

Pages that no one writes stay shared. A forked R worker that only reads a large data frame shares its pages with the parent. The kernel never copied those pages.

The gain is smaller than it appears. The child’s first full garbage collection walks the reachable objects and writes a mark bit into each object’s header. Those writes trigger page faults on shared pages, and copy-on-write privatizes most of them. In sunlight’s Day 7 benchmark, the summed PSS of the forked workers was only 1.28 times lower than the summed RSS of the best spawned-worker setup at 8 workers, and 1.10 times lower at 32, far from the several-fold reduction copy-on-write suggests. Setting PSS against RSS favors sunlight, the run was on WSL2, not bare-metal Linux, and crew was left out of the comparison because it did not start all its workers. See Fork, Copy-on-Write, and the First GC.

Pages do stay shared if no one writes to them. The machine code of R and of loaded packages stays shared across many forked workers, and so does the data of a large numeric vector, since the collector writes only the vector’s header. The many small objects a package creates when it loads are another matter, and their pages are privatized quickly.

1.7.7 Assumptions that break

Connections. A forked child inherits the parent’s open connections (database links, file handles, sockets). The child gets its own copy of the R connection object, but both copies refer to the same file descriptor, so parent and child talk over one socket to one database session. Their messages interleave and break the session’s protocol. The child must open its own connections.

Graphics devices and GUIs. The child inherits the parent’s graphics device state, but the device itself (an X11 or Quartz window) does not know about the child. R’s documentation says child processes should never use on-screen graphics devices and strongly discourages forking from a GUI or embedded R at all, because several processes then share one GUI. RStudio counts: future’s multicore plan refuses to fork there and runs sequentially.

Threads created by packages. If the parent loaded a package that started native threads (a multithreaded BLAS, a thread pool in Rcpp code, or an OpenMP team), none of those threads exist in the child. Only the thread that called fork() survives. What the child does inherit is the memory those threads were working in, including any mutex one of them held at the moment of the fork. The lock is still set and the thread that would have released it is gone, so the child’s first call into that library can block forever. R’s documentation strongly discourages mclapply() in any R process that runs threads from a third-party library. macOS goes further: its fork(2) manual says every framework and library API should be assumed unsafe in a forked child unless it is documented as safe.

External resources. Files and directories the parent created, including its tempdir(), are the same files for the child. Semaphores in shared memory keep whatever count they had. The child can collide with the parent over the same file names.

Random number state. The child starts with a copy of the parent’s RNG state, so without reseeding both would draw the same numbers. mclapply() and mcparallel() reseed each child by default (mc.set.seed = TRUE), and with RNGkind("L'Ecuyer-CMRG") each child gets the next stream.

1.7.8 Portable parallel design

To stay portable across platforms:

  1. Do not assume fork. Some users run on Windows. The code must work with spawn.

  2. Do not rely on inherited state. Treat workers as fresh processes. Send them what they need.

  3. Reset connections and devices in workers. Close and reopen after the worker starts.

  4. Handle the RNG explicitly. Each worker must set up its own stream or receive a stream index.

A parallel design that works on Unix fork can fail on Windows spawn because it relied on inherited state. The reverse is rare: code written for spawned workers already sends everything it needs, so it usually runs under fork too.

1.7.9 Platform differences in practice

The parallel package provides makeForkCluster() for Unix (fork) and makePSOCKcluster() for Windows and Unix (spawn). On Windows, makeForkCluster() stops with “fork clusters are not supported on Windows”, and mclapply() with mc.cores > 1 stops with “‘mc.cores’ > 1 is not supported on Windows”. Code written for them has to be rewritten for makePSOCKcluster() there.

The future package abstracts over this with strategy selection. future::plan(multicore) forks on Unix and falls back to sequential processing on Windows and inside RStudio. future::plan(multisession) spawns on any OS. Code written with multisession works everywhere but pays the spawn cost everywhere.

sunlight uses fork on Unix and provides no Windows port.

Further reading

  • fork(2) <https://man7.org/linux/man-pages/man2/fork.2.html>, the Linux manual page, including what the child does not inherit.
  • macOS fork(2) <https://manp.gs/mac/2/fork>, whose CAVEATS section covers framework safety after a fork.
  • Microsoft, Creating Processes <https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes>, how a Windows program starts another with CreateProcess().
  • Cygwin User’s Guide, Highlights of Cygwin Functionality <https://cygwin.com/cygwin-ug-net/highlights.html>, with its account of how Cygwin emulates fork.
  • R Core Team, Package ‘parallel’ <https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf>, the package vignette.
  • Processes vs Threads. Copy-on-write mechanics.

Where this appears

  • sunlight sunlight forks its workers from a template process to inherit its state cheaply, so it runs only on POSIX systems.

Where to go next

Back to top