spawn_process: Start a new child process.

Description Usage Arguments Format Details Value Termination

Description

In Linux, the usual combination of fork() and exec() is used to spawn a new child process. Standard streams are redirected over regular unnamed pipes.

In Windows a new process is spawned with CreateProcess() and streams are redirected over unnamed pipes obtained with CreatePipe(). However, because non-blocking (overlapped in Windows-speak) read/write is not supported for unnamed pipes, two reader threads are created for each new child process. These threads never touch memory allocated by R and thus they will not interfere with R interpreter's memory management (garbage collection).

is_process_handle() verifies that an object is a valid process handle as returned by spawn_process().

TERMINATION_GROUP: process_terminate(handle) and process_kill(handle) deliver the signal to the child process pointed to by handle and all of its descendants.

TERMINATION_CHILD_ONLY: process_terminate(handle) and process_kill(handle) deliver the signal only to the child process pointed to by handle but to none of its descendants.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
spawn_process(command, arguments = character(), environment = character(),
  workdir = "", termination_mode = TERMINATION_GROUP)

## S3 method for class 'process_handle'
print(x, ...)

is_process_handle(x)

TERMINATION_GROUP

TERMINATION_CHILD_ONLY

Arguments

command

Path to the executable.

arguments

Optional arguments for the program.

environment

Optional environment.

workdir

Optional new working directory.

termination_mode

Either TERMINATION_GROUP or TERMINATION_CHILD_ONLY.

x

Object to be printed or tested.

...

Other parameters passed to the print method.

Format

TERMINATION_GROUP and TERMINATION_CHILD_ONLY are single character values.

Details

command is always prepended to arguments so that the child process can correcty recognize the name of its executable via its argv vector. This is done automatically by spawn_process.

environment can be passed as a character vector whose elements take the form "NAME=VALUE", a named character vector or a named list.

workdir is the path to the directory where the new process is ought to be started. NULL and "" mean that working directory is inherited from the parent.

Value

spawn_process() returns an object of the process handle class.

Termination

The termination_mode specifies what should happen when process_terminate() or process_kill() is called on a subprocess. If it is set to TERMINATION_GROUP, then the termination signal is sent to the parent and all its descendants (sub-processes). If termination mode is set to TERMINATION_CHILD_ONLY, only the child process spawned directly from the R session receives the signal.

In Windows this is implemented with the job API, namely CreateJobObject(), AssignProcessToJobObject() and TerminateJobObject(). In Linux, the child calls setsid() after fork() but before execve(), and kill() is called with the negate process id.


subprocess documentation built on May 2, 2019, 4:04 p.m.