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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified by Michael Haardt (michael@moria.de)
8 .\" Modified Sat Jul 24 13:22:07 1993 by Rik Faith (faith@cs.unc.edu)
9 .\" Modified 21 Aug 1994 by Michael Chastain (mec@shell.portal.com):
10 .\" Referenced 'clone(2)'.
11 .\" Modified 1995-06-10, 1996-04-18, 1999-11-01, 2000-12-24
12 .\" by Andries Brouwer (aeb@cwi.nl)
13 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
14 .\" Added notes on capability requirements
15 .\" 2006-09-04, Michael Kerrisk
16 .\" Greatly expanded, to describe all attributes that differ
19 .TH fork 2 (date) "Linux man-pages (unreleased)"
21 fork \- create a child process
24 .RI ( libc ", " \-lc )
27 .B #include <unistd.h>
33 creates a new process by duplicating the calling process.
34 The new process is referred to as the
37 The calling process is referred to as the
41 The child process and the parent process run in separate memory spaces.
44 both memory spaces have the same content.
45 Memory writes, file mappings
49 performed by one of the processes do not affect the other.
51 The child process is an exact duplicate of the parent
52 process except for the following points:
54 The child has its own unique process ID,
55 and this PID does not match the ID of any existing process group
59 The child's parent process ID is the same as the parent's process ID.
61 The child does not inherit its parent's memory locks
65 Process resource utilizations
69 are reset to zero in the child.
71 The child's set of pending signals is initially empty
72 .RB ( sigpending (2)).
74 The child does not inherit semaphore adjustments from its parent
77 The child does not inherit process-associated record locks from its parent
79 (On the other hand, it does inherit
81 open file description locks and
83 locks from its parent.)
85 The child does not inherit timers from its parent
88 .BR timer_create (2)).
90 The child does not inherit outstanding asynchronous I/O operations
94 nor does it inherit any asynchronous I/O contexts from its parent (see
97 The process attributes in the preceding list are all specified
99 The parent and child also differ with respect to the following
100 Linux-specific process attributes:
102 The child does not inherit directory change notifications (dnotify)
104 (see the description of
112 setting is reset so that the child does not receive a signal
113 when its parent terminates.
115 The default timer slack value is set to the parent's
116 current timer slack value.
117 See the description of
122 Memory mappings that have been marked with the
125 flag are not inherited across a
128 Memory in address ranges that have been marked with the
131 flag is zeroed in the child after a
135 setting remains in place for those address ranges in the child.)
137 The termination signal of the child is always
142 The port access permission bits set by
144 are not inherited by the child;
145 the child must turn on any bits that it requires using
148 Note the following further points:
150 The child process is created with a single thread\[em]the
153 The entire virtual address space of the parent is replicated in the child,
154 including the states of mutexes, condition variables,
155 and other pthreads objects; the use of
156 .BR pthread_atfork (3)
157 may be helpful for dealing with problems that this can cause.
161 in a multithreaded program,
162 the child can safely call only async-signal-safe functions (see
163 .BR signal\-safety (7))
164 until such time as it calls
167 The child inherits copies of the parent's set of open file descriptors.
168 Each file descriptor in the child refers to the same
169 open file description (see
171 as the corresponding file descriptor in the parent.
172 This means that the two file descriptors share open file status flags,
174 and signal-driven I/O attributes (see the description of
181 The child inherits copies of the parent's set of open message
182 queue descriptors (see
183 .BR mq_overview (7)).
184 Each file descriptor in the child refers to the same
185 open message queue description
186 as the corresponding file descriptor in the parent.
187 This means that the two file descriptors share the same flags
190 The child inherits copies of the parent's set of open directory streams (see
192 POSIX.1 says that the corresponding directory streams
193 in the parent and child
195 share the directory stream positioning;
196 on Linux/glibc they do not.
198 On success, the PID of the child process is returned in the parent,
199 and 0 is returned in the child.
200 On failure, \-1 is returned in the parent,
201 no child process is created, and
203 is set to indicate the error.
207 .\" NOTE! The following should match the description in pthread_create(3)
208 A system-imposed limit on the number of threads was encountered.
209 There are a number of limits that may trigger this error:
214 soft resource limit (set via
216 which limits the number of processes and threads for a real user ID,
219 the kernel's system-wide limit on the number of processes and threads,
220 .IR /proc/sys/kernel/threads\-max ,
224 the maximum number of PIDs,
225 .IR /proc/sys/kernel/pid_max ,
232 imposed by the cgroup "process number" (PIDs) controller was reached.
236 The caller is operating under the
238 scheduling policy and does not have the reset-on-fork flag set.
244 failed to allocate the necessary kernel structures because memory is tight.
247 An attempt was made to create a child process in a PID namespace
248 whose "init" process has terminated.
250 .BR pid_namespaces (7).
254 is not supported on this platform (for example,
255 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
256 hardware without a Memory-Management Unit).
258 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
259 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
260 System call was interrupted by a signal and will be restarted.
261 (This can be seen only during a trace.)
263 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
267 is implemented using copy-on-write pages, so the only penalty that it incurs
268 is the time and memory required to duplicate the parent's page tables,
269 and to create a unique task structure for the child.
270 .SS C library/kernel differences
272 .\" nptl/sysdeps/unix/sysv/linux/fork.c
273 rather than invoking the kernel's
278 wrapper that is provided as part of the
279 NPTL threading implementation invokes
281 with flags that provide the same effect as the traditional system call.
284 is equivalent to a call to
290 The glibc wrapper invokes any fork handlers that have been
292 .BR pthread_atfork (3).
293 .\" and does some magic to ensure that getpid(2) returns the right value.
301 .\" SRC BEGIN (fork.c)
314 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
324 puts("Child exiting.");
327 printf("Child is PID %jd\en", (intmax_t) pid);
328 puts("Parent exiting.");
343 .BR pthread_atfork (3),
344 .BR capabilities (7),