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-28 "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 process-associated record locks from its parent
77 (On the other hand, it does inherit
79 open file description locks and
81 locks from its parent.)
83 The child does not inherit timers from its parent
86 .BR timer_create (2)).
88 The child does not inherit outstanding asynchronous I/O operations
92 nor does it inherit any asynchronous I/O contexts from its parent (see
95 The process attributes in the preceding list are all specified
97 The parent and child also differ with respect to the following
98 Linux-specific process attributes:
100 The child does not inherit directory change notifications (dnotify)
102 (see the description of
110 setting is reset so that the child does not receive a signal
111 when its parent terminates.
113 The default timer slack value is set to the parent's
114 current timer slack value.
115 See the description of
116 .BR PR_SET_TIMERSLACK
120 Memory mappings that have been marked with the
123 flag are not inherited across a
126 The termination signal of the child is always
131 The port access permission bits set by
133 are not inherited by the child;
134 the child must turn on any bits that it requires using
137 Note the following further points:
139 The child process is created with a single thread\(emthe
142 The entire virtual address space of the parent is replicated in the child,
143 including the states of mutexes, condition variables,
144 and other pthreads objects; the use of
145 .BR pthread_atfork (3)
146 may be helpful for dealing with problems that this can cause.
148 The child inherits copies of the parent's set of open file descriptors.
149 Each file descriptor in the child refers to the same
150 open file description (see
152 as the corresponding file descriptor in the parent.
153 This means that the two descriptors share open file status flags,
155 and signal-driven I/O attributes (see the description of
162 The child inherits copies of the parent's set of open message
163 queue descriptors (see
164 .BR mq_overview (7)).
165 Each descriptor in the child refers to the same
166 open message queue description
167 as the corresponding descriptor in the parent.
168 This means that the two descriptors share the same flags
171 The child inherits copies of the parent's set of open directory streams (see
173 POSIX.1-2001 says that the corresponding directory streams
174 in the parent and child
176 share the directory stream positioning;
177 on Linux/glibc they do not.
179 On success, the PID of the child process is returned in the parent,
180 and 0 is returned in the child.
181 On failure, \-1 is returned in the parent,
182 no child process is created, and
184 is set appropriately.
189 cannot allocate sufficient memory to copy the parent's page tables and
190 allocate a task structure for the child.
193 \" NOTE! The following should match the description in pthread_create(3)
194 A system-imposed limit on the number of threads was encountered.
195 There are a number of limits that may trigger this error: the
197 soft resource limit (set via
199 which limits the number of processes and threads for a real user ID,
201 the kernel's system-wide limit on the number of processes and threads,
202 .IR /proc/sys/kernel/threads-max ,
205 or the maximum number of PIDs,
206 .IR /proc/sys/kernel/pid_max ,
211 The caller is operating under the
213 scheduling policy and does not have the reset-on-fork flag set.
219 failed to allocate the necessary kernel structures because memory is tight.
223 is not supported on this platform (for example,
224 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
225 hardware without a Memory-Management Unit).
227 SVr4, 4.3BSD, POSIX.1-2001.
232 is implemented using copy-on-write pages, so the only penalty that it incurs
233 is the time and memory required to duplicate the parent's page tables,
234 and to create a unique task structure for the child.
237 .\" nptl/sysdeps/unix/sysv/linux/fork.c
238 rather than invoking the kernel's
243 wrapper that is provided as part of the
244 NPTL threading implementation invokes
246 with flags that provide the same effect as the traditional system call.
249 is equivalent to a call to
255 The glibc wrapper invokes any fork handlers that have been
257 .BR pthread_atfork (3).
258 .\" and does some magic to ensure that getpid(2) returns the right value.
273 .BR capabilities (7),
276 This page is part of release 3.79 of the Linux
279 A description of the project,
280 information about reporting bugs,
281 and the latest version of this page,
283 \%http://www.kernel.org/doc/man\-pages/.