470 lines
12 KiB
HTML
470 lines
12 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of FORK</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>FORK</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>
|
|
|
|
fork - create a child process
|
|
<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 fork(void);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>fork</B>()
|
|
|
|
creates a new process by duplicating the calling process.
|
|
The new process is referred to as the
|
|
<I>child</I>
|
|
|
|
process.
|
|
The calling process is referred to as the
|
|
<I>parent</I>
|
|
|
|
process.
|
|
<P>
|
|
|
|
The child process and the parent process run in separate memory spaces.
|
|
At the time of
|
|
<B>fork</B>()
|
|
|
|
both memory spaces have the same content.
|
|
Memory writes, file mappings
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)),
|
|
|
|
and unmappings
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+munmap">munmap</A></B>(2))
|
|
|
|
performed by one of the processes do not affect the other.
|
|
<P>
|
|
|
|
The child process is an exact duplicate of the parent
|
|
process except for the following points:
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
The child has its own unique process ID,
|
|
and this PID does not match the ID of any existing process group
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+setpgid">setpgid</A></B>(2))
|
|
|
|
or session.
|
|
<DT id="2">*<DD>
|
|
The child's parent process ID is the same as the parent's process ID.
|
|
<DT id="3">*<DD>
|
|
The child does not inherit its parent's memory locks
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+mlock">mlock</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mlockall">mlockall</A></B>(2)).
|
|
|
|
<DT id="4">*<DD>
|
|
Process resource utilizations
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+getrusage">getrusage</A></B>(2))
|
|
|
|
and CPU time counters
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+times">times</A></B>(2))
|
|
|
|
are reset to zero in the child.
|
|
<DT id="5">*<DD>
|
|
The child's set of pending signals is initially empty
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+sigpending">sigpending</A></B>(2)).
|
|
|
|
<DT id="6">*<DD>
|
|
The child does not inherit semaphore adjustments from its parent
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+semop">semop</A></B>(2)).
|
|
|
|
<DT id="7">*<DD>
|
|
The child does not inherit process-associated record locks from its parent
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)).
|
|
|
|
(On the other hand, it does inherit
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
|
|
|
|
open file description locks and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+flock">flock</A></B>(2)
|
|
|
|
locks from its parent.)
|
|
<DT id="8">*<DD>
|
|
The child does not inherit timers from its parent
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+setitimer">setitimer</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+alarm">alarm</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+timer_create">timer_create</A></B>(2)).
|
|
|
|
<DT id="9">*<DD>
|
|
The child does not inherit outstanding asynchronous I/O operations
|
|
from its parent
|
|
(<B><A HREF="/cgi-bin/man/man2html?3+aio_read">aio_read</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+aio_write">aio_write</A></B>(3)),
|
|
|
|
nor does it inherit any asynchronous I/O contexts from its parent (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+io_setup">io_setup</A></B>(2)).
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
The process attributes in the preceding list are all specified
|
|
in POSIX.1.
|
|
The parent and child also differ with respect to the following
|
|
Linux-specific process attributes:
|
|
<DL COMPACT>
|
|
<DT id="10">*<DD>
|
|
The child does not inherit directory change notifications (dnotify)
|
|
from its parent
|
|
(see the description of
|
|
<B>F_NOTIFY</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)).
|
|
|
|
<DT id="11">*<DD>
|
|
The
|
|
<B><A HREF="/cgi-bin/man/man2html?2+prctl">prctl</A></B>(2)
|
|
|
|
<B>PR_SET_PDEATHSIG</B>
|
|
|
|
setting is reset so that the child does not receive a signal
|
|
when its parent terminates.
|
|
<DT id="12">*<DD>
|
|
The default timer slack value is set to the parent's
|
|
current timer slack value.
|
|
See the description of
|
|
<B>PR_SET_TIMERSLACK</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+prctl">prctl</A></B>(2).
|
|
|
|
<DT id="13">*<DD>
|
|
Memory mappings that have been marked with the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+madvise">madvise</A></B>(2)
|
|
|
|
<B>MADV_DONTFORK</B>
|
|
|
|
flag are not inherited across a
|
|
<B>fork</B>().
|
|
|
|
<DT id="14">*<DD>
|
|
Memory in address ranges that have been marked with the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+madvise">madvise</A></B>(2)
|
|
|
|
<B>MADV_WIPEONFORK</B>
|
|
|
|
flag is zeroed in the child after a
|
|
<B>fork</B>().
|
|
|
|
(The
|
|
<B>MADV_WIPEONFORK</B>
|
|
|
|
setting remains in place for those address ranges in the child.)
|
|
<DT id="15">*<DD>
|
|
The termination signal of the child is always
|
|
<B>SIGCHLD</B>
|
|
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)).
|
|
|
|
<DT id="16">*<DD>
|
|
The port access permission bits set by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ioperm">ioperm</A></B>(2)
|
|
|
|
are not inherited by the child;
|
|
the child must turn on any bits that it requires using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ioperm">ioperm</A></B>(2).
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
Note the following further points:
|
|
<DL COMPACT>
|
|
<DT id="17">*<DD>
|
|
The child process is created with a single thread---the
|
|
one that called
|
|
<B>fork</B>().
|
|
|
|
The entire virtual address space of the parent is replicated in the child,
|
|
including the states of mutexes, condition variables,
|
|
and other pthreads objects; the use of
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_atfork">pthread_atfork</A></B>(3)
|
|
|
|
may be helpful for dealing with problems that this can cause.
|
|
<DT id="18">*<DD>
|
|
After a
|
|
<B>fork</B>()
|
|
|
|
in a multithreaded program,
|
|
the child can safely call only async-signal-safe functions (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal-safety">signal-safety</A></B>(7))
|
|
|
|
until such time as it calls
|
|
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
|
|
|
|
<DT id="19">*<DD>
|
|
The child inherits copies of the parent's set of open file descriptors.
|
|
Each file descriptor in the child refers to the same
|
|
open file description (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
|
|
|
|
as the corresponding file descriptor in the parent.
|
|
This means that the two file descriptors share open file status flags,
|
|
file offset,
|
|
and signal-driven I/O attributes (see the description of
|
|
<B>F_SETOWN</B>
|
|
|
|
and
|
|
<B>F_SETSIG</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)).
|
|
|
|
<DT id="20">*<DD>
|
|
The child inherits copies of the parent's set of open message
|
|
queue descriptors (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+mq_overview">mq_overview</A></B>(7)).
|
|
|
|
Each file descriptor in the child refers to the same
|
|
open message queue description
|
|
as the corresponding file descriptor in the parent.
|
|
This means that the two file descriptors share the same flags
|
|
(<I>mq_flags</I>).
|
|
|
|
<DT id="21">*<DD>
|
|
The child inherits copies of the parent's set of open directory streams (see
|
|
<B><A HREF="/cgi-bin/man/man2html?3+opendir">opendir</A></B>(3)).
|
|
|
|
POSIX.1 says that the corresponding directory streams
|
|
in the parent and child
|
|
<I>may</I>
|
|
|
|
share the directory stream positioning;
|
|
on Linux/glibc they do not.
|
|
</DL>
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, the PID of the child process is returned in the parent,
|
|
and 0 is returned in the child.
|
|
On failure, -1 is returned in the parent,
|
|
no child process is created, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="22"><B>EAGAIN</B>
|
|
|
|
<DD>
|
|
|
|
A system-imposed limit on the number of threads was encountered.
|
|
There are a number of limits that may trigger this error:
|
|
<DL COMPACT><DT id="23"><DD>
|
|
<DL COMPACT>
|
|
<DT id="24">*<DD>
|
|
the
|
|
<B>RLIMIT_NPROC</B>
|
|
|
|
soft resource limit (set via
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setrlimit">setrlimit</A></B>(2)),
|
|
|
|
which limits the number of processes and threads for a real user ID,
|
|
was reached;
|
|
<DT id="25">*<DD>
|
|
the kernel's system-wide limit on the number of processes and threads,
|
|
<I>/proc/sys/kernel/threads-max</I>,
|
|
|
|
was reached (see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5));
|
|
|
|
<DT id="26">*<DD>
|
|
the maximum number of PIDs,
|
|
<I>/proc/sys/kernel/pid_max</I>,
|
|
|
|
was reached (see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5));
|
|
|
|
or
|
|
<DT id="27">*<DD>
|
|
the PID limit
|
|
(<I>pids.max</I>)
|
|
|
|
imposed by the cgroup "process number" (PIDs) controller was reached.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="28"><B>EAGAIN</B>
|
|
|
|
<DD>
|
|
The caller is operating under the
|
|
<B>SCHED_DEADLINE</B>
|
|
|
|
scheduling policy and does not have the reset-on-fork flag set.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
|
|
|
|
<DT id="29"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
<B>fork</B>()
|
|
|
|
failed to allocate the necessary kernel structures because memory is tight.
|
|
<DT id="30"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
An attempt was made to create a child process in a PID namespace
|
|
whose "init" process has terminated.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pid_namespaces">pid_namespaces</A></B>(7).
|
|
|
|
<DT id="31"><B>ENOSYS</B>
|
|
|
|
<DD>
|
|
<B>fork</B>()
|
|
|
|
is not supported on this platform (for example,
|
|
|
|
hardware without a Memory-Management Unit).
|
|
<DT id="32"><B>ERESTARTNOINTR</B> (since Linux 2.6.17)
|
|
|
|
<DD>
|
|
|
|
System call was interrupted by a signal and will be restarted.
|
|
(This can be seen only during a trace.)
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<P>
|
|
|
|
Under Linux,
|
|
<B>fork</B>()
|
|
|
|
is implemented using copy-on-write pages, so the only penalty that it incurs
|
|
is the time and memory required to duplicate the parent's page tables,
|
|
and to create a unique task structure for the child.
|
|
<A NAME="lbAI"> </A>
|
|
<H3>C library/kernel differences</H3>
|
|
|
|
Since version 2.3.3,
|
|
|
|
rather than invoking the kernel's
|
|
<B>fork</B>()
|
|
|
|
system call,
|
|
the glibc
|
|
<B>fork</B>()
|
|
|
|
wrapper that is provided as part of the
|
|
NPTL threading implementation invokes
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
|
|
|
|
with flags that provide the same effect as the traditional system call.
|
|
(A call to
|
|
<B>fork</B>()
|
|
|
|
is equivalent to a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
|
|
|
|
specifying
|
|
<I>flags</I>
|
|
|
|
as just
|
|
<B>SIGCHLD</B>.)
|
|
|
|
The glibc wrapper invokes any fork handlers that have been
|
|
established using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_atfork">pthread_atfork</A></B>(3).
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+pipe">pipe</A></B>(2)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait">wait</A></B>(2).
|
|
|
|
<A NAME="lbAK"> </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+setrlimit">setrlimit</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+unshare">unshare</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+vfork">vfork</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait">wait</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+daemon">daemon</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_atfork">pthread_atfork</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7)
|
|
|
|
<A NAME="lbAL"> </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="33"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="34"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="35"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="36"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="37"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="38"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="39"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="40"><A HREF="#lbAI">C library/kernel differences</A><DD>
|
|
</DL>
|
|
<DT id="41"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DT id="42"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="43"><A HREF="#lbAL">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:32 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|