]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/fork.2
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man2 / fork.2
CommitLineData
c11b1abf 1.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
f5e74ede
MK
2.\" A few fragments remain from an earlier (1992) page by
3.\" Drew Eckhardt (drew@cs.colorado.edu),
fea681da 4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
6.\"
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)
c11b1abf 13.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 14.\" Added notes on capability requirements
a99426d3
MK
15.\" 2006-09-04, Michael Kerrisk
16.\" Greatly expanded, to describe all attributes that differ
17.\" parent and child.
fea681da 18.\"
4c1c5274 19.TH fork 2 (date) "Linux man-pages (unreleased)"
fea681da
MK
20.SH NAME
21fork \- create a child process
643ff518
AC
22.SH LIBRARY
23Standard C library
8fc3b2cf 24.RI ( libc ", " \-lc )
fea681da 25.SH SYNOPSIS
c7db92b9 26.nf
fea681da 27.B #include <unistd.h>
68e4db0a 28.PP
fea681da 29.B pid_t fork(void);
c7db92b9 30.fi
fea681da 31.SH DESCRIPTION
e511ffb6 32.BR fork ()
a99426d3 33creates a new process by duplicating the calling process.
7f810a61
HS
34The new process is referred to as the
35.I child
36process.
37The calling process is referred to as the
38.I parent
39process.
efeece04 40.PP
7f810a61
HS
41The child process and the parent process run in separate memory spaces.
42At the time of
43.BR fork ()
44both memory spaces have the same content.
45Memory writes, file mappings
46.RB ( mmap (2)),
47and unmappings
48.RB ( munmap (2))
49performed by one of the processes do not affect the other.
efeece04 50.PP
7f810a61
HS
51The child process is an exact duplicate of the parent
52process except for the following points:
22356d97 53.IP \(bu 3
a99426d3
MK
54The child has its own unique process ID,
55and this PID does not match the ID of any existing process group
5a1fa66f
MK
56.RB ( setpgid (2))
57or session.
22356d97 58.IP \(bu
a99426d3 59The child's parent process ID is the same as the parent's process ID.
22356d97 60.IP \(bu
a99426d3
MK
61The child does not inherit its parent's memory locks
62.RB ( mlock (2),
63.BR mlockall (2)).
22356d97 64.IP \(bu
d9bfdb9c 65Process resource utilizations
a99426d3
MK
66.RB ( getrusage (2))
67and CPU time counters
68.RB ( times (2))
69are reset to zero in the child.
22356d97 70.IP \(bu
65e7dad2 71The child's set of pending signals is initially empty
a99426d3 72.RB ( sigpending (2)).
22356d97 73.IP \(bu
a99426d3
MK
74The child does not inherit semaphore adjustments from its parent
75.RB ( semop (2)).
22356d97 76.IP \(bu
5879ccc0 77The child does not inherit process-associated record locks from its parent
a99426d3 78.RB ( fcntl (2)).
5879ccc0
MK
79(On the other hand, it does inherit
80.BR fcntl (2)
81open file description locks and
82.BR flock (2)
83locks from its parent.)
22356d97 84.IP \(bu
0fb58b28 85The child does not inherit timers from its parent
f5e74ede 86.RB ( setitimer (2),
76375bc6 87.BR alarm (2),
804f03e6 88.BR timer_create (2)).
22356d97 89.IP \(bu
c13182ef 90The child does not inherit outstanding asynchronous I/O operations
a99426d3
MK
91from its parent
92.RB ( aio_read (3),
cb6a78e6 93.BR aio_write (3)),
f75ad1f3
YK
94nor does it inherit any asynchronous I/O contexts from its parent (see
95.BR io_setup (2)).
c13182ef
MK
96.PP
97The process attributes in the preceding list are all specified
9b612e3c 98in POSIX.1.
a99426d3
MK
99The parent and child also differ with respect to the following
100Linux-specific process attributes:
22356d97 101.IP \(bu 3
c13182ef 102The child does not inherit directory change notifications (dnotify)
a99426d3
MK
103from its parent
104(see the description of
c13182ef
MK
105.B F_NOTIFY
106in
a99426d3 107.BR fcntl (2)).
22356d97 108.IP \(bu
a99426d3
MK
109The
110.BR prctl (2)
111.B PR_SET_PDEATHSIG
c13182ef 112setting is reset so that the child does not receive a signal
a99426d3 113when its parent terminates.
22356d97 114.IP \(bu
e089cba1
MK
115The default timer slack value is set to the parent's
116current timer slack value.
117See the description of
1ae6b2c7 118.B PR_SET_TIMERSLACK
e089cba1
MK
119in
120.BR prctl (2).
22356d97 121.IP \(bu
c13182ef 122Memory mappings that have been marked with the
78cb5911
MK
123.BR madvise (2)
124.B MADV_DONTFORK
125flag are not inherited across a
2777b1ca 126.BR fork ().
22356d97 127.IP \(bu
64f89da8 128Memory in address ranges that have been marked with the
399f3e39
RR
129.BR madvise (2)
130.B MADV_WIPEONFORK
131flag is zeroed in the child after a
132.BR fork ().
292e6c07
MK
133(The
134.B MADV_WIPEONFORK
135setting remains in place for those address ranges in the child.)
22356d97 136.IP \(bu
8bd58774
MK
137The termination signal of the child is always
138.B SIGCHLD
a99426d3
MK
139(see
140.BR clone (2)).
22356d97 141.IP \(bu
acfd9925
MK
142The port access permission bits set by
143.BR ioperm (2)
144are not inherited by the child;
145the child must turn on any bits that it requires using
146.BR ioperm (2).
fea681da 147.PP
a99426d3 148Note the following further points:
22356d97 149.IP \(bu 3
5503c85e 150The child process is created with a single thread\(emthe
c13182ef 151one that called
2777b1ca 152.BR fork ().
a99426d3 153The entire virtual address space of the parent is replicated in the child,
c13182ef 154including the states of mutexes, condition variables,
a99426d3
MK
155and other pthreads objects; the use of
156.BR pthread_atfork (3)
157may be helpful for dealing with problems that this can cause.
22356d97 158.IP \(bu
2c7c50cd 159After a
bf7bc8b8 160.BR fork ()
2c7c50cd
MK
161in a multithreaded program,
162the child can safely call only async-signal-safe functions (see
28a4c58c 163.BR signal\-safety (7))
2c7c50cd
MK
164until such time as it calls
165.BR execve (2).
22356d97 166.IP \(bu
a99426d3 167The child inherits copies of the parent's set of open file descriptors.
c13182ef 168Each file descriptor in the child refers to the same
a99426d3
MK
169open file description (see
170.BR open (2))
171as the corresponding file descriptor in the parent.
d9cb0d7d 172This means that the two file descriptors share open file status flags,
c72249c5 173file offset,
a99426d3
MK
174and signal-driven I/O attributes (see the description of
175.B F_SETOWN
c13182ef 176and
a99426d3 177.B F_SETSIG
c13182ef 178in
a99426d3 179.BR fcntl (2)).
22356d97 180.IP \(bu
c13182ef 181The child inherits copies of the parent's set of open message
a99426d3
MK
182queue descriptors (see
183.BR mq_overview (7)).
d9cb0d7d 184Each file descriptor in the child refers to the same
a99426d3 185open message queue description
d9cb0d7d
MK
186as the corresponding file descriptor in the parent.
187This means that the two file descriptors share the same flags
a99426d3 188.RI ( mq_flags ).
22356d97 189.IP \(bu
f5e74ede
MK
190The child inherits copies of the parent's set of open directory streams (see
191.BR opendir (3)).
9b612e3c 192POSIX.1 says that the corresponding directory streams
f5e74ede
MK
193in the parent and child
194.I may
195share the directory stream positioning;
196on Linux/glibc they do not.
47297adb 197.SH RETURN VALUE
f5e74ede
MK
198On success, the PID of the child process is returned in the parent,
199and 0 is returned in the child.
200On failure, \-1 is returned in the parent,
201no child process is created, and
fea681da 202.I errno
f6a4078b 203is set to indicate the error.
fea681da
MK
204.SH ERRORS
205.TP
206.B EAGAIN
a9eb4a80 207.\" NOTE! The following should match the description in pthread_create(3)
41dfc98f 208A system-imposed limit on the number of threads was encountered.
bf54cc86
MK
209There are a number of limits that may trigger this error:
210.RS
22356d97 211.IP \(bu 3
bf54cc86 212the
1ae6b2c7 213.B RLIMIT_NPROC
41dfc98f
MK
214soft resource limit (set via
215.BR setrlimit (2)),
216which limits the number of processes and threads for a real user ID,
217was reached;
22356d97 218.IP \(bu
41dfc98f 219the kernel's system-wide limit on the number of processes and threads,
b49c2acb 220.IR /proc/sys/kernel/threads\-max ,
41dfc98f
MK
221was reached (see
222.BR proc (5));
22356d97 223.IP \(bu
91551f10 224the maximum number of PIDs,
41dfc98f
MK
225.IR /proc/sys/kernel/pid_max ,
226was reached (see
91551f10 227.BR proc (5));
bf54cc86 228or
22356d97 229.IP \(bu
bf54cc86
MK
230the PID limit
231.RI ( pids.max )
91551f10 232imposed by the cgroup "process number" (PIDs) controller was reached.
bf54cc86 233.RE
fea681da 234.TP
75becc94
MK
235.B EAGAIN
236The caller is operating under the
1ae6b2c7 237.B SCHED_DEADLINE
75becc94
MK
238scheduling policy and does not have the reset-on-fork flag set.
239See
240.BR sched (7).
241.TP
fea681da 242.B ENOMEM
e511ffb6 243.BR fork ()
fea681da 244failed to allocate the necessary kernel structures because memory is tight.
aed1f3b9 245.TP
f516d6d9
MK
246.B ENOMEM
247An attempt was made to create a child process in a PID namespace
248whose "init" process has terminated.
249See
250.BR pid_namespaces (7).
251.TP
aed1f3b9
MF
252.B ENOSYS
253.BR fork ()
254is not supported on this platform (for example,
255.\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
256hardware without a Memory-Management Unit).
10e46057
NF
257.TP
258.BR ERESTARTNOINTR " (since Linux 2.6.17)"
11a6d050 259.\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
10e46057
NF
260System call was interrupted by a signal and will be restarted.
261(This can be seen only during a trace.)
3113c7f3 262.SH STANDARDS
9b612e3c 263POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
a99426d3 264.SH NOTES
a99426d3
MK
265Under Linux,
266.BR fork ()
267is implemented using copy-on-write pages, so the only penalty that it incurs
268is the time and memory required to duplicate the parent's page tables,
269and to create a unique task structure for the child.
0722a578 270.SS C library/kernel differences
429e9867
MK
271Since version 2.3.3,
272.\" nptl/sysdeps/unix/sysv/linux/fork.c
273rather than invoking the kernel's
274.BR fork ()
275system call,
276the glibc
277.BR fork ()
278wrapper that is provided as part of the
279NPTL threading implementation invokes
280.BR clone (2)
281with flags that provide the same effect as the traditional system call.
6de06bb8
MK
282(A call to
283.BR fork ()
284is equivalent to a call to
285.BR clone (2)
286specifying
287.I flags
288as just
289.BR SIGCHLD .)
429e9867 290The glibc wrapper invokes any fork handlers that have been
7364dea6 291established using
429e9867
MK
292.BR pthread_atfork (3).
293.\" and does some magic to ensure that getpid(2) returns the right value.
a14af333 294.SH EXAMPLES
2b2581ee
MK
295See
296.BR pipe (2)
297and
94b639c1
AC
298.BR wait (2)
299for more examples.
300.PP
301.\" SRC BEGIN (fork.c)
302.EX
303#include <signal.h>
94b639c1 304#include <stdint.h>
76ee15b1 305#include <stdio.h>
94b639c1
AC
306#include <stdlib.h>
307#include <unistd.h>
308
309int
310main(void)
311{
312 pid_t pid;
313
314 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
315 perror("signal");
316 exit(EXIT_FAILURE);
317 }
318 pid = fork();
319 switch (pid) {
529027f0 320 case \-1:
94b639c1
AC
321 perror("fork");
322 exit(EXIT_FAILURE);
323 case 0:
324 puts("Child exiting.");
325 exit(EXIT_SUCCESS);
326 default:
327 printf("Child is PID %jd\en", (intmax_t) pid);
328 puts("Parent exiting.");
329 exit(EXIT_SUCCESS);
330 }
331}
332.EE
333.\" SRC END
47297adb 334.SH SEE ALSO
fea681da
MK
335.BR clone (2),
336.BR execve (2),
a44dc571 337.BR exit (2),
fea681da 338.BR setrlimit (2),
5cc01e9c 339.BR unshare (2),
fea681da
MK
340.BR vfork (2),
341.BR wait (2),
baf17bc4 342.BR daemon (3),
c5cd305d 343.BR pthread_atfork (3),
53a1443c
MK
344.BR capabilities (7),
345.BR credentials (7)