-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork.html
604 lines (437 loc) · 14.7 KB
/
fork.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:25:43 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>FORK</title>
</head>
<body>
<h1 align="center">FORK</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">fork - create a
child process</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/types.h> <br>
#include <unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>pid_t
fork(void);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>fork</b>()
creates a new process by duplicating the calling process.
The new process is referred to as the <i>child</i> process.
The calling process is referred to as the <i>parent</i>
process.</p>
<p style="margin-left:11%; margin-top: 1em">The child
process and the parent process run in separate memory
spaces. At the time of <b>fork</b>() both memory spaces have
the same content. Memory writes, file mappings
(<b>mmap</b>(2)), and unmappings (<b>munmap</b>(2))
performed by one of the processes do not affect the
other.</p>
<p style="margin-left:11%; margin-top: 1em">The child
process is an exact duplicate of the parent process except
for the following points:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child has its own unique process ID, and this PID
does not match the ID of any existing process group
(<b>setpgid</b>(2)) or session.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child’s parent process ID is the same as the
parent’s process ID.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child does not inherit its parent’s memory
locks (<b>mlock</b>(2), <b>mlockall</b>(2)).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>Process resource utilizations (<b>getrusage</b>(2)) and
CPU time counters (<b>times</b>(2)) are reset to zero in the
child.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child’s set of pending signals is initially
empty (<b>sigpending</b>(2)).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child does not inherit semaphore adjustments from
its parent (<b>semop</b>(2)).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child does not inherit process-associated record
locks from its parent (<b>fcntl</b>(2)). (On the other hand,
it does inherit <b>fcntl</b>(2) open file description locks
and <b>flock</b>(2) locks from its parent.)</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child does not inherit timers from its parent
(<b>setitimer</b>(2), <b>alarm</b>(2),
<b>timer_create</b>(2)).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child does not inherit outstanding asynchronous I/O
operations from its parent (<b>aio_read</b>(3),
<b>aio_write</b>(3)), nor does it inherit any asynchronous
I/O contexts from its parent (see <b>io_setup</b>(2)).</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The process
attributes in the preceding list are all specified in
POSIX.1. The parent and child also differ with respect to
the following Linux-specific process attributes:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p style="margin-top: 1em">*</p></td>
<td width="3%"></td>
<td width="85%">
<p style="margin-top: 1em">The child does not inherit
directory change notifications (dnotify) from its parent
(see the description of <b>F_NOTIFY</b> in
<b>fcntl</b>(2)).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The <b>prctl</b>(2) <b>PR_SET_PDEATHSIG</b> setting is
reset so that the child does not receive a signal when its
parent terminates.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The default timer slack value is set to the
parent’s current timer slack value. See the
description of <b>PR_SET_TIMERSLACK</b> in
<b>prctl</b>(2).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>Memory mappings that have been marked with the
<b>madvise</b>(2) <b>MADV_DONTFORK</b> flag are not
inherited across a <b>fork</b>().</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>Memory in address ranges that have been marked with the
<b>madvise</b>(2) <b>MADV_WIPEONFORK</b> flag is zeroed in
the child after a <b>fork</b>(). (The <b>MADV_WIPEONFORK</b>
setting remains in place for those address ranges in the
child.)</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The termination signal of the child is always
<b>SIGCHLD</b> (see <b>clone</b>(2)).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The port access permission bits set by <b>ioperm</b>(2)
are not inherited by the child; the child must turn on any
bits that it requires using <b>ioperm</b>(2).</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Note the
following further points:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p style="margin-top: 1em">*</p></td>
<td width="3%"></td>
<td width="85%">
<p style="margin-top: 1em">The child process is created
with a single thread—the one that called
<b>fork</b>(). The entire virtual address space of the
parent is replicated in the child, including the states of
mutexes, condition variables, and other pthreads objects;
the use of <b>pthread_atfork</b>(3) may be helpful for
dealing with problems that this can cause.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>After a <b>fork</b>() in a multithreaded program, the
child can safely call only async-signal-safe functions (see
<b>signal-safety</b>(7)) until such time as it calls
<b>execve</b>(2).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child inherits copies of the parent’s set of
open file descriptors. Each file descriptor in the child
refers to the same open file description (see
<b>open</b>(2)) as the corresponding file descriptor in the
parent. This means that the two file descriptors share open
file status flags, file offset, and signal-driven I/O
attributes (see the description of <b>F_SETOWN</b> and
<b>F_SETSIG</b> in <b>fcntl</b>(2)).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child inherits copies of the parent’s set of
open message queue descriptors (see <b>mq_overview</b>(7)).
Each file descriptor in the child refers to the same open
message queue description as the corresponding file
descriptor in the parent. This means that the two file
descriptors share the same flags (<i>mq_flags</i>).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>The child inherits copies of the parent’s set of
open directory streams (see <b>opendir</b>(3)). POSIX.1 says
that the corresponding directory streams in the parent and
child <i>may</i> share the directory stream positioning; on
Linux/glibc they do not.</p></td></tr>
</table>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success, the
PID of the child process is returned in the parent, and 0 is
returned in the child. On failure, -1 is returned in the
parent, no child process is created, and <i>errno</i> is set
appropriately.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EAGAIN</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">A system-imposed limit on the
number of threads was encountered. There are a number of
limits that may trigger this error:</p></td></tr>
</table>
<p style="margin-left:22%;">*</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="12%"></td>
<td width="3%"></td>
<td width="74%">
<p style="margin-top: 1em">the <b>RLIMIT_NPROC</b> soft
resource limit (set via <b>setrlimit</b>(2)), which limits
the number of processes and threads for a real user ID, was
reached;</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="12%">
<p>*</p></td>
<td width="3%"></td>
<td width="74%">
<p>the kernel’s system-wide limit on the number of
processes and threads, <i>/proc/sys/kernel/threads-max</i>,
was reached (see <b>proc</b>(5));</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="12%">
<p>*</p></td>
<td width="3%"></td>
<td width="74%">
<p>the maximum number of PIDs,
<i>/proc/sys/kernel/pid_max</i>, was reached (see
<b>proc</b>(5)); or</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="12%">
<p>*</p></td>
<td width="3%"></td>
<td width="74%">
<p>the PID limit (<i>pids.max</i>) imposed by the cgroup
"process number" (PIDs) controller was
reached.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="12%">
<p><b>EAGAIN</b></p></td>
<td width="3%"></td>
<td width="74%">
</td></tr>
</table>
<p style="margin-left:22%; margin-top: 1em">The caller is
operating under the <b>SCHED_DEADLINE</b> scheduling policy
and does not have the reset-on-fork flag set. See
<b>sched</b>(7).</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em"><b>fork</b>() failed to allocate
the necessary kernel structures because memory is tight.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An attempt was made to create a child process in a PID
namespace whose "init" process has terminated. See
<b>pid_namespaces</b>(7).</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOSYS</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><b>fork</b>() is not supported on this platform (for
example, hardware without a Memory-Management Unit).</p></td></tr>
</table>
<p style="margin-left:11%;"><b>ERESTARTNOINTR</b> (since
Linux 2.6.17)</p>
<p style="margin-left:22%;">System call was interrupted by
a signal and will be restarted. (This can be seen only
during a trace.)</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2001,
POSIX.1-2008, SVr4, 4.3BSD.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Under Linux,
<b>fork</b>() is implemented using copy-on-write pages, so
the only penalty that it incurs is the time and memory
required to duplicate the parent’s page tables, and to
create a unique task structure for the child.</p>
<p style="margin-left:11%; margin-top: 1em"><b>C
library/kernel differences</b> <br>
Since version 2.3.3, rather than invoking the kernel’s
<b>fork</b>() system call, the glibc <b>fork</b>() wrapper
that is provided as part of the NPTL threading
implementation invokes <b>clone</b>(2) with flags that
provide the same effect as the traditional system call. (A
call to <b>fork</b>() is equivalent to a call to
<b>clone</b>(2) specifying <i>flags</i> as just
<b>SIGCHLD</b>.) The glibc wrapper invokes any fork handlers
that have been established using
<b>pthread_atfork</b>(3).</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">See
<b>pipe</b>(2) and <b>wait</b>(2).</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>clone</b>(2),
<b>execve</b>(2), <b>exit</b>(2), <b>setrlimit</b>(2),
<b>unshare</b>(2), <b>vfork</b>(2), <b>wait</b>(2),
<b>daemon</b>(3), <b>pthread_atfork</b>(3),
<b>capabilities</b>(7), <b>credentials</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>