OSDN Git Service

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