]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/vfork.2
brk.2, getdtablesize.2, getpagesize.2, vfork.2, getpass.3, ualarm.3, usleep.3: Update...
[thirdparty/man-pages.git] / 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 2009-06-21 "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 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
56 .PD
57 .RE
58 .ad b
59 .SH DESCRIPTION
60 .SS "Standard Description"
61 (From POSIX.1)
62 The
63 .BR vfork ()
64 function has the same effect as
65 .BR fork (2),
66 except that the behavior is undefined if the process created by
67 .BR vfork ()
68 either modifies any data other than a variable of type
69 .I pid_t
70 used to store the return value from
71 .BR vfork (),
72 or returns from the function in which
73 .BR vfork ()
74 was called, or calls any other function before successfully calling
75 .BR _exit (2)
76 or one of the
77 .BR exec (3)
78 family of functions.
79 .SS "Linux Description"
80 .BR vfork (),
81 just like
82 .BR fork (2),
83 creates a child process of the calling process.
84 For details and return value and errors, see
85 .BR fork (2).
86 .PP
87 .BR vfork ()
88 is a special case of
89 .BR clone (2).
90 It is used to create new processes without copying the page tables of
91 the parent process.
92 It may be useful in performance-sensitive applications
93 where a child is created which then immediately issues an
94 .BR execve (2).
95 .PP
96 .BR vfork ()
97 differs from
98 .BR fork (2)
99 in that the parent is suspended until the child terminates
100 (either normally,
101 by calling
102 .BR _exit (2),
103 or abnormally, after delivery of a fatal signal),
104 or it makes a call to
105 .BR execve (2).
106 Until that point, the child shares all memory with its parent,
107 including the stack.
108 The child must not return from the current function or call
109 .BR exit (3),
110 but may call
111 .BR _exit (2).
112 .PP
113 Signal handlers are inherited, but not shared.
114 Signals to the parent
115 arrive after the child releases the parent's memory
116 (i.e., after the child terminates
117 or calls
118 .BR execve (2)).
119 .SS "Historic Description"
120 Under Linux,
121 .BR fork (2)
122 is implemented using copy-on-write pages, so the only penalty incurred by
123 .BR fork (2)
124 is the time and memory required to duplicate the parent's page tables,
125 and to create a unique task structure for the child.
126 However, in the bad old days a
127 .BR fork (2)
128 would require making a complete copy of the caller's data space,
129 often needlessly, since usually immediately afterwards an
130 .BR exec (3)
131 is done.
132 Thus, for greater efficiency, BSD introduced the
133 .BR vfork ()
134 system call, which did not fully copy the address space of
135 the parent process, but borrowed the parent's memory and thread
136 of control until a call to
137 .BR execve (2)
138 or an exit occurred.
139 The parent process was suspended while the
140 child was using its resources.
141 The use of
142 .BR vfork ()
143 was tricky: for example, not modifying data
144 in the parent process depended on knowing which variables were
145 held in a register.
146 .SH "CONFORMING TO"
147 4.3BSD, POSIX.1-2001.
148 POSIX.1-2008 removes the specification of
149 .BR vfork ().
150 The requirements put on
151 .BR vfork ()
152 by the standards are weaker than those put on
153 .BR fork (2),
154 so an implementation where the two are synonymous is compliant.
155 In particular, the programmer cannot rely on the parent
156 remaining blocked until the child either terminates or calls
157 .BR execve (2),
158 and cannot rely on any specific behavior with respect to shared memory.
159 .\" In AIXv3.1 vfork is equivalent to fork.
160 .SH NOTES
161 .SS Linux Notes
162 Fork handlers established using
163 .BR pthread_atfork (3)
164 are not called when a multithreaded program employing
165 the NPTL threading library calls
166 .BR vfork ().
167 Fork handlers are called in this case in a program using the
168 LinuxThreads threading library.
169 (See
170 .BR pthreads (7)
171 for a description of Linux threading libraries.)
172 .SS History
173 The
174 .BR vfork ()
175 system call appeared in 3.0BSD.
176 .\" In the release notes for 4.2BSD Sam Leffler wrote: `vfork: Is still
177 .\" present, but definitely on its way out'.
178 In 4.4BSD it was made synonymous to
179 .BR fork (2)
180 but NetBSD introduced it again,
181 cf. http://www.netbsd.org/Documentation/kernel/vfork.html .
182 In Linux, it has been equivalent to
183 .BR fork (2)
184 until 2.2.0-pre6 or so.
185 Since 2.2.0-pre9 (on i386, somewhat later on
186 other architectures) it is an independent system call.
187 Support was added in glibc 2.0.112.
188 .SH BUGS
189 It is rather unfortunate that Linux revived this specter from the past.
190 The BSD man page states:
191 "This system call will be eliminated when proper system sharing mechanisms
192 are implemented.
193 Users should not depend on the memory sharing semantics of
194 .BR vfork ()
195 as it will, in that case, be made synonymous to
196 .BR fork (2).\c
197 "
198
199 Details of the signal handling are obscure and differ between systems.
200 The BSD man page states:
201 "To avoid a possible deadlock situation, processes that are children
202 in the middle of a
203 .BR vfork ()
204 are never sent
205 .B SIGTTOU
206 or
207 .B SIGTTIN
208 signals; rather, output or
209 .IR ioctl s
210 are allowed and input attempts result in an end-of-file indication."
211 .\"
212 .\" As far as I can tell, the following is not true in 2.6.19:
213 .\" Currently (Linux 2.3.25),
214 .\" .BR strace (1)
215 .\" cannot follow
216 .\" .BR vfork ()
217 .\" and requires a kernel patch.
218 .SH "SEE ALSO"
219 .BR clone (2),
220 .BR execve (2),
221 .BR fork (2),
222 .BR unshare (2),
223 .BR wait (2)