1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl), 1 Nov 1999
2 .\" and Copyright 2006, 2012, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" 1999-11-10: Merged text taken from the page contributed by
27 .\" Reed H. Petty (rhp@draper.net)
29 .TH VFORK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
31 vfork \- create a child process and block parent
34 .B #include <unistd.h>
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
47 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
48 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
49 || /* Glibc <= 2.19: */ _BSD_SOURCE
51 _BSD_SOURCE || _XOPEN_SOURCE >= 500
52 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
55 .SS Standard description
59 function has the same effect as
61 except that the behavior is undefined if the process created by
63 either modifies any data other than a variable of type
65 used to store the return value from
67 or returns from the function in which
69 was called, or calls any other function before successfully calling
78 creates a child process of the calling process.
79 For details and return value and errors, see
85 It is used to create new processes without copying the page tables of
87 It may be useful in performance-sensitive applications
88 where a child is created which then immediately issues an
94 in that the calling thread is suspended until the child terminates
98 or abnormally, after delivery of a fatal signal),
101 Until that point, the child shares all memory with its parent,
103 The child must not return from the current function or call
105 (which would have the effect of calling exit handlers
106 established by the parent process and flushing the parent's
108 buffers), but may call
113 the child process created by
115 inherits copies of various of the caller's process attributes
116 (e.g., file descriptors, signal dispositions, and current working directory);
119 call differs only in the treatment of the virtual address space,
122 Signals sent to the parent
123 arrive after the child releases the parent's memory
124 (i.e., after the child terminates
127 .SS Historic description
130 is implemented using copy-on-write pages, so the only penalty incurred by
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
136 would require making a complete copy of the caller's data space,
137 often needlessly, since usually immediately afterward an
140 Thus, for greater efficiency, BSD introduced the
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
147 The parent process was suspended while the
148 child was using its resources.
151 was tricky: for example, not modifying data
152 in the parent process depended on knowing which variables were
155 4.3BSD; POSIX.1-2001 (but marked OBSOLETE).
156 POSIX.1-2008 removes the specification of
159 The requirements put on
161 by the standards are weaker than those put on
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
167 and cannot rely on any specific behavior with respect to shared memory.
168 .\" In AIXv3.1 vfork is equivalent to fork.
170 Some consider the semantics of
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
175 Users should not depend on the memory sharing semantics of
177 as it will, in that case, be made synonymous to
180 However, even though modern memory management hardware
181 has decreased the performance difference between
185 there are various reasons why Linux and other systems have retained
188 Some performance-critical applications require the small performance
189 advantage conferred by
193 can be implemented on systems that lack a memory-management unit (MMU), but
195 can't be implemented on such systems.
196 (POSIX.1-2008 removed
198 from the standard; the POSIX rationale for the
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
209 On systems where memory is constrained,
211 avoids the need to temporarily commit memory (see the description of
212 .IR /proc/sys/vm/overcommit_memory
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.)
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.
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
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).
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
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
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
258 In this case, attacks are possible where the parent process uses
260 to map in code that will be executed by the privileged child process.
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
268 Fork handlers are called in this case in a program using the
269 LinuxThreads threading library.
272 for a description of Linux threading libraries.)
276 is equivalent to calling
282 CLONE_VM | CLONE_VFORK | SIGCHLD
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
291 but NetBSD introduced it again;
293 .UR http://www.netbsd.org\:/Documentation\:/kernel\:/vfork.html
295 In Linux, it has been equivalent to
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.
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
311 signals; rather, output or
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),
320 .\" and requires a kernel patch.