logical, if TRUE then the new process has
no ties to the parent process, will not show in the list of
children and will not be killed on exit.
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". If estranged is
set to TRUE then the child process will be of the class
"estrangedProcess" and cannot communicate with the master
process nor will it show up on the list of children. 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 OS X, but users of
third-party front-ends should consult their documentation.
This can also apply to other connections (e.g., to an X server) created
before forking, and to files opened by e.g. graphics devices.
Note that tcltk counts as a GUI for these purposes since
Tcl runs an event loop. That event loop is inhibited in a
child process but there could still be problems with Tk graphical
connections.
## This will work when run as an example, but not when pasted in.
p <- parallel:::mcfork()
if (inherits(p, "masterProcess")) {
cat("I'm a child! ", Sys.getpid(), "\n")
parallel:::mcexit(,"I was a child")
}
cat("I'm the master\n")
unserialize(parallel:::readChildren(1.5))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(parallel)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/parallel/mcfork.Rd_%03d_medium.png", width=480, height=480)
> ### Name: mcfork
> ### Title: Fork a Copy of the Current R Process
> ### Aliases: mcfork mcexit
> ### Keywords: interface
>
> ### ** Examples
> ## No test:
> ## This will work when run as an example, but not when pasted in.
> p <- parallel:::mcfork()
> if (inherits(p, "masterProcess")) {
+ cat("I'm a child! ", Sys.getpid(), "\n")
> if (inherits(p, "masterProcess")) {
+ parallel:::mcexit(,"I was a child")
+ }
+ cat("I'm a child! ", Sys.getpid(), "\n")
+ parallel:::mcexit(,"I was a child")
> cat("I'm the master\n")
+ }
I'm the master
> unserialize(parallel:::readChildren(1.5))
I'm a child! 1531
[1] "I was a child"
> ## End(No test)
>
>
>
>
> dev.off()
null device
1
>