fork {multicore}R Documentation

Fork a copy of the current R process


fork creates a new child process as a copy of the current R process

exit closes the current child process, informing the master process as necessary


exit(exit.code = 0L, send = NULL)



process exit code. Currently it is not used by multicore, but other applciations might. By convention 0 signifies clean exit, 1 an error.


if not NULL send this data before exiting (equivalent to using sendMaster)


The fork function provides an interface to the fork system call. In addition it sets up a pipe between the master and child process that can be used to send data from the child process to the master (see sendMaster) and child's stdin is re-mapped to another pipe held by the master process (see link{sendChildStdin}).

If you are not familiar with the fork system call, do not use this function since it leads to very complex inter-process interactions among the R processes involved.

In a nutshell fork spawns a copy (child) of the current process, that can work in parallel to the master (parent) process. At the point of forking both processes share exactly the same state including the workspace, global options, loaded packages etc. Forking is relatively cheap in modern operating systems and no real copy of the used memory is created, instead both processes share the same memory and only modified parts are copied. This makes fork an ideal tool for parallel processing since there is no need to setup the parallel working environment, data and code is shared automatically from the start.

It is strongly discouraged to use fork in GUI or embedded environments, because it leads to several processes sharing the same GUI which will likely cause chaos (and possibly crashes). Child processes should never use on-screen graphics devices.


fork returns an object of the class childProcess (to the master) and masterProcess (to the child).

exit never returns


This is a very low-level API for expert use only. If you are interested in user-level parallel execution use mclapply, parallel and friends instead.


Windows opearting system lacks the fork system call so it cannot be used with multicore.


Simon Urbanek

See Also

parallel, sendMaster


  p <- fork()
  if (inherits(p, "masterProcess")) {
    cat("I'm a child! ", Sys.getpid(), "\n")
    exit(,"I was a child")
  cat("I'm the master\n")

[Package multicore version 0.1-8 Index]