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