shithub: riscv

ref: fc1d4d1b24669d870a45310f8122e6b10ff953d0
dir: /sys/man/2/fork/

View raw version
.TH FORK 2
.SH NAME
fork, rfork \- manipulate process resources
.SH SYNOPSIS
.B #include <u.h>
.br
.B #include <libc.h>
.PP
.nf
.B
int fork(void)
.PP
.B
int rfork(int flags)
.fi
.SH DESCRIPTION
Forking is the only way new processes are created.
The
.I flags
argument to
.I rfork
selects which resources of the
invoking process (parent) are shared
by the new process (child) or initialized to
their default values.
The resources include
the file name space,
the open file descriptor table (which, when shared, permits processes
to open and close files for other processes),
the set of environment variables
(see
.IR env (3)),
the note group
(the set of processes that receive notes written to a member's
.B notepg
file;
see
.IR proc (3)),
the set of rendezvous tags
(see
.IR rendezvous (2)); 
and open files.
.I Flags
is the logical OR of some subset of
.TF RFCNAMEG
.TP
.B RFPROC
If set a new process is created; otherwise changes affect the
current process.
.TP
.B RFNOWAIT
If set, the child process will be dissociated from the parent. Upon
exit the child will leave no
.B Waitmsg
(see
.IR wait (2))
for the parent to collect.
.TP
.B RFNAMEG
If set, the new process inherits a copy of the parent's name space;
otherwise the new process shares the parent's name space.
Is mutually exclusive with
.BR RFCNAMEG .
.TP
.B RFCNAMEG
If set, the new process starts with a clean name space. A new
name space must be built from a mount of an open file descriptor.
Is mutually exclusive with
.BR RFNAMEG .
.TP
.B RFNOMNT
If set, subsequent mounts into the new name space are disallowed.
All pathnames starting with
.B #
besides those used to access
.IR pipe (3),
.IR dup (3),
.IR env (3),
.IR cons (3),
and
.IR proc (3) 
can not be walked.
.TP
.B RFENVG
If set, the environment variables are copied;
otherwise the two processes share environment variables.
Is mutually exclusive with
.BR RFCENVG .
.TP
.B RFCENVG
If set, the new process starts with an empty environment.
Is mutually exclusive with
.BR RFENVG .
.TP
.B RFNOTEG
Each process is a member of a group of processes that all
receive notes when a note is written to any of their
.B notepg
files (see
.IR proc (3)).
The group of a new process is by default the same as its parent, but if
.B RFNOTEG
is set (regardless of
.BR RFPROC ),
the process becomes the first in a new group, isolated from
previous processes.
.TP
.B RFFDG
If set, the invoker's file descriptor table (see
.IR intro (2))
is copied; otherwise the two processes share a
single table.
.TP
.B RFCFDG
If set, the new process starts with a clean file descriptor table.
Is mutually exclusive with
.BR RFFDG .
.TP
.B RFREND
If set, the process will be unable to
.IR rendezvous (2)
with any of its ancestors; its children will, however, be able to
.B rendezvous
with it.  In effect,
.B RFREND
makes the process the first in a group of processes that share a space for
.B rendezvous
tags.
.TP
.B RFMEM
If set, the child and the parent will share
.B data
and
.B bss
segments.
Otherwise, the child inherits a copy of those segments.
Other segment types, in particular stack segments, will be unaffected.
May be set only with
.BR RFPROC .
.PD
.PP
File descriptors in a shared file descriptor table are kept
open until either they are explicitly closed
or all processes sharing the table exit.
.PP
If
.B RFPROC
is set, the
value returned in the parent process
is the process id
of the child process; the value returned in the child is zero.
Without
.BR RFPROC ,
the return value is zero.
Process ids range from 1 to the maximum positive integer
.RB ( int )
value.
.I Rfork
may also return -1 if process resources are exhausted.
.PP
.I Fork
is just a call of
.BR rfork(RFFDG|RFREND|RFPROC) .
.SH SOURCE
.B /sys/src/libc/9syscall
.br
.B /sys/src/libc/9sys/fork.c
.SH SEE ALSO
.IR intro (2),
.IR proc (3),
.SH DIAGNOSTICS
These functions set
.IR errstr .