'\"macro stdmacro
.if n .pH g3.vfork @(#)vfork	40.5 of 10/10/89
.\" Copyright 1989 AT&T
.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\"
.\" @(#)vfork.2 1.3 89/09/12 SMI; from UCB 6.2 6/30/85
'\" macro stdmacro
.\" Copyright (c) 1988 Sun Microsystems, Inc. - All Rights Reserved.
.\"
.nr X
.if \nX=0 .ds x} vfork 2 "UNIX System V" "\&"
.if \nX=1 .ds x} vfork 2 "UNIX System V"
.if \nX=2 .ds x} vfork 2 "" "\&"
.if \nX=3 .ds x} vfork "" "" "\&"
.TH \*(x}
.SH NAME
\f4vfork\f1 \- spawn new process in a virtual memory efficient way
.SH SYNOPSIS
.nf
.ft 4
#include <unistd.h>
.ft 1
.P
.ft 4
pid_t vfork (void);
.ft 1
.SH DESCRIPTION
.P
\f4vfork\f1
can be used to create new processes without fully copying the address
space of the old process, which is horrendously inefficient in a paged
environment.
It is useful when the purpose of
\f4fork\f1
would have been to create a new system context for an
\f4execve\f1.
\f4vfork\f1
differs from
\f4fork\f1
in that the child borrows the parent's memory and thread of
control until a call to
\f4execve\f1
or an exit (either by a call to
\f4exit\f1
or abnormally.)
The parent process is suspended while the child is using its resources.
.P
\f4vfork\f1
returns 0 in the child's context and (later) the
process
\s-1ID\s0
(\s-1PID\s0)
of the child in the parent's context.
.P
\f4vfork\f1
can normally be used just like
\f4fork\f1.
It does not work, however, to return while running in the child's context
from the procedure which called
\f4vfork\f1
since the eventual return from
\f4vfork\f1
would then return to a no longer existent stack frame.
Be careful, also, to call
\f4_exit\f1
rather than
\f4exit\f1
if you cannot
\f4execve\f1,
since
\f4exit\f1
will flush and close standard I/O channels, and thereby mess up the
parent processes standard I/O data structures.
Even with
\f4fork\f1
it is wrong to call
\f4exit\f1
since buffered data would then be flushed twice.
.SH DIAGNOSTICS
.P
Upon successful completion,
\f4vfork\f1
returns a value
of 0 to the child process and returns the process
\s-1ID\s0
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
\f4errno\f1
is set to indicate the error.
.PP
\f4vfork\f1
will fail and no child process will be created if one or more of
the following are true:
.TP 15
\s-1EAGAIN\s0
The system-imposed limit on the total
number of processes under execution would be exceeded.
This limit is determined when the system is generated.
.TP
\s-1EAGAIN\s0
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.
.TP
\s-1ENOMEM\s0
There is insufficient swap space for the new process.
.SH SEE ALSO
\f4exec\fP(2),
\f4exit\fP(2),
\f4fork\fP(2),
\f4ioctl\fP(2),
\f4wait\fP(2).
.SH NOTES
.P
This system call will be eliminated in a future release.
System implementation changes are making the efficiency gain of
\f4vfork\f1
over
\f4fork\f1
smaller.
The memory sharing semantics of
\f4vfork\f1
can be obtained through other mechanisms.
.P
To avoid a possible deadlock situation,
processes that are children in the middle of a
\f4vfork\f1
are never sent
\f4SIGTTOU\f1
or
\f4SIGTTIN\f1
signals; rather, output or
\f2ioctl\f1s
are allowed and input attempts result in an
\s-1EOF\s0
indication.
.P
On some systems, the implementation of
\f4vfork\f1
causes the parent to inherit register values from the child.
This can create problems for certain optimizing compilers if
\f4<unistd.h>\f1
is not included in the source calling
\f4vfork\f1.
