]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fork.2
intro.1, fork.2, futex.2, open.2, rename.2, select_tut.2, semop.2, spu_create.2,...
[thirdparty/man-pages.git] / man2 / fork.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" A few fragments remain from an earlier (1992) page by
5 .\" Drew Eckhardt (drew@cs.colorado.edu),
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
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 2009-04-27 "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, referred to as the \fIchild\fP,
50 is an exact duplicate of the calling process,
51 referred to as the \fIparent\fP, except for the following points:
52 .IP * 3
53 The child has its own unique process ID,
54 and this PID does not match the ID of any existing process group
55 .RB ( setpgid (2)).
56 .IP *
57 The child's parent process ID is the same as the parent's process ID.
58 .IP *
59 The child does not inherit its parent's memory locks
60 .RB ( mlock (2),
61 .BR mlockall (2)).
62 .IP *
63 Process resource utilizations
64 .RB ( getrusage (2))
65 and CPU time counters
66 .RB ( times (2))
67 are reset to zero in the child.
68 .IP *
69 The child's set of pending signals is initially empty
70 .RB ( sigpending (2)).
71 .IP *
72 The child does not inherit semaphore adjustments from its parent
73 .RB ( semop (2)).
74 .IP *
75 The child does not inherit record locks from its parent
76 .RB ( fcntl (2)).
77 .IP *
78 The child does not inherit timers from its parent
79 .RB ( setitimer (2),
80 .BR alarm (2),
81 .BR timer_create (2)).
82 .IP *
83 The child does not inherit outstanding asynchronous I/O operations
84 from its parent
85 .RB ( aio_read (3),
86 .BR aio_write (3)),
87 nor does it inherit any asynchronous I/O contexts from its parent (see
88 .BR io_setup (2)).
89 .PP
90 The process attributes in the preceding list are all specified
91 in POSIX.1-2001.
92 The parent and child also differ with respect to the following
93 Linux-specific process attributes:
94 .IP * 3
95 The child does not inherit directory change notifications (dnotify)
96 from its parent
97 (see the description of
98 .B F_NOTIFY
99 in
100 .BR fcntl (2)).
101 .IP *
102 The
103 .BR prctl (2)
104 .B PR_SET_PDEATHSIG
105 setting is reset so that the child does not receive a signal
106 when its parent terminates.
107 .IP *
108 Memory mappings that have been marked with the
109 .BR madvise (2)
110 .B MADV_DONTFORK
111 flag are not inherited across a
112 .BR fork ().
113 .IP *
114 The termination signal of the child is always
115 .B SIGCHLD
116 (see
117 .BR clone (2)).
118 .PP
119 Note the following further points:
120 .IP * 3
121 The child process is created with a single thread\(emthe
122 one that called
123 .BR fork ().
124 The entire virtual address space of the parent is replicated in the child,
125 including the states of mutexes, condition variables,
126 and other pthreads objects; the use of
127 .BR pthread_atfork (3)
128 may be helpful for dealing with problems that this can cause.
129 .IP *
130 The child inherits copies of the parent's set of open file descriptors.
131 Each file descriptor in the child refers to the same
132 open file description (see
133 .BR open (2))
134 as the corresponding file descriptor in the parent.
135 This means that the two descriptors share open file status flags,
136 current file offset,
137 and signal-driven I/O attributes (see the description of
138 .B F_SETOWN
139 and
140 .B F_SETSIG
141 in
142 .BR fcntl (2)).
143 .IP *
144 The child inherits copies of the parent's set of open message
145 queue descriptors (see
146 .BR mq_overview (7)).
147 Each descriptor in the child refers to the same
148 open message queue description
149 as the corresponding descriptor in the parent.
150 This means that the two descriptors share the same flags
151 .RI ( mq_flags ).
152 .IP *
153 The child inherits copies of the parent's set of open directory streams (see
154 .BR opendir (3)).
155 POSIX.1-2001 says that the corresponding directory streams
156 in the parent and child
157 .I may
158 share the directory stream positioning;
159 on Linux/glibc they do not.
160 .SH "RETURN VALUE"
161 On success, the PID of the child process is returned in the parent,
162 and 0 is returned in the child.
163 On failure, \-1 is returned in the parent,
164 no child process is created, and
165 .I errno
166 is set appropriately.
167 .SH ERRORS
168 .TP
169 .B EAGAIN
170 .BR fork ()
171 cannot allocate sufficient memory to copy the parent's page tables and
172 allocate a task structure for the child.
173 .TP
174 .B EAGAIN
175 It was not possible to create a new process because the caller's
176 .B RLIMIT_NPROC
177 resource limit was encountered.
178 To exceed this limit, the process must have either the
179 .B CAP_SYS_ADMIN
180 or the
181 .B CAP_SYS_RESOURCE
182 capability.
183 .TP
184 .B ENOMEM
185 .BR fork ()
186 failed to allocate the necessary kernel structures because memory is tight.
187 .SH "CONFORMING TO"
188 SVr4, 4.3BSD, POSIX.1-2001.
189 .SH NOTES
190 .PP
191 Under Linux,
192 .BR fork ()
193 is implemented using copy-on-write pages, so the only penalty that it incurs
194 is the time and memory required to duplicate the parent's page tables,
195 and to create a unique task structure for the child.
196
197 Since version 2.3.3,
198 .\" nptl/sysdeps/unix/sysv/linux/fork.c
199 rather than invoking the kernel's
200 .BR fork ()
201 system call,
202 the glibc
203 .BR fork ()
204 wrapper that is provided as part of the
205 NPTL threading implementation invokes
206 .BR clone (2)
207 with flags that provide the same effect as the traditional system call.
208 The glibc wrapper invokes any fork handlers that have been
209 established using
210 .BR pthread_atfork (3).
211 .\" and does some magic to ensure that getpid(2) returns the right value.
212 .SH EXAMPLE
213 See
214 .BR pipe (2)
215 and
216 .BR wait (2).
217 .SH "SEE ALSO"
218 .BR clone (2),
219 .BR execve (2),
220 .BR setrlimit (2),
221 .BR unshare (2),
222 .BR vfork (2),
223 .BR wait (2),
224 .BR daemon (3),
225 .BR capabilities (7),
226 .BR credentials (7)