xref: /freebsd/lib/libsys/vfork.2 (revision 1edb7116)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 22, 2016
29.Dt VFORK 2
30.Os
31.Sh NAME
32.Nm vfork
33.Nd create a new process without copying the address space
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In unistd.h
38.Ft pid_t
39.Fn vfork void
40.Sh DESCRIPTION
41.Bf -symbolic
42Since this function is hard to use correctly from application software,
43it is recommended to use
44.Xr posix_spawn 3
45or
46.Xr fork 2
47instead.
48.Ef
49.Pp
50The
51.Fn vfork
52system call
53can be used to create new processes without fully copying the address
54space of the old process, which is inefficient in a paged
55environment.
56It is useful when the purpose of
57.Xr fork 2
58would have been to create a new system context for an
59.Xr execve 2 .
60The
61.Fn vfork
62system call
63differs from
64.Xr fork 2
65in that the child borrows the parent process's address space and the
66calling thread's stack
67until a call to
68.Xr execve 2
69or an exit (either by a call to
70.Xr _exit 2
71or abnormally).
72The calling thread is suspended while the child is using its resources.
73Other threads continue to run.
74.Pp
75The
76.Fn vfork
77system call
78returns 0 in the child's context and (later) the pid of the child in
79the parent's context.
80.Pp
81Many problems can occur when replacing
82.Xr fork 2
83with
84.Fn vfork .
85For example, it does not work to return while running in the child's context
86from the procedure that called
87.Fn vfork
88since the eventual return from
89.Fn vfork
90would then return to a no longer existent stack frame.
91Also, changing process state which is partially implemented in user space
92such as signal handlers with
93.Xr libthr 3
94will corrupt the parent's state.
95.Pp
96Be careful, also, to call
97.Xr _exit 2
98rather than
99.Xr exit 3
100if you cannot
101.Xr execve 2 ,
102since
103.Xr exit 3
104will flush and close standard I/O channels, and thereby mess up the
105parent processes standard I/O data structures.
106(Even with
107.Xr fork 2
108it is wrong to call
109.Xr exit 3
110since buffered data would then be flushed twice.)
111.Sh RETURN VALUES
112Same as for
113.Xr fork 2 .
114.Sh SEE ALSO
115.Xr _exit 2 ,
116.Xr execve 2 ,
117.Xr fork 2 ,
118.Xr rfork 2 ,
119.Xr sigaction 2 ,
120.Xr wait 2 ,
121.Xr exit 3 ,
122.Xr posix_spawn 3
123.Sh HISTORY
124The
125.Fn vfork
126system call appeared in
127.Bx 3 .
128.Sh BUGS
129To avoid a possible deadlock situation,
130processes that are children in the middle
131of a
132.Fn vfork
133are never sent
134.Dv SIGTTOU
135or
136.Dv SIGTTIN
137signals; rather,
138output or
139.Xr ioctl 2
140calls
141are allowed
142and input attempts result in an end-of-file indication.
143