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