OSDN Git Service

966bb5cf2c30ee4cba79da225321e71a5e576950
[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 2013-03-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 ENOMEM
198 .BR fork ()
199 failed to allocate the necessary kernel structures because memory is tight.
200 .TP
201 .B ENOSYS
202 .BR fork ()
203 is not supported on this platform (for example,
204 .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa
205 hardware without a Memory-Management Unit).
206 .SH CONFORMING TO
207 SVr4, 4.3BSD, POSIX.1-2001.
208 .SH NOTES
209 .PP
210 Under Linux,
211 .BR fork ()
212 is implemented using copy-on-write pages, so the only penalty that it incurs
213 is the time and memory required to duplicate the parent's page tables,
214 and to create a unique task structure for the child.
215
216 Since version 2.3.3,
217 .\" nptl/sysdeps/unix/sysv/linux/fork.c
218 rather than invoking the kernel's
219 .BR fork ()
220 system call,
221 the glibc
222 .BR fork ()
223 wrapper that is provided as part of the
224 NPTL threading implementation invokes
225 .BR clone (2)
226 with flags that provide the same effect as the traditional system call.
227 (A call to
228 .BR fork ()
229 is equivalent to a call to
230 .BR clone (2)
231 specifying
232 .I flags
233 as just
234 .BR SIGCHLD .)
235 The glibc wrapper invokes any fork handlers that have been
236 established using
237 .BR pthread_atfork (3).
238 .\" and does some magic to ensure that getpid(2) returns the right value.
239 .SH EXAMPLE
240 See
241 .BR pipe (2)
242 and
243 .BR wait (2).
244 .SH SEE ALSO
245 .BR clone (2),
246 .BR execve (2),
247 .BR exit (2),
248 .BR setrlimit (2),
249 .BR unshare (2),
250 .BR vfork (2),
251 .BR wait (2),
252 .BR daemon (3),
253 .BR capabilities (7),
254 .BR credentials (7)
255 .SH COLOPHON
256 This page is part of release 3.64 of the Linux
257 .I man-pages
258 project.
259 A description of the project,
260 and information about reporting bugs,
261 can be found at
262 \%http://www.kernel.org/doc/man\-pages/.