OSDN Git Service

00d19670d80fcf582afc7fd140903f18d6a43a6d
[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-12 "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 The default timer slack value is set to the parent's
109 current timer slack value.
110 See the description of
111 .BR PR_SET_TIMERSLACK
112 in
113 .BR prctl (2).
114 .IP *
115 Memory mappings that have been marked with the
116 .BR madvise (2)
117 .B MADV_DONTFORK
118 flag are not inherited across a
119 .BR fork ().
120 .IP *
121 The termination signal of the child is always
122 .B SIGCHLD
123 (see
124 .BR clone (2)).
125 .IP *
126 The port access permission bits set by
127 .BR ioperm (2)
128 are not inherited by the child;
129 the child must turn on any bits that it requires using
130 .BR ioperm (2).
131 .PP
132 Note the following further points:
133 .IP * 3
134 The child process is created with a single thread\(emthe
135 one that called
136 .BR fork ().
137 The entire virtual address space of the parent is replicated in the child,
138 including the states of mutexes, condition variables,
139 and other pthreads objects; the use of
140 .BR pthread_atfork (3)
141 may be helpful for dealing with problems that this can cause.
142 .IP *
143 The child inherits copies of the parent's set of open file descriptors.
144 Each file descriptor in the child refers to the same
145 open file description (see
146 .BR open (2))
147 as the corresponding file descriptor in the parent.
148 This means that the two descriptors share open file status flags,
149 current file offset,
150 and signal-driven I/O attributes (see the description of
151 .B F_SETOWN
152 and
153 .B F_SETSIG
154 in
155 .BR fcntl (2)).
156 .IP *
157 The child inherits copies of the parent's set of open message
158 queue descriptors (see
159 .BR mq_overview (7)).
160 Each descriptor in the child refers to the same
161 open message queue description
162 as the corresponding descriptor in the parent.
163 This means that the two descriptors share the same flags
164 .RI ( mq_flags ).
165 .IP *
166 The child inherits copies of the parent's set of open directory streams (see
167 .BR opendir (3)).
168 POSIX.1-2001 says that the corresponding directory streams
169 in the parent and child
170 .I may
171 share the directory stream positioning;
172 on Linux/glibc they do not.
173 .SH RETURN VALUE
174 On success, the PID of the child process is returned in the parent,
175 and 0 is returned in the child.
176 On failure, \-1 is returned in the parent,
177 no child process is created, and
178 .I errno
179 is set appropriately.
180 .SH ERRORS
181 .TP
182 .B EAGAIN
183 .BR fork ()
184 cannot allocate sufficient memory to copy the parent's page tables and
185 allocate a task structure for the child.
186 .TP
187 .B EAGAIN
188 It was not possible to create a new process because the caller's
189 .B RLIMIT_NPROC
190 resource limit was encountered.
191 To exceed this limit, the process must have either the
192 .B CAP_SYS_ADMIN
193 or the
194 .B CAP_SYS_RESOURCE
195 capability.
196 .TP
197 .B EAGAIN
198 The caller is operating under the
199 .BR SCHED_DEADLINE
200 scheduling policy and does not have the reset-on-fork flag set.
201 See
202 .BR sched (7).
203 .TP
204 .B ENOMEM
205 .BR fork ()
206 failed to allocate the necessary kernel structures because memory is tight.
207 .TP
208 .B ENOSYS
209 .BR fork ()
210 is not supported on this platform (for example,
211 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
212 hardware without a Memory-Management Unit).
213 .SH CONFORMING TO
214 SVr4, 4.3BSD, POSIX.1-2001.
215 .SH NOTES
216 .PP
217 Under Linux,
218 .BR fork ()
219 is implemented using copy-on-write pages, so the only penalty that it incurs
220 is the time and memory required to duplicate the parent's page tables,
221 and to create a unique task structure for the child.
222
223 Since version 2.3.3,
224 .\" nptl/sysdeps/unix/sysv/linux/fork.c
225 rather than invoking the kernel's
226 .BR fork ()
227 system call,
228 the glibc
229 .BR fork ()
230 wrapper that is provided as part of the
231 NPTL threading implementation invokes
232 .BR clone (2)
233 with flags that provide the same effect as the traditional system call.
234 (A call to
235 .BR fork ()
236 is equivalent to a call to
237 .BR clone (2)
238 specifying
239 .I flags
240 as just
241 .BR SIGCHLD .)
242 The glibc wrapper invokes any fork handlers that have been
243 established using
244 .BR pthread_atfork (3).
245 .\" and does some magic to ensure that getpid(2) returns the right value.
246 .SH EXAMPLE
247 See
248 .BR pipe (2)
249 and
250 .BR wait (2).
251 .SH SEE ALSO
252 .BR clone (2),
253 .BR execve (2),
254 .BR exit (2),
255 .BR setrlimit (2),
256 .BR unshare (2),
257 .BR vfork (2),
258 .BR wait (2),
259 .BR daemon (3),
260 .BR capabilities (7),
261 .BR credentials (7)
262 .SH COLOPHON
263 This page is part of release 3.67 of the Linux
264 .I man-pages
265 project.
266 A description of the project,
267 information about reporting bugs,
268 and the latest version of this page,
269 can be found at
270 \%http://www.kernel.org/doc/man\-pages/.