1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" A few fragments remain from an earlier (1992) page by
3 .\" Drew Eckhardt (drew@cs.colorado.edu),
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified by Michael Haardt (michael@moria.de)
28 .\" Modified Sat Jul 24 13:22:07 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified 21 Aug 1994 by Michael Chastain (mec@shell.portal.com):
30 .\" Referenced 'clone(2)'.
31 .\" Modified 1995-06-10, 1996-04-18, 1999-11-01, 2000-12-24
32 .\" by Andries Brouwer (aeb@cwi.nl)
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added notes on capability requirements
35 .\" 2006-09-04, Michael Kerrisk
36 .\" Greatly expanded, to describe all attributes that differ
39 .TH FORK 2 2014-05-12 "Linux" "Linux Programmer's Manual"
41 fork \- create a child process
43 .B #include <unistd.h>
48 creates a new process by duplicating the calling process.
49 The new process, referred to as the \fIchild\fP,
50 is an exact duplicate of the calling process,
51 referred to as the \fIparent\fP, except for the following points:
53 The child has its own unique process ID,
54 and this PID does not match the ID of any existing process group
57 The child's parent process ID is the same as the parent's process ID.
59 The child does not inherit its parent's memory locks
63 Process resource utilizations
67 are reset to zero in the child.
69 The child's set of pending signals is initially empty
70 .RB ( sigpending (2)).
72 The child does not inherit semaphore adjustments from its parent
75 The child does not inherit record locks from its parent
78 The child does not inherit timers from its parent
81 .BR timer_create (2)).
83 The child does not inherit outstanding asynchronous I/O operations
87 nor does it inherit any asynchronous I/O contexts from its parent (see
90 The process attributes in the preceding list are all specified
92 The parent and child also differ with respect to the following
93 Linux-specific process attributes:
95 The child does not inherit directory change notifications (dnotify)
97 (see the description of
105 setting is reset so that the child does not receive a signal
106 when its parent terminates.
108 The default timer slack value is set to the parent's
109 current timer slack value.
110 See the description of
111 .BR PR_SET_TIMERSLACK
115 Memory mappings that have been marked with the
118 flag are not inherited across a
121 The termination signal of the child is always
126 The port access permission bits set by
128 are not inherited by the child;
129 the child must turn on any bits that it requires using
132 Note the following further points:
134 The child process is created with a single thread\(emthe
137 The entire virtual address space of the parent is replicated in the child,
138 including the states of mutexes, condition variables,
139 and other pthreads objects; the use of
140 .BR pthread_atfork (3)
141 may be helpful for dealing with problems that this can cause.
143 The child inherits copies of the parent's set of open file descriptors.
144 Each file descriptor in the child refers to the same
145 open file description (see
147 as the corresponding file descriptor in the parent.
148 This means that the two descriptors share open file status flags,
150 and signal-driven I/O attributes (see the description of
157 The child inherits copies of the parent's set of open message
158 queue descriptors (see
159 .BR mq_overview (7)).
160 Each descriptor in the child refers to the same
161 open message queue description
162 as the corresponding descriptor in the parent.
163 This means that the two descriptors share the same flags
166 The child inherits copies of the parent's set of open directory streams (see
168 POSIX.1-2001 says that the corresponding directory streams
169 in the parent and child
171 share the directory stream positioning;
172 on Linux/glibc they do not.
174 On success, the PID of the child process is returned in the parent,
175 and 0 is returned in the child.
176 On failure, \-1 is returned in the parent,
177 no child process is created, and
179 is set appropriately.
184 cannot allocate sufficient memory to copy the parent's page tables and
185 allocate a task structure for the child.
188 It was not possible to create a new process because the caller's
190 resource limit was encountered.
191 To exceed this limit, the process must have either the
198 The caller is operating under the
200 scheduling policy and does not have the reset-on-fork flag set.
206 failed to allocate the necessary kernel structures because memory is tight.
210 is not supported on this platform (for example,
211 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
212 hardware without a Memory-Management Unit).
214 SVr4, 4.3BSD, POSIX.1-2001.
219 is implemented using copy-on-write pages, so the only penalty that it incurs
220 is the time and memory required to duplicate the parent's page tables,
221 and to create a unique task structure for the child.
224 .\" nptl/sysdeps/unix/sysv/linux/fork.c
225 rather than invoking the kernel's
230 wrapper that is provided as part of the
231 NPTL threading implementation invokes
233 with flags that provide the same effect as the traditional system call.
236 is equivalent to a call to
242 The glibc wrapper invokes any fork handlers that have been
244 .BR pthread_atfork (3).
245 .\" and does some magic to ensure that getpid(2) returns the right value.
260 .BR capabilities (7),
263 This page is part of release 3.67 of the Linux
266 A description of the project,
267 information about reporting bugs,
268 and the latest version of this page,
270 \%http://www.kernel.org/doc/man\-pages/.