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