OSDN Git Service

2bd0701a27078d8439e884794a66740dcea721e6
[linuxjm/LDP_man-pages.git] / original / 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 2014-05-28 "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 process-associated record locks from its parent
76 .RB ( fcntl (2)).
77 (On the other hand, it does inherit
78 .BR fcntl (2)
79 open file description locks and
80 .BR flock (2)
81 locks from its parent.)
82 .IP *
83 The child does not inherit timers from its parent
84 .RB ( setitimer (2),
85 .BR alarm (2),
86 .BR timer_create (2)).
87 .IP *
88 The child does not inherit outstanding asynchronous I/O operations
89 from its parent
90 .RB ( aio_read (3),
91 .BR aio_write (3)),
92 nor does it inherit any asynchronous I/O contexts from its parent (see
93 .BR io_setup (2)).
94 .PP
95 The process attributes in the preceding list are all specified
96 in POSIX.1-2001.
97 The parent and child also differ with respect to the following
98 Linux-specific process attributes:
99 .IP * 3
100 The child does not inherit directory change notifications (dnotify)
101 from its parent
102 (see the description of
103 .B F_NOTIFY
104 in
105 .BR fcntl (2)).
106 .IP *
107 The
108 .BR prctl (2)
109 .B PR_SET_PDEATHSIG
110 setting is reset so that the child does not receive a signal
111 when its parent terminates.
112 .IP *
113 The default timer slack value is set to the parent's
114 current timer slack value.
115 See the description of
116 .BR PR_SET_TIMERSLACK
117 in
118 .BR prctl (2).
119 .IP *
120 Memory mappings that have been marked with the
121 .BR madvise (2)
122 .B MADV_DONTFORK
123 flag are not inherited across a
124 .BR fork ().
125 .IP *
126 The termination signal of the child is always
127 .B SIGCHLD
128 (see
129 .BR clone (2)).
130 .IP *
131 The port access permission bits set by
132 .BR ioperm (2)
133 are not inherited by the child;
134 the child must turn on any bits that it requires using
135 .BR ioperm (2).
136 .PP
137 Note the following further points:
138 .IP * 3
139 The child process is created with a single thread\(emthe
140 one that called
141 .BR fork ().
142 The entire virtual address space of the parent is replicated in the child,
143 including the states of mutexes, condition variables,
144 and other pthreads objects; the use of
145 .BR pthread_atfork (3)
146 may be helpful for dealing with problems that this can cause.
147 .IP *
148 The child inherits copies of the parent's set of open file descriptors.
149 Each file descriptor in the child refers to the same
150 open file description (see
151 .BR open (2))
152 as the corresponding file descriptor in the parent.
153 This means that the two descriptors share open file status flags,
154 current file offset,
155 and signal-driven I/O attributes (see the description of
156 .B F_SETOWN
157 and
158 .B F_SETSIG
159 in
160 .BR fcntl (2)).
161 .IP *
162 The child inherits copies of the parent's set of open message
163 queue descriptors (see
164 .BR mq_overview (7)).
165 Each descriptor in the child refers to the same
166 open message queue description
167 as the corresponding descriptor in the parent.
168 This means that the two descriptors share the same flags
169 .RI ( mq_flags ).
170 .IP *
171 The child inherits copies of the parent's set of open directory streams (see
172 .BR opendir (3)).
173 POSIX.1-2001 says that the corresponding directory streams
174 in the parent and child
175 .I may
176 share the directory stream positioning;
177 on Linux/glibc they do not.
178 .SH RETURN VALUE
179 On success, the PID of the child process is returned in the parent,
180 and 0 is returned in the child.
181 On failure, \-1 is returned in the parent,
182 no child process is created, and
183 .I errno
184 is set appropriately.
185 .SH ERRORS
186 .TP
187 .B EAGAIN
188 .BR fork ()
189 cannot allocate sufficient memory to copy the parent's page tables and
190 allocate a task structure for the child.
191 .TP
192 .B EAGAIN
193 \" NOTE! The following should match the description in pthread_create(3)
194 A system-imposed limit on the number of threads was encountered.
195 There are a number of limits that may trigger this error: the
196 .BR RLIMIT_NPROC
197 soft resource limit (set via
198 .BR setrlimit (2)),
199 which limits the number of processes and threads for a real user ID,
200 was reached;
201 the kernel's system-wide limit on the number of processes and threads,
202 .IR /proc/sys/kernel/threads-max ,
203 was reached (see
204 .BR proc (5));
205 or the maximum number of PIDs,
206 .IR /proc/sys/kernel/pid_max ,
207 was reached (see
208 .BR proc (5)).
209 .TP
210 .B EAGAIN
211 The caller is operating under the
212 .BR SCHED_DEADLINE
213 scheduling policy and does not have the reset-on-fork flag set.
214 See
215 .BR sched (7).
216 .TP
217 .B ENOMEM
218 .BR fork ()
219 failed to allocate the necessary kernel structures because memory is tight.
220 .TP
221 .B ENOSYS
222 .BR fork ()
223 is not supported on this platform (for example,
224 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
225 hardware without a Memory-Management Unit).
226 .SH CONFORMING TO
227 SVr4, 4.3BSD, POSIX.1-2001.
228 .SH NOTES
229 .PP
230 Under Linux,
231 .BR fork ()
232 is implemented using copy-on-write pages, so the only penalty that it incurs
233 is the time and memory required to duplicate the parent's page tables,
234 and to create a unique task structure for the child.
235
236 Since version 2.3.3,
237 .\" nptl/sysdeps/unix/sysv/linux/fork.c
238 rather than invoking the kernel's
239 .BR fork ()
240 system call,
241 the glibc
242 .BR fork ()
243 wrapper that is provided as part of the
244 NPTL threading implementation invokes
245 .BR clone (2)
246 with flags that provide the same effect as the traditional system call.
247 (A call to
248 .BR fork ()
249 is equivalent to a call to
250 .BR clone (2)
251 specifying
252 .I flags
253 as just
254 .BR SIGCHLD .)
255 The glibc wrapper invokes any fork handlers that have been
256 established using
257 .BR pthread_atfork (3).
258 .\" and does some magic to ensure that getpid(2) returns the right value.
259 .SH EXAMPLE
260 See
261 .BR pipe (2)
262 and
263 .BR wait (2).
264 .SH SEE ALSO
265 .BR clone (2),
266 .BR execve (2),
267 .BR exit (2),
268 .BR setrlimit (2),
269 .BR unshare (2),
270 .BR vfork (2),
271 .BR wait (2),
272 .BR daemon (3),
273 .BR capabilities (7),
274 .BR credentials (7)
275 .SH COLOPHON
276 This page is part of release 3.78 of the Linux
277 .I man-pages
278 project.
279 A description of the project,
280 information about reporting bugs,
281 and the latest version of this page,
282 can be found at
283 \%http://www.kernel.org/doc/man\-pages/.