xref: /original-bsd/lib/libc/sys/vfork.2 (revision 049d63db)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)vfork.2	6.3 (Berkeley) 03/10/91
7.\"
8.Dd
9.Dt VFORK 2
10.Os BSD 4
11.Sh NAME
12.Nm vfork
13.Nd spawn new process in a virtual memory efficient way
14.Sh SYNOPSIS
15.Fd #include <unistd.h>
16.Ft int
17.Fn vfork void
18.Sh DESCRIPTION
19.Fn Vfork
20can be used to create new processes without fully copying the address
21space of the old process, which is horrendously inefficient in a paged
22environment.  It is useful when the purpose of
23.Xr fork 2
24would have been to create a new system context for an
25.Xr execve .
26.Fn Vfork
27differs from
28.Xr fork
29in that the child borrows the parent's memory and thread of
30control until a call to
31.Xr execve 2
32or an exit (either by a call to
33.Xr exit 2
34or abnormally.)
35The parent process is suspended while the child is using its resources.
36.Pp
37.Fn Vfork
38returns 0 in the child's context and (later) the pid of the child in
39the parent's context.
40.Pp
41.Fn Vfork
42can normally be used just like
43.Xr fork .
44It does not work, however, to return while running in the childs context
45from the procedure that called
46.Fn vfork
47since the eventual return from
48.Fn vfork
49would then return to a no longer existent stack frame.
50Be careful, also, to call
51.Xr _exit
52rather than
53.Xr exit
54if you can't
55.Xr execve ,
56since
57.Xr exit
58will flush and close standard I/O channels, and thereby mess up the
59parent processes standard I/O data structures.
60(Even with
61.Xr fork
62it is wrong to call
63.Xr exit
64since buffered data would then be flushed twice.)
65.Sh SEE ALSO
66.Xr fork 2 ,
67.Xr execve 2 ,
68.Xr sigvec 2 ,
69.Xr wait 2 ,
70.Sh DIAGNOSTICS
71Same as for
72.Xr fork .
73.Sh BUGS
74This system call will be eliminated when proper system sharing
75mechanisms are implemented.
76Users should not depend on the memory
77sharing semantics of
78.Xr vfork
79as it will, in that case, be made synonymous to
80.Xr fork .
81.Pp
82To avoid a possible deadlock situation,
83processes that are children in the middle
84of a
85.Fn vfork
86are never sent
87.Dv SIGTTOU
88or
89.Dv SIGTTIN
90signals; rather,
91output or
92.Xr ioctl 2
93calls
94are allowed
95and input attempts result in an end-of-file indication.
96.Sh HISTORY
97The
98.Nm
99function call appeared in
100.Bx 3.0 .
101