1VFORK(2) Linux Programmer's Manual VFORK(2)
2
3
4
6 vfork - create a child process and block parent
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 pid_t vfork(void);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 vfork():
17 Since glibc 2.12:
18 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
19 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
20 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
21 Before glibc 2.12:
22 _BSD_SOURCE || _XOPEN_SOURCE >= 500
23
25 Standard description
26 (From POSIX.1) The vfork() function has the same effect as fork(2), ex‐
27 cept that the behavior is undefined if the process created by vfork()
28 either modifies any data other than a variable of type pid_t used to
29 store the return value from vfork(), or returns from the function in
30 which vfork() was called, or calls any other function before success‐
31 fully calling _exit(2) or one of the exec(3) family of functions.
32
33 Linux description
34 vfork(), just like fork(2), creates a child process of the calling
35 process. For details and return value and errors, see fork(2).
36
37 vfork() is a special case of clone(2). It is used to create new pro‐
38 cesses without copying the page tables of the parent process. It may
39 be useful in performance-sensitive applications where a child is cre‐
40 ated which then immediately issues an execve(2).
41
42 vfork() differs from fork(2) in that the calling thread is suspended
43 until the child terminates (either normally, by calling _exit(2), or
44 abnormally, after delivery of a fatal signal), or it makes a call to
45 execve(2). Until that point, the child shares all memory with its par‐
46 ent, including the stack. The child must not return from the current
47 function or call exit(3) (which would have the effect of calling exit
48 handlers established by the parent process and flushing the parent's
49 stdio(3) buffers), but may call _exit(2).
50
51 As with fork(2), the child process created by vfork() inherits copies
52 of various of the caller's process attributes (e.g., file descriptors,
53 signal dispositions, and current working directory); the vfork() call
54 differs only in the treatment of the virtual address space, as de‐
55 scribed above.
56
57 Signals sent to the parent arrive after the child releases the parent's
58 memory (i.e., after the child terminates or calls execve(2)).
59
60 Historic description
61 Under Linux, fork(2) is implemented using copy-on-write pages, so the
62 only penalty incurred by fork(2) is the time and memory required to du‐
63 plicate the parent's page tables, and to create a unique task structure
64 for the child. However, in the bad old days a fork(2) would require
65 making a complete copy of the caller's data space, often needlessly,
66 since usually immediately afterward an exec(3) is done. Thus, for
67 greater efficiency, BSD introduced the vfork() system call, which did
68 not fully copy the address space of the parent process, but borrowed
69 the parent's memory and thread of control until a call to execve(2) or
70 an exit occurred. The parent process was suspended while the child was
71 using its resources. The use of vfork() was tricky: for example, not
72 modifying data in the parent process depended on knowing which vari‐
73 ables were held in a register.
74
76 4.3BSD; POSIX.1-2001 (but marked OBSOLETE). POSIX.1-2008 removes the
77 specification of vfork().
78
79 The requirements put on vfork() by the standards are weaker than those
80 put on fork(2), so an implementation where the two are synonymous is
81 compliant. In particular, the programmer cannot rely on the parent re‐
82 maining blocked until the child either terminates or calls execve(2),
83 and cannot rely on any specific behavior with respect to shared memory.
84
86 Some consider the semantics of vfork() to be an architectural blemish,
87 and the 4.2BSD man page stated: "This system call will be eliminated
88 when proper system sharing mechanisms are implemented. Users should
89 not depend on the memory sharing semantics of vfork() as it will, in
90 that case, be made synonymous to fork(2)." However, even though modern
91 memory management hardware has decreased the performance difference be‐
92 tween fork(2) and vfork(), there are various reasons why Linux and
93 other systems have retained vfork():
94
95 * Some performance-critical applications require the small performance
96 advantage conferred by vfork().
97
98 * vfork() can be implemented on systems that lack a memory-management
99 unit (MMU), but fork(2) can't be implemented on such systems.
100 (POSIX.1-2008 removed vfork() from the standard; the POSIX rationale
101 for the posix_spawn(3) function notes that that function, which pro‐
102 vides functionality equivalent to fork(2)+exec(3), is designed to be
103 implementable on systems that lack an MMU.)
104
105 * On systems where memory is constrained, vfork() avoids the need to
106 temporarily commit memory (see the description of /proc/sys/vm/over‐
107 commit_memory in proc(5)) in order to execute a new program. (This
108 can be especially beneficial where a large parent process wishes to
109 execute a small helper program in a child process.) By contrast,
110 using fork(2) in this scenario requires either committing an amount
111 of memory equal to the size of the parent process (if strict over‐
112 committing is in force) or overcommitting memory with the risk that
113 a process is terminated by the out-of-memory (OOM) killer.
114
115 Caveats
116 The child process should take care not to modify the memory in unin‐
117 tended ways, since such changes will be seen by the parent process once
118 the child terminates or executes another program. In this regard, sig‐
119 nal handlers can be especially problematic: if a signal handler that is
120 invoked in the child of vfork() changes memory, those changes may re‐
121 sult in an inconsistent process state from the perspective of the par‐
122 ent process (e.g., memory changes would be visible in the parent, but
123 changes to the state of open file descriptors would not be visible).
124
125 When vfork() is called in a multithreaded process, only the calling
126 thread is suspended until the child terminates or executes a new pro‐
127 gram. This means that the child is sharing an address space with other
128 running code. This can be dangerous if another thread in the parent
129 process changes credentials (using setuid(2) or similar), since there
130 are now two processes with different privilege levels running in the
131 same address space. As an example of the dangers, suppose that a mul‐
132 tithreaded program running as root creates a child using vfork(). Af‐
133 ter the vfork(), a thread in the parent process drops the process to an
134 unprivileged user in order to run some untrusted code (e.g., perhaps
135 via plug-in opened with dlopen(3)). In this case, attacks are possible
136 where the parent process uses mmap(2) to map in code that will be exe‐
137 cuted by the privileged child process.
138
139 Linux notes
140 Fork handlers established using pthread_atfork(3) are not called when a
141 multithreaded program employing the NPTL threading library calls
142 vfork(). Fork handlers are called in this case in a program using the
143 LinuxThreads threading library. (See pthreads(7) for a description of
144 Linux threading libraries.)
145
146 A call to vfork() is equivalent to calling clone(2) with flags speci‐
147 fied as:
148
149 CLONE_VM | CLONE_VFORK | SIGCHLD
150
151 History
152 The vfork() system call appeared in 3.0BSD. In 4.4BSD it was made syn‐
153 onymous to fork(2) but NetBSD introduced it again; see
154 ⟨http://www.netbsd.org/Documentation/kernel/vfork.html⟩. In Linux, it
155 has been equivalent to fork(2) until 2.2.0-pre6 or so. Since
156 2.2.0-pre9 (on i386, somewhat later on other architectures) it is an
157 independent system call. Support was added in glibc 2.0.112.
158
160 Details of the signal handling are obscure and differ between systems.
161 The BSD man page states: "To avoid a possible deadlock situation, pro‐
162 cesses that are children in the middle of a vfork() are never sent
163 SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and
164 input attempts result in an end-of-file indication."
165
167 clone(2), execve(2), _exit(2), fork(2), unshare(2), wait(2)
168
170 This page is part of release 5.10 of the Linux man-pages project. A
171 description of the project, information about reporting bugs, and the
172 latest version of this page, can be found at
173 https://www.kernel.org/doc/man-pages/.
174
175
176
177Linux 2017-09-15 VFORK(2)