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