R: Fork a Copy of the Current R Process

mcfork {parallel} | R Documentation

## Fork a Copy of the Current R Process

### Description

These are low-level functions, not available on Windows, and not exported from the namespace.

`mcfork` creates a new child process as a copy of the current R process.

`mcexit` closes the current child process, informing the master process as necessary.

### Usage

    mcfork()

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

### Arguments

`exit.code` | process exit code. By convention `0L` signifies a clean exit, `1L` an error.
`send` | if not `NULL` send this data before exiting (equivalent to using `sendMaster`).

### Details

The `mcfork` 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 `sendChildStdin`).

If you are not familiar with the `fork` system call, do not use this function directly as it leads to very complex inter-process interactions amongst 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 `mcfork` 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.

`mcexit` is to be run in the child process. It sends `send` to the master (unless `NULL`) and then shuts down the child process. The child can also be shut down by sending it the signal `SIGUSR1`, as is done by the unexported function `parallel:::rmChild`.

### Value

`mcfork` returns an object of the class `"childProcess"` to the master and of class `"masterProcess"` to the child: both the classes inherit from class `"process"`. These are lists with components `pid` (the process id of the other process) and a vector `fd` of the two file descriptor numbers for ends in the current process of the inter-process pipes.

`mcexit` never returns.

### GUI/embedded environments

It is strongly discouraged to use `mcfork` and the higher-level functions which rely on it (e.g., `mcparallel`, `mclapply` and `pvec`) 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. Some precautions have been taken to make this usable in `R.app` on Mac OS X, but users of third-party front-ends should consult their documentation.

### Warning

This is a very low-level API for expert use only.

### Author(s)

Simon Urbanek and R Core.

Derived from the multicore package.

### See Also

`mcparallel`, `sendMaster`

### Examples

    ## This will work when run as an example, but not when pasted in.
    p <- mcfork()
    if (inherits(p, "masterProcess")) {
        cat("I'm a child! ", Sys.getpid(), "\n")
        mcexit(,"I was a child")
    }
    cat("I'm the master\n")
    unserialize(readChildren(1.5))

[Package parallel version 2.15.1 Index]