]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fork.2
2173a4698d19bf048b9b43031f62fbed8d697e5d
[thirdparty/man-pages.git] / man2 / fork.2
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),
4 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
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
37 .\" parent and child.
38 .\"
39 .TH FORK 2 2016-03-15 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 fork \- create a child process
42 .SH SYNOPSIS
43 .B #include <unistd.h>
44 .sp
45 .B pid_t fork(void);
46 .SH DESCRIPTION
47 .BR fork ()
48 creates a new process by duplicating the calling process.
49 The new process is referred to as the
50 .I child
51 process.
52 The calling process is referred to as the
53 .I parent
54 process.
55
56 The child process and the parent process run in separate memory spaces.
57 At the time of
58 .BR fork ()
59 both memory spaces have the same content.
60 Memory writes, file mappings
61 .RB ( mmap (2)),
62 and unmappings
63 .RB ( munmap (2))
64 performed by one of the processes do not affect the other.
65
66 The child process is an exact duplicate of the parent
67 process except for the following points:
68 .IP * 3
69 The child has its own unique process ID,
70 and this PID does not match the ID of any existing process group
71 .RB ( setpgid (2)).
72 .IP *
73 The child's parent process ID is the same as the parent's process ID.
74 .IP *
75 The child does not inherit its parent's memory locks
76 .RB ( mlock (2),
77 .BR mlockall (2)).
78 .IP *
79 Process resource utilizations
80 .RB ( getrusage (2))
81 and CPU time counters
82 .RB ( times (2))
83 are reset to zero in the child.
84 .IP *
85 The child's set of pending signals is initially empty
86 .RB ( sigpending (2)).
87 .IP *
88 The child does not inherit semaphore adjustments from its parent
89 .RB ( semop (2)).
90 .IP *
91 The child does not inherit process-associated record locks from its parent
92 .RB ( fcntl (2)).
93 (On the other hand, it does inherit
94 .BR fcntl (2)
95 open file description locks and
96 .BR flock (2)
97 locks from its parent.)
98 .IP *
99 The child does not inherit timers from its parent
100 .RB ( setitimer (2),
101 .BR alarm (2),
102 .BR timer_create (2)).
103 .IP *
104 The child does not inherit outstanding asynchronous I/O operations
105 from its parent
106 .RB ( aio_read (3),
107 .BR aio_write (3)),
108 nor does it inherit any asynchronous I/O contexts from its parent (see
109 .BR io_setup (2)).
110 .PP
111 The process attributes in the preceding list are all specified
112 in POSIX.1.
113 The parent and child also differ with respect to the following
114 Linux-specific process attributes:
115 .IP * 3
116 The child does not inherit directory change notifications (dnotify)
117 from its parent
118 (see the description of
119 .B F_NOTIFY
120 in
121 .BR fcntl (2)).
122 .IP *
123 The
124 .BR prctl (2)
125 .B PR_SET_PDEATHSIG
126 setting is reset so that the child does not receive a signal
127 when its parent terminates.
128 .IP *
129 The default timer slack value is set to the parent's
130 current timer slack value.
131 See the description of
132 .BR PR_SET_TIMERSLACK
133 in
134 .BR prctl (2).
135 .IP *
136 Memory mappings that have been marked with the
137 .BR madvise (2)
138 .B MADV_DONTFORK
139 flag are not inherited across a
140 .BR fork ().
141 .IP *
142 The termination signal of the child is always
143 .B SIGCHLD
144 (see
145 .BR clone (2)).
146 .IP *
147 The port access permission bits set by
148 .BR ioperm (2)
149 are not inherited by the child;
150 the child must turn on any bits that it requires using
151 .BR ioperm (2).
152 .PP
153 Note the following further points:
154 .IP * 3
155 The child process is created with a single thread\(emthe
156 one that called
157 .BR fork ().
158 The entire virtual address space of the parent is replicated in the child,
159 including the states of mutexes, condition variables,
160 and other pthreads objects; the use of
161 .BR pthread_atfork (3)
162 may be helpful for dealing with problems that this can cause.
163 .IP *
164 After a
165 .BR fork (2)
166 in a multithreaded program,
167 the child can safely call only async-signal-safe functions (see
168 .BR signal (7))
169 until such time as it calls
170 .BR execve (2).
171 .IP *
172 The child inherits copies of the parent's set of open file descriptors.
173 Each file descriptor in the child refers to the same
174 open file description (see
175 .BR open (2))
176 as the corresponding file descriptor in the parent.
177 This means that the two file descriptors share open file status flags,
178 file offset,
179 and signal-driven I/O attributes (see the description of
180 .B F_SETOWN
181 and
182 .B F_SETSIG
183 in
184 .BR fcntl (2)).
185 .IP *
186 The child inherits copies of the parent's set of open message
187 queue descriptors (see
188 .BR mq_overview (7)).
189 Each file descriptor in the child refers to the same
190 open message queue description
191 as the corresponding file descriptor in the parent.
192 This means that the two file descriptors share the same flags
193 .RI ( mq_flags ).
194 .IP *
195 The child inherits copies of the parent's set of open directory streams (see
196 .BR opendir (3)).
197 POSIX.1 says that the corresponding directory streams
198 in the parent and child
199 .I may
200 share the directory stream positioning;
201 on Linux/glibc they do not.
202 .SH RETURN VALUE
203 On success, the PID of the child process is returned in the parent,
204 and 0 is returned in the child.
205 On failure, \-1 is returned in the parent,
206 no child process is created, and
207 .I errno
208 is set appropriately.
209 .SH ERRORS
210 .TP
211 .B EAGAIN
212 \" NOTE! The following should match the description in pthread_create(3)
213 A system-imposed limit on the number of threads was encountered.
214 There are a number of limits that may trigger this error: the
215 .BR RLIMIT_NPROC
216 soft resource limit (set via
217 .BR setrlimit (2)),
218 which limits the number of processes and threads for a real user ID,
219 was reached;
220 the kernel's system-wide limit on the number of processes and threads,
221 .IR /proc/sys/kernel/threads-max ,
222 was reached (see
223 .BR proc (5));
224 or the maximum number of PIDs,
225 .IR /proc/sys/kernel/pid_max ,
226 was reached (see
227 .BR proc (5)).
228 .TP
229 .B EAGAIN
230 The caller is operating under the
231 .BR SCHED_DEADLINE
232 scheduling policy and does not have the reset-on-fork flag set.
233 See
234 .BR sched (7).
235 .TP
236 .B ENOMEM
237 .BR fork ()
238 failed to allocate the necessary kernel structures because memory is tight.
239 .TP
240 .B ENOMEM
241 An attempt was made to create a child process in a PID namespace
242 whose "init" process has terminated.
243 See
244 .BR pid_namespaces (7).
245 .TP
246 .B ENOSYS
247 .BR fork ()
248 is not supported on this platform (for example,
249 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
250 hardware without a Memory-Management Unit).
251 .TP
252 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
253 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
254 System call was interrupted by a signal and will be restarted.
255 (This can be seen only during a trace.)
256 .SH CONFORMING TO
257 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
258 .SH NOTES
259 .PP
260 Under Linux,
261 .BR fork ()
262 is implemented using copy-on-write pages, so the only penalty that it incurs
263 is the time and memory required to duplicate the parent's page tables,
264 and to create a unique task structure for the child.
265 .SS C library/kernel differences
266 Since version 2.3.3,
267 .\" nptl/sysdeps/unix/sysv/linux/fork.c
268 rather than invoking the kernel's
269 .BR fork ()
270 system call,
271 the glibc
272 .BR fork ()
273 wrapper that is provided as part of the
274 NPTL threading implementation invokes
275 .BR clone (2)
276 with flags that provide the same effect as the traditional system call.
277 (A call to
278 .BR fork ()
279 is equivalent to a call to
280 .BR clone (2)
281 specifying
282 .I flags
283 as just
284 .BR SIGCHLD .)
285 The glibc wrapper invokes any fork handlers that have been
286 established using
287 .BR pthread_atfork (3).
288 .\" and does some magic to ensure that getpid(2) returns the right value.
289 .SH EXAMPLE
290 See
291 .BR pipe (2)
292 and
293 .BR wait (2).
294 .SH SEE ALSO
295 .BR clone (2),
296 .BR execve (2),
297 .BR exit (2),
298 .BR setrlimit (2),
299 .BR unshare (2),
300 .BR vfork (2),
301 .BR wait (2),
302 .BR daemon (3),
303 .BR capabilities (7),
304 .BR credentials (7)