486 lines
13 KiB
HTML
486 lines
13 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of VFORK</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>VFORK</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
vfork - create a child process and block parent
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>pid_t vfork(void);</B>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>vfork</B>():
|
|
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
|
|
<DL COMPACT>
|
|
<DT id="2">Since glibc 2.12:<DD>
|
|
<PRE>
|
|
(_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
|
|
|| /* Since glibc 2.19: */ _DEFAULT_SOURCE
|
|
|| /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
<DT id="3"></PRE>
|
|
|
|
<DD>
|
|
Before glibc 2.12:
|
|
_BSD_SOURCE || _XOPEN_SOURCE >= 500
|
|
|
|
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Standard description</H3>
|
|
|
|
(From POSIX.1)
|
|
The
|
|
<B>vfork</B>()
|
|
|
|
function has the same effect as
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
except that the behavior is undefined if the process created by
|
|
<B>vfork</B>()
|
|
|
|
either modifies any data other than a variable of type
|
|
<I>pid_t</I>
|
|
|
|
used to store the return value from
|
|
<B>vfork</B>(),
|
|
|
|
or returns from the function in which
|
|
<B>vfork</B>()
|
|
|
|
was called, or calls any other function before successfully calling
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2)
|
|
|
|
or one of the
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exec">exec</A></B>(3)
|
|
|
|
family of functions.
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Linux description</H3>
|
|
|
|
<B>vfork</B>(),
|
|
|
|
just like
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
creates a child process of the calling process.
|
|
For details and return value and errors, see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2).
|
|
|
|
<P>
|
|
|
|
<B>vfork</B>()
|
|
|
|
is a special case of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
It is used to create new processes without copying the page tables of
|
|
the parent process.
|
|
It may be useful in performance-sensitive applications
|
|
where a child is created which then immediately issues an
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
|
|
|
|
<P>
|
|
|
|
<B>vfork</B>()
|
|
|
|
differs from
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
in that the calling thread is suspended until the child terminates
|
|
(either normally,
|
|
by calling
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
|
|
|
|
or abnormally, after delivery of a fatal signal),
|
|
or it makes a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
|
|
|
|
Until that point, the child shares all memory with its parent,
|
|
including the stack.
|
|
The child must not return from the current function or call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
|
|
|
|
(which would have the effect of calling exit handlers
|
|
established by the parent process and flushing the parent's
|
|
<B><A HREF="/cgi-bin/man/man2html?3+stdio">stdio</A></B>(3)
|
|
|
|
buffers), but may call
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2).
|
|
|
|
<P>
|
|
|
|
As with
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
the child process created by
|
|
<B>vfork</B>()
|
|
|
|
inherits copies of various of the caller's process attributes
|
|
(e.g., file descriptors, signal dispositions, and current working directory);
|
|
the
|
|
<B>vfork</B>()
|
|
|
|
call differs only in the treatment of the virtual address space,
|
|
as described above.
|
|
<P>
|
|
|
|
Signals sent to the parent
|
|
arrive after the child releases the parent's memory
|
|
(i.e., after the child terminates
|
|
or calls
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)).
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H3>Historic description</H3>
|
|
|
|
Under Linux,
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
is implemented using copy-on-write pages, so the only penalty incurred by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
is the time and memory required to duplicate the parent's page tables,
|
|
and to create a unique task structure for the child.
|
|
However, in the bad old days a
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
would require making a complete copy of the caller's data space,
|
|
often needlessly, since usually immediately afterward an
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exec">exec</A></B>(3)
|
|
|
|
is done.
|
|
Thus, for greater efficiency, BSD introduced the
|
|
<B>vfork</B>()
|
|
|
|
system call, which did not fully copy the address space of
|
|
the parent process, but borrowed the parent's memory and thread
|
|
of control until a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
|
|
|
|
or an exit occurred.
|
|
The parent process was suspended while the
|
|
child was using its resources.
|
|
The use of
|
|
<B>vfork</B>()
|
|
|
|
was tricky: for example, not modifying data
|
|
in the parent process depended on knowing which variables were
|
|
held in a register.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
4.3BSD; POSIX.1-2001 (but marked OBSOLETE).
|
|
POSIX.1-2008 removes the specification of
|
|
<B>vfork</B>().
|
|
|
|
<P>
|
|
|
|
The requirements put on
|
|
<B>vfork</B>()
|
|
|
|
by the standards are weaker than those put on
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
so an implementation where the two are synonymous is compliant.
|
|
In particular, the programmer cannot rely on the parent
|
|
remaining blocked until the child either terminates or calls
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
|
|
|
|
and cannot rely on any specific behavior with respect to shared memory.
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<P>
|
|
|
|
Some consider the semantics of
|
|
<B>vfork</B>()
|
|
|
|
to be an architectural blemish, and the 4.2BSD man page stated:
|
|
"This system call will be eliminated when proper system sharing mechanisms
|
|
are implemented.
|
|
Users should not depend on the memory sharing semantics of
|
|
<B>vfork</B>()
|
|
|
|
as it will, in that case, be made synonymous to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)."
|
|
However, even though modern memory management hardware
|
|
has decreased the performance difference between
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
and
|
|
<B>vfork</B>(),
|
|
|
|
there are various reasons why Linux and other systems have retained
|
|
<B>vfork</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="4">*<DD>
|
|
Some performance-critical applications require the small performance
|
|
advantage conferred by
|
|
<B>vfork</B>().
|
|
|
|
<DT id="5">*<DD>
|
|
<B>vfork</B>()
|
|
|
|
can be implemented on systems that lack a memory-management unit (MMU), but
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
can't be implemented on such systems.
|
|
(POSIX.1-2008 removed
|
|
<B>vfork</B>()
|
|
|
|
from the standard; the POSIX rationale for the
|
|
<B><A HREF="/cgi-bin/man/man2html?3+posix_spawn">posix_spawn</A></B>(3)
|
|
|
|
function notes that that function,
|
|
which provides functionality equivalent to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)+<B><A HREF="/cgi-bin/man/man2html?3+exec">exec</A></B>(3),
|
|
|
|
is designed to be implementable on systems that lack an MMU.)
|
|
|
|
|
|
|
|
|
|
<DT id="6">*<DD>
|
|
On systems where memory is constrained,
|
|
<B>vfork</B>()
|
|
|
|
avoids the need to temporarily commit memory (see the description of
|
|
<I>/proc/sys/vm/overcommit_memory</I>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5))
|
|
|
|
in order to execute a new program.
|
|
(This can be especially beneficial where a large parent process wishes
|
|
to execute a small helper program in a child process.)
|
|
By contrast, using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
in this scenario requires either committing an amount of memory equal
|
|
to the size of the parent process (if strict overcommitting is in force)
|
|
or overcommitting memory with the risk that a process is terminated
|
|
by the out-of-memory (OOM) killer.
|
|
|
|
</DL>
|
|
<A NAME="lbAJ"> </A>
|
|
<H3>Caveats</H3>
|
|
|
|
The child process should take care not to modify the memory in unintended ways,
|
|
since such changes will be seen by the parent process once
|
|
the child terminates or executes another program.
|
|
In this regard, signal handlers can be especially problematic:
|
|
if a signal handler that is invoked in the child of
|
|
<B>vfork</B>()
|
|
|
|
changes memory, those changes may result in an inconsistent process state
|
|
from the perspective of the parent process
|
|
(e.g., memory changes would be visible in the parent,
|
|
but changes to the state of open file descriptors would not be visible).
|
|
<P>
|
|
|
|
When
|
|
<B>vfork</B>()
|
|
|
|
is called in a multithreaded process,
|
|
only the calling thread is suspended until the child terminates
|
|
or executes a new program.
|
|
This means that the child is sharing an address space with other running code.
|
|
This can be dangerous if another thread in the parent process
|
|
changes credentials (using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setuid">setuid</A></B>(2)
|
|
|
|
or similar),
|
|
since there are now two processes with different privilege levels
|
|
running in the same address space.
|
|
As an example of the dangers,
|
|
suppose that a multithreaded program running as root creates a child using
|
|
<B>vfork</B>().
|
|
|
|
After the
|
|
<B>vfork</B>(),
|
|
|
|
a thread in the parent process drops the process to an unprivileged user
|
|
in order to run some untrusted code
|
|
(e.g., perhaps via plug-in opened with
|
|
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3)).
|
|
|
|
In this case, attacks are possible where the parent process uses
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
|
|
|
|
to map in code that will be executed by the privileged child process.
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Linux notes</H3>
|
|
|
|
Fork handlers established using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_atfork">pthread_atfork</A></B>(3)
|
|
|
|
are not called when a multithreaded program employing
|
|
the NPTL threading library calls
|
|
<B>vfork</B>().
|
|
|
|
Fork handlers are called in this case in a program using the
|
|
LinuxThreads threading library.
|
|
(See
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
|
|
|
|
for a description of Linux threading libraries.)
|
|
<P>
|
|
|
|
A call to
|
|
<B>vfork</B>()
|
|
|
|
is equivalent to calling
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
|
|
|
|
with
|
|
<I>flags</I>
|
|
|
|
specified as:
|
|
<P>
|
|
|
|
<BR> CLONE_VM | CLONE_VFORK | SIGCHLD
|
|
<A NAME="lbAL"> </A>
|
|
<H3>History</H3>
|
|
|
|
The
|
|
<B>vfork</B>()
|
|
|
|
system call appeared in 3.0BSD.
|
|
|
|
|
|
In 4.4BSD it was made synonymous to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
but NetBSD introduced it again;
|
|
see
|
|
|
|
|
|
In Linux, it has been equivalent to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
until 2.2.0-pre6 or so.
|
|
Since 2.2.0-pre9 (on i386, somewhat later on
|
|
other architectures) it is an independent system call.
|
|
Support was added in glibc 2.0.112.
|
|
<A NAME="lbAM"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
<P>
|
|
|
|
Details of the signal handling are obscure and differ between systems.
|
|
The BSD man page states:
|
|
"To avoid a possible deadlock situation, processes that are children
|
|
in the middle of a
|
|
<B>vfork</B>()
|
|
|
|
are never sent
|
|
<B>SIGTTOU</B>
|
|
|
|
or
|
|
<B>SIGTTIN</B>
|
|
|
|
signals; rather, output or
|
|
<I>ioctl</I>s
|
|
|
|
are allowed and input attempts result in an end-of-file indication."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+unshare">unshare</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait">wait</A></B>(2)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="10"><A HREF="#lbAE">Standard description</A><DD>
|
|
<DT id="11"><A HREF="#lbAF">Linux description</A><DD>
|
|
<DT id="12"><A HREF="#lbAG">Historic description</A><DD>
|
|
</DL>
|
|
<DT id="13"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="14"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="15"><A HREF="#lbAJ">Caveats</A><DD>
|
|
<DT id="16"><A HREF="#lbAK">Linux notes</A><DD>
|
|
<DT id="17"><A HREF="#lbAL">History</A><DD>
|
|
</DL>
|
|
<DT id="18"><A HREF="#lbAM">BUGS</A><DD>
|
|
<DT id="19"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="20"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:35 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|