vfork(2) vfork(2)
NAME
vfork - spawn new process in virtual memory
SYNOPSIS
#include <unistd.h>
pidt vfork(void);
DESCRIPTION
vfork() can be used to create new processes without fully copying the
address space of the old process. It is useful when the purpose of
fork() would have been to create a new system context for an execve().
vfork() differs from fork() in that the child borrows the parent's
memory and thread of control until a call to execve() or an exit
(either by a call to exit() or abnormally.) The parent process is
suspended while the child is using its resources.
vfork() returns 0 in the child's context and (later) the process ID
(PID) of the child in the parent's context.
vfork() can normally be used just like fork(). It does not work, how-
ever, to return while running in the child's context from the pro-
cedure which called vfork() since the eventual return from vfork()
would then return to a no longer existent stack frame. Be careful,
also, to call exit() rather than exit() if you cannot execve(), since
exit() will flush and close standard I/O channels, and thereby mess up
the parent processes standard I/O data structures. Even with fork() it
is wrong to call exit() since buffered data would then be flushed
twice.
RESULT
Upon successful completion, vfork() returns a value of 0 to the child
process and returns the process ID of the child process to the parent
process. Otherwise, a value of -1 is returned to the parent process,
no child process is created, and the global variable errno is set to
indicate the error.
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
vfork() will fail and no child process will be created if one or more
of the following apply:
EAGAIN The system-imposed limit on the total number of processes
under execution would be exceeded. This limit is determined
when the system is generated.
EAGAIN The system-imposed limit on the total number of processes
under execution by a single user would be exceeded. This
limit is determined when the system is generated.
Page 1 Reliant UNIX 5.44 Printed 11/98
vfork(2) vfork(2)
ENOMEM There is insufficient swap space for the new process.
APPLICATION USAGE
On some systems, vfork() is the same as fork().
The vfork() function differs from fork() only in that the child pro-
cess can share code and data with the calling process (parent pro-
cess). This speeds cloning activity significantly at a risk to the
integrity of the parent process if vfork() is misused.
The use of vfork() for any purpose except as a prelude to an immediate
call to a function from the exec family, or to exit(), is not
advised.
The vfork() function can be used to create new processes without fully
copying the address space of the old process. If a forked process is
simply going to call exec the data space copied from the parent to the
child by fork() is not used. This is particularly inefficient in a
paged environment, making vfork() particularly useful. Depending upon
the size of the parent's data space, vfork() can give a significant
performance improvement over fork().
If signal handlers are invoked in the child process after vfork(),
they must follow the same rules as other code in the child process.
The [vfork, exec] window begins at the vfork() call and ends when the
child completes its exec call.
SEE ALSO
exec(2), exit(2), fork(2), ioctl(2), wait(2), unistd(4).
Page 2 Reliant UNIX 5.44 Printed 11/98