We are hiring ! See our job offers.
Raw File
Tip revision: 3ea66bd3ecb5f9467b3db36480ee97c06fc001e4 authored by Simon Urbanek on 08 August 1977, 00:00:00 UTC
version 0.1-7
Tip revision: 3ea66bd
  Fork a copy of the current R process
  \code{fork} creates a new child process as a copy of the current R process
  \code{exit} closes the current child process, informing the master process as necessary
  This is a very low-level API for expert use only. If you are
  interested in user-level parallel execution use
  \code{\link{mclapply}}, \code{\link{parallel}} and friends instead.
exit(exit.code = 0L, send = NULL)
\item{exit.code}{process exit code. Currently it is not used by
multicore, but other applciations might. By convention 0 signifies
clean exit, 1 an error.}
\item{send}{if not \code{NULL} send this data before exiting
(equivalent to using \code{\link{sendMaster}})}
  \code{fork} returns an object of the class \code{childProcess} (to
  the master) and \code{masterProcess} (to the child).

  \code{exit} never returns
  The \code{fork} function provides an interface to the \code{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 \code{\link{sendMaster}}) and child's stdin is
  re-mapped to another pipe held by the master process (see

  If you are not familiar with the \code{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 \code{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
  \code{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 \emph{strongly discouraged} to use \code{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
  Windows opearting system lacks the \code{fork} system call so it
  cannot be used with multicore.
  \code{\link{parallel}}, \code{\link{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")
\author{Simon Urbanek}
back to top