]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/vfork.2
sock_diag.7: Tweaks to Dmitry Levin's page
[thirdparty/man-pages.git] / 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 2016-03-15 "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 (_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L)
51 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
52 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
53 .TP 4
54 .fi
55 Before glibc 2.12:
56 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
57 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
58 .PD
59 .RE
60 .ad b
61 .SH DESCRIPTION
62 .SS Standard description
63 (From POSIX.1)
64 The
65 .BR vfork ()
66 function has the same effect as
67 .BR fork (2),
68 except that the behavior is undefined if the process created by
69 .BR vfork ()
70 either modifies any data other than a variable of type
71 .I pid_t
72 used to store the return value from
73 .BR vfork (),
74 or returns from the function in which
75 .BR vfork ()
76 was called, or calls any other function before successfully calling
77 .BR _exit (2)
78 or one of the
79 .BR exec (3)
80 family of functions.
81 .SS Linux description
82 .BR vfork (),
83 just like
84 .BR fork (2),
85 creates a child process of the calling process.
86 For details and return value and errors, see
87 .BR fork (2).
88 .PP
89 .BR vfork ()
90 is a special case of
91 .BR clone (2).
92 It is used to create new processes without copying the page tables of
93 the parent process.
94 It may be useful in performance-sensitive applications
95 where a child is created which then immediately issues an
96 .BR execve (2).
97 .PP
98 .BR vfork ()
99 differs from
100 .BR fork (2)
101 in that the calling thread is suspended until the child terminates
102 (either normally,
103 by calling
104 .BR _exit (2),
105 or abnormally, after delivery of a fatal signal),
106 or it makes a call to
107 .BR execve (2).
108 Until that point, the child shares all memory with its parent,
109 including the stack.
110 The child must not return from the current function or call
111 .BR exit (3),
112 but may call
113 .BR _exit (2).
114
115 As with
116 .BR fork (2),
117 the child process created by
118 .BR vfork ()
119 inherits copies of various of the caller's process attributes
120 (e.g., file descriptors, signal dispositions, and current working directory);
121 the
122 .BR vfork ()
123 call differs only in the treatment of the virtual address space,
124 as described above.
125
126 Signals sent to the parent
127 arrive after the child releases the parent's memory
128 (i.e., after the child terminates
129 or calls
130 .BR execve (2)).
131 .SS Historic description
132 Under Linux,
133 .BR fork (2)
134 is implemented using copy-on-write pages, so the only penalty incurred by
135 .BR fork (2)
136 is the time and memory required to duplicate the parent's page tables,
137 and to create a unique task structure for the child.
138 However, in the bad old days a
139 .BR fork (2)
140 would require making a complete copy of the caller's data space,
141 often needlessly, since usually immediately afterward an
142 .BR exec (3)
143 is done.
144 Thus, for greater efficiency, BSD introduced the
145 .BR vfork ()
146 system call, which did not fully copy the address space of
147 the parent process, but borrowed the parent's memory and thread
148 of control until a call to
149 .BR execve (2)
150 or an exit occurred.
151 The parent process was suspended while the
152 child was using its resources.
153 The use of
154 .BR vfork ()
155 was tricky: for example, not modifying data
156 in the parent process depended on knowing which variables were
157 held in a register.
158 .SH CONFORMING TO
159 4.3BSD; POSIX.1-2001 (but marked OBSOLETE).
160 POSIX.1-2008 removes the specification of
161 .BR vfork ().
162
163 The requirements put on
164 .BR vfork ()
165 by the standards are weaker than those put on
166 .BR fork (2),
167 so an implementation where the two are synonymous is compliant.
168 In particular, the programmer cannot rely on the parent
169 remaining blocked until the child either terminates or calls
170 .BR execve (2),
171 and cannot rely on any specific behavior with respect to shared memory.
172 .\" In AIXv3.1 vfork is equivalent to fork.
173 .SH NOTES
174 .PP
175 Some consider the semantics of
176 .BR vfork ()
177 to be an architectural blemish, and the 4.2BSD man page stated:
178 "This system call will be eliminated when proper system sharing mechanisms
179 are implemented.
180 Users should not depend on the memory sharing semantics of
181 .BR vfork ()
182 as it will, in that case, be made synonymous to
183 .BR fork (2).\c
184 "
185 However, even though modern memory management hardware
186 has decreased the performance difference between
187 .BR fork (2)
188 and
189 .BR vfork (),
190 there are various reasons why Linux and other systems have retained
191 .BR vfork ():
192 .IP * 3
193 Some performance-critical applications require the small performance
194 advantage conferred by
195 .BR vfork ().
196 .IP *
197 .BR vfork ()
198 can be implemented on systems that lack a memory-management unit (MMU), but
199 .BR fork (2)
200 can't be implemented on such systems.
201 (POSIX.1-2008 removed
202 .BR vfork ()
203 from the standard; the POSIX rationale for the
204 .BR posix_spawn (3)
205 function notes that that function,
206 which provides functionality equivalent to
207 .BR fork (2)+ exec (3),
208 is designed to be implementable on systems that lack an MMU.)
209 .\" http://stackoverflow.com/questions/4259629/what-is-the-difference-between-fork-and-vfork
210 .\" http://developers.sun.com/solaris/articles/subprocess/subprocess.html
211 .\" http://mailman.uclinux.org/pipermail/uclinux-dev/2009-April/000684.html
212 .SS Linux notes
213 Fork handlers established using
214 .BR pthread_atfork (3)
215 are not called when a multithreaded program employing
216 the NPTL threading library calls
217 .BR vfork ().
218 Fork handlers are called in this case in a program using the
219 LinuxThreads threading library.
220 (See
221 .BR pthreads (7)
222 for a description of Linux threading libraries.)
223
224 A call to
225 .BR vfork ()
226 is equivalent to calling
227 .BR clone (2)
228 with
229 .I flags
230 specified as:
231
232 CLONE_VM | CLONE_VFORK | SIGCHLD
233 .SS History
234 The
235 .BR vfork ()
236 system call appeared in 3.0BSD.
237 .\" In the release notes for 4.2BSD Sam Leffler wrote: `vfork: Is still
238 .\" present, but definitely on its way out'.
239 In 4.4BSD it was made synonymous to
240 .BR fork (2)
241 but NetBSD introduced it again,
242 cf.
243 .UR http://www.netbsd.org\:/Documentation\:/kernel\:/vfork.html
244 .UE .
245 In Linux, it has been equivalent to
246 .BR fork (2)
247 until 2.2.0-pre6 or so.
248 Since 2.2.0-pre9 (on i386, somewhat later on
249 other architectures) it is an independent system call.
250 Support was added in glibc 2.0.112.
251 .SH BUGS
252 .PP
253 Details of the signal handling are obscure and differ between systems.
254 The BSD man page states:
255 "To avoid a possible deadlock situation, processes that are children
256 in the middle of a
257 .BR vfork ()
258 are never sent
259 .B SIGTTOU
260 or
261 .B SIGTTIN
262 signals; rather, output or
263 .IR ioctl s
264 are allowed and input attempts result in an end-of-file indication."
265 .\"
266 .\" As far as I can tell, the following is not true in 2.6.19:
267 .\" Currently (Linux 2.3.25),
268 .\" .BR strace (1)
269 .\" cannot follow
270 .\" .BR vfork ()
271 .\" and requires a kernel patch.
272 .SH SEE ALSO
273 .BR clone (2),
274 .BR execve (2),
275 .BR fork (2),
276 .BR unshare (2),
277 .BR wait (2)