{"size": 1910, "ext": "scrbl", "max_stars_repo_path": "pkgs/racket-doc/scribblings/inside/subprocesses.scrbl", "max_stars_repo_name": "lkh01/racket", "max_stars_repo_stars_event_min_datetime": "2018-07-05T21:18:24.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-04T14:48:37.000Z", "max_issues_repo_path": "pkgs/racket-doc/scribblings/inside/subprocesses.scrbl", "max_issues_repo_name": "lkh01/racket", "max_issues_repo_issues_event_min_datetime": "2017-05-10T07:33:20.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-19T15:49:46.000Z", "max_forks_repo_path": "pkgs/racket-doc/scribblings/inside/subprocesses.scrbl", "max_forks_repo_name": "lkh01/racket", "max_forks_repo_forks_event_min_datetime": "2016-04-25T11:47:57.000Z", "max_forks_repo_forks_event_max_datetime": "2017-05-09T14:52:48.000Z"}
{"max_stars_count": 4, "max_issues_count": 9, "max_forks_count": 3, "avg_line_length": 47.75, "max_line_length": 70, "alphanum_fraction": 0.7780104712}
#lang scribble/doc
@(require "utils.rkt"
          (for-label racket/system))

@title{Subprocesses}

On Unix and Mac OS, subprocess handling involves
@as-index[@cpp{fork}], @as-index[@cpp{waitpid}], and
@as-index[@cpp{SIGCHLD}], which creates a variety of issues within an
embedding application. On Windows, subprocess handling is more
straightforward, since no @cpp{fork} is required, and since Windows
provides an abstraction that is a close fit to Racket's subprocess
values.

After Racket creates a subprocess via @racket[subprocess] (or
@racket[system], @racket[process], etc.), it periodically polls the
process status using @cpp{waitpid}. If the process is created as its
own group, then the call to @cpp{waitpid} uses the created
subprocess's process ID; for all other subprocesses, polling uses a
single call to @cpp{waitpid} with the first argument as @cpp{0}. Using
@cpp{0}, in particular, can interfere with other libraries in an
embedding context, so Racket refrains from calling @cpp{waitpid} if no
subprocesses are pending.

Racket may or may not rely on a @cpp{SIGCHLD} handler, and it may or
may not block @cpp{SIGCHLD}. Currently, when Racket is compiled to
support @|tech-place|s, Racket blocks @cpp{SIGCHLD} on start up with
the expectation that all created threads have @cpp{SIGCHLD} blocked.
When Racket is not compiled to support @|tech-place|s, then a
@cpp{SIGCHLD} handler is installed.

Using @cpp{fork} in an application that embeds Racket is problematic
for several reasons: Racket may install a @cpp{SIGALRM} handler and
schedule alarms to implement context switches, it may have file
descriptors open that should be closed in a child process, and it may
have changed the disposition of signals such as
@cpp{SIGCHLD}. Consequently, embedding Racket in a process that
@cpp{fork}s is technically not supported; in the future, Racket may
provide better support for such applications.
