xref: /csrg-svn/lib/libc/sys/vfork.2 (revision 47208)
1*47208Scael.\" Copyright (c) 1980, 1991 Regents of the University of California.
2*47208Scael.\" All rights reserved.
320151Smckusick.\"
4*47208Scael.\" %sccs.include.redist.man%
520151Smckusick.\"
6*47208Scael.\"     @(#)vfork.2	6.3 (Berkeley) 03/10/91
7*47208Scael.\"
8*47208Scael.Dd
9*47208Scael.Dt VFORK 2
10*47208Scael.Os BSD 4
11*47208Scael.Sh NAME
12*47208Scael.Nm vfork
13*47208Scael.Nd spawn new process in a virtual memory efficient way
14*47208Scael.Sh SYNOPSIS
15*47208Scael.Fd #include <unistd.h>
16*47208Scael.Ft int
17*47208Scael.Fn vfork void
18*47208Scael.Sh DESCRIPTION
19*47208Scael.Fn Vfork
2020151Smckusickcan be used to create new processes without fully copying the address
2120151Smckusickspace of the old process, which is horrendously inefficient in a paged
2220151Smckusickenvironment.  It is useful when the purpose of
23*47208Scael.Xr fork 2
2420151Smckusickwould have been to create a new system context for an
25*47208Scael.Xr execve .
26*47208Scael.Fn Vfork
2720151Smckusickdiffers from
28*47208Scael.Xr fork
2920151Smckusickin that the child borrows the parent's memory and thread of
3020151Smckusickcontrol until a call to
31*47208Scael.Xr execve 2
3220151Smckusickor an exit (either by a call to
33*47208Scael.Xr exit 2
3420151Smckusickor abnormally.)
3520151SmckusickThe parent process is suspended while the child is using its resources.
36*47208Scael.Pp
37*47208Scael.Fn Vfork
3820151Smckusickreturns 0 in the child's context and (later) the pid of the child in
3920151Smckusickthe parent's context.
40*47208Scael.Pp
41*47208Scael.Fn Vfork
4220151Smckusickcan normally be used just like
43*47208Scael.Xr fork .
4420151SmckusickIt does not work, however, to return while running in the childs context
4523829Ssechrestfrom the procedure that called
46*47208Scael.Fn vfork
4720151Smckusicksince the eventual return from
48*47208Scael.Fn vfork
4920151Smckusickwould then return to a no longer existent stack frame.
5020151SmckusickBe careful, also, to call
51*47208Scael.Xr _exit
5220151Smckusickrather than
53*47208Scael.Xr exit
5420151Smckusickif you can't
55*47208Scael.Xr execve ,
5620151Smckusicksince
57*47208Scael.Xr exit
5820151Smckusickwill flush and close standard I/O channels, and thereby mess up the
5920151Smckusickparent processes standard I/O data structures.
6020151Smckusick(Even with
61*47208Scael.Xr fork
6220151Smckusickit is wrong to call
63*47208Scael.Xr exit
6420151Smckusicksince buffered data would then be flushed twice.)
65*47208Scael.Sh SEE ALSO
66*47208Scael.Xr fork 2 ,
67*47208Scael.Xr execve 2 ,
68*47208Scael.Xr sigvec 2 ,
69*47208Scael.Xr wait 2 ,
70*47208Scael.Sh DIAGNOSTICS
7120151SmckusickSame as for
72*47208Scael.Xr fork .
73*47208Scael.Sh BUGS
7420152SmckusickThis system call will be eliminated when proper system sharing
7520152Smckusickmechanisms are implemented.
7620152SmckusickUsers should not depend on the memory
7720151Smckusicksharing semantics of
78*47208Scael.Xr vfork
7920152Smckusickas it will, in that case, be made synonymous to
80*47208Scael.Xr fork .
81*47208Scael.Pp
8220151SmckusickTo avoid a possible deadlock situation,
8323829Ssechrestprocesses that are children in the middle
8420151Smckusickof a
85*47208Scael.Fn vfork
86*47208Scaelare never sent
87*47208Scael.Dv SIGTTOU
88*47208Scaelor
89*47208Scael.Dv SIGTTIN
90*47208Scaelsignals; rather,
9120151Smckusickoutput or
92*47208Scael.Xr ioctl 2
93*47208Scaelcalls
9420151Smckusickare allowed
9520151Smckusickand input attempts result in an end-of-file indication.
96*47208Scael.Sh HISTORY
97*47208ScaelThe
98*47208Scael.Nm
99*47208Scaelfunction call appeared in
100*47208Scael.Bx 3.0 .
101