VFORK(2) BSD Programmer's Manual VFORK(2)NAMEvfork - create a new process in a virtual memory efficient way
SYNOPSIS
#include <unistd.h>
int
vfork(void);
DESCRIPTIONVfork() creates a new process that shares the virtual memory of the cur-
rent process. Vfork() can be a more efficient replacement for fork(2)
when the purpose of fork(2) would have been to create a new system con-
text for an execve(2). The parent process is suspended until the child
makes a successful call to execve(2) or exits (either by calling _exit(2)
or by receiving a fatal signal).
These rules of programming must be followed in order to use vfork() safe-
ly:
o The function that calls vfork() must not return in the child process.
The child process must call execve(2) or exit without returning.
Failure to observe this rule can result in very mysterious core dumps
or stack corruption in the parent process.
o The child process should call _exit(2) rather than exit(3) to exit
explicitly. The exit(3) function flushes buffered I/O, executes C++
destructor functions and calls functions that have been registered
using atexit(3). Since the child process shares memory with the par-
ent process, these actions may inappropriately change the state of
the parent process.
o Be very careful about any action in the child that might change the
state of the parent in unexpected ways. For example, a call to
setenv(3) in the child process after a call to vfork() may change the
environment list in both the parent and the child.
A call to vfork() is exactly the same as a call to sfork(2), with param-
eters as follows:
sfork(SF_MEM|SF_WAITCHILD, NULL, SIGCHLD)
SEE ALSOfork(2), execve(2), sfork(2), sigaction(2), wait(2).
ERRORSVfork() will fail and no child process will be created if:
[EAGAIN] The system-imposed limit on the total number of processes under
execution would be exceeded. This limit is configuration-de-
pendent.
[EAGAIN] The system-imposed RLIMIT_NPROC resource limit on the total
number of processes under execution by the invoking user would
be exceeded. See setrlimit(2).
[ENOMEM] There is insufficient swap space for the new process.
BUGSVfork() is extremely easy to misuse. Unless your program really needs
the efficiency advantage of vfork(), you should use fork(2) instead.
To avoid a possible deadlock situation, processes that are children in
the middle of a vfork() are never sent SIGTTOU or SIGTTIN signals;
rather, output or ioctl(2) calls are allowed and input attempts result in
an end-of-file indication.
HISTORY
The vfork function call appeared in 3.0BSD.
4th Berkeley Distribution September 14, 2000 2