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