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