OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man2 / vfork.2
1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl), 1 Nov 1999
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" 1999-11-10: Merged text taken from the page contributed by
26 .\" Reed H. Petty (rhp@draper.net)
27 .\"
28 .TH VFORK 2 2012-08-05 "Linux" "Linux Programmer's Manual"
29 .SH NAME
30 vfork \- create a child process and block parent
31 .SH SYNOPSIS
32 .B #include <sys/types.h>
33 .br
34 .B #include <unistd.h>
35 .sp
36 .B pid_t vfork(void);
37 .sp
38 .in -4n
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
41 .in
42 .sp
43 .BR vfork ():
44 .ad l
45 .RS 4
46 .PD 0
47 .TP 4
48 Since glibc 2.12:
49 .nf
50 _BSD_SOURCE ||
51     (_XOPEN_SOURCE\ >=\ 500 ||
52         _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
53     !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
54 .TP 4
55 .fi
56 Before glibc 2.12:
57 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
58 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
59 .PD
60 .RE
61 .ad b
62 .SH DESCRIPTION
63 .SS Standard description
64 (From POSIX.1)
65 The
66 .BR vfork ()
67 function has the same effect as
68 .BR fork (2),
69 except that the behavior is undefined if the process created by
70 .BR vfork ()
71 either modifies any data other than a variable of type
72 .I pid_t
73 used to store the return value from
74 .BR vfork (),
75 or returns from the function in which
76 .BR vfork ()
77 was called, or calls any other function before successfully calling
78 .BR _exit (2)
79 or one of the
80 .BR exec (3)
81 family of functions.
82 .SS Linux description
83 .BR vfork (),
84 just like
85 .BR fork (2),
86 creates a child process of the calling process.
87 For details and return value and errors, see
88 .BR fork (2).
89 .PP
90 .BR vfork ()
91 is a special case of
92 .BR clone (2).
93 It is used to create new processes without copying the page tables of
94 the parent process.
95 It may be useful in performance-sensitive applications
96 where a child is created which then immediately issues an
97 .BR execve (2).
98 .PP
99 .BR vfork ()
100 differs from
101 .BR fork (2)
102 in that the calling thread is suspended until the child terminates
103 (either normally,
104 by calling
105 .BR _exit (2),
106 or abnormally, after delivery of a fatal signal),
107 or it makes a call to
108 .BR execve (2).
109 Until that point, the child shares all memory with its parent,
110 including the stack.
111 The child must not return from the current function or call
112 .BR exit (3),
113 but may call
114 .BR _exit (2).
115
116 As with
117 .BR fork (2),
118 the child process created by
119 .BR vfork ()
120 inherits copies of various of the caller's process attributes
121 (e.g., file descriptors, signal dispositions, and current working directory);
122 the
123 .BR vfork ()
124 call differs only in the treatment of the virtual address space,
125 as described above.
126
127 Signals sent to the parent
128 arrive after the child releases the parent's memory
129 (i.e., after the child terminates
130 or calls
131 .BR execve (2)).
132 .SS Historic description
133 Under Linux,
134 .BR fork (2)
135 is implemented using copy-on-write pages, so the only penalty incurred by
136 .BR fork (2)
137 is the time and memory required to duplicate the parent's page tables,
138 and to create a unique task structure for the child.
139 However, in the bad old days a
140 .BR fork (2)
141 would require making a complete copy of the caller's data space,
142 often needlessly, since usually immediately afterward an
143 .BR exec (3)
144 is done.
145 Thus, for greater efficiency, BSD introduced the
146 .BR vfork ()
147 system call, which did not fully copy the address space of
148 the parent process, but borrowed the parent's memory and thread
149 of control until a call to
150 .BR execve (2)
151 or an exit occurred.
152 The parent process was suspended while the
153 child was using its resources.
154 The use of
155 .BR vfork ()
156 was tricky: for example, not modifying data
157 in the parent process depended on knowing which variables were
158 held in a register.
159 .SH CONFORMING TO
160 4.3BSD; POSIX.1-2001 (but marked OBSOLETE).
161 POSIX.1-2008 removes the specification of
162 .BR vfork ().
163
164 The requirements put on
165 .BR vfork ()
166 by the standards are weaker than those put on
167 .BR fork (2),
168 so an implementation where the two are synonymous is compliant.
169 In particular, the programmer cannot rely on the parent
170 remaining blocked until the child either terminates or calls
171 .BR execve (2),
172 and cannot rely on any specific behavior with respect to shared memory.
173 .\" In AIXv3.1 vfork is equivalent to fork.
174 .SH NOTES
175 .PP
176 Some consider the semantics of
177 .BR vfork ()
178 to be an architectural blemish, and the 4.2BSD man page stated:
179 "This system call will be eliminated when proper system sharing mechanisms
180 are implemented.
181 Users should not depend on the memory sharing semantics of
182 .BR vfork ()
183 as it will, in that case, be made synonymous to
184 .BR fork (2).\c
185 "
186 However, even though modern memory management hardware
187 has decreased the performance difference between
188 .BR fork (2)
189 and
190 .BR vfork (),
191 there are various reasons why Linux and other systems have retained
192 .BR vfork ():
193 .IP * 3
194 Some performance-critical applications require the small performance
195 advantage conferred by
196 .BR vfork ().
197 .IP *
198 .BR vfork ()
199 can be implemented on systems that lack a memory-management unit (MMU), but
200 .BR fork (2)
201 can't be implemented on such systems.
202 (POSIX.1-2008 removed
203 .BR vfork ()
204 from the standard; the POSIX rationale for the
205 .BR posix_spawn (3)
206 function notes that that function,
207 which provides functionality equivalent to
208 .BR fork (2)+ exec (3),
209 is designed to be implementable on systems that lack an MMU.)
210 .\" http://stackoverflow.com/questions/4259629/what-is-the-difference-between-fork-and-vfork
211 .\" http://developers.sun.com/solaris/articles/subprocess/subprocess.html
212 .\" http://mailman.uclinux.org/pipermail/uclinux-dev/2009-April/000684.html
213 .SS Linux notes
214 Fork handlers established using
215 .BR pthread_atfork (3)
216 are not called when a multithreaded program employing
217 the NPTL threading library calls
218 .BR vfork ().
219 Fork handlers are called in this case in a program using the
220 LinuxThreads threading library.
221 (See
222 .BR pthreads (7)
223 for a description of Linux threading libraries.)
224
225 A call to
226 .BR vfork ()
227 is equivalent to calling
228 .BR clone (2)
229 with
230 .I flags
231 specified as:
232
233      CLONE_VM | CLONE_VFORK | SIGCHLD
234 .SS History
235 The
236 .BR vfork ()
237 system call appeared in 3.0BSD.
238 .\" In the release notes for 4.2BSD Sam Leffler wrote: `vfork: Is still
239 .\" present, but definitely on its way out'.
240 In 4.4BSD it was made synonymous to
241 .BR fork (2)
242 but NetBSD introduced it again,
243 cf.
244 .UR http://www.netbsd.org\:/Documentation\:/kernel\:/vfork.html
245 .UE .
246 In Linux, it has been equivalent to
247 .BR fork (2)
248 until 2.2.0-pre6 or so.
249 Since 2.2.0-pre9 (on i386, somewhat later on
250 other architectures) it is an independent system call.
251 Support was added in glibc 2.0.112.
252 .SH BUGS
253 .PP
254 Details of the signal handling are obscure and differ between systems.
255 The BSD man page states:
256 "To avoid a possible deadlock situation, processes that are children
257 in the middle of a
258 .BR vfork ()
259 are never sent
260 .B SIGTTOU
261 or
262 .B SIGTTIN
263 signals; rather, output or
264 .IR ioctl s
265 are allowed and input attempts result in an end-of-file indication."
266 .\"
267 .\" As far as I can tell, the following is not true in 2.6.19:
268 .\" Currently (Linux 2.3.25),
269 .\" .BR strace (1)
270 .\" cannot follow
271 .\" .BR vfork ()
272 .\" and requires a kernel patch.
273 .SH SEE ALSO
274 .BR clone (2),
275 .BR execve (2),
276 .BR fork (2),
277 .BR unshare (2),
278 .BR wait (2)
279 .SH COLOPHON
280 This page is part of release 3.68 of the Linux
281 .I man-pages
282 project.
283 A description of the project,
284 information about reporting bugs,
285 and the latest version of this page,
286 can be found at
287 \%http://www.kernel.org/doc/man\-pages/.