xref: /titanic_50/usr/src/man/man2/vfork.2 (revision 3a005aada8ac0e291c13cbc488ba9ae1473f0a96)
1aaec9ca2SGarrett D'Amore.\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
2c10c16deSRichard Lowe.\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
3c10c16deSRichard Lowe.\" Copyright 1989 AT&T.
4aaec9ca2SGarrett D'Amore.\" Copyright (c) 1980 Regents of the University of California.
5aaec9ca2SGarrett D'Amore.\" All rights reserved.  The Berkeley software License Agreement
6aaec9ca2SGarrett D'Amore.\" specifies the terms and conditions for redistribution.
7aaec9ca2SGarrett D'Amore.Dd Aug 20, 2014
8aaec9ca2SGarrett D'Amore.Dt VFORK 2
9aaec9ca2SGarrett D'Amore.Os
10aaec9ca2SGarrett D'Amore.Sh NAME
11aaec9ca2SGarrett D'Amore.Nm vfork ,
12aaec9ca2SGarrett D'Amore.Nm vforkx
13aaec9ca2SGarrett D'Amore.Nd spawn new process in a virtual memory efficient way
14aaec9ca2SGarrett D'Amore.Sh SYNOPSIS
15aaec9ca2SGarrett D'Amore.In unistd.h
16aaec9ca2SGarrett D'Amore.Ft pid_t
17aaec9ca2SGarrett D'Amore.Fn vfork void
18aaec9ca2SGarrett D'Amore.
19aaec9ca2SGarrett D'Amore.In sys/fork.h
20aaec9ca2SGarrett D'Amore.Ft pid_t
21aaec9ca2SGarrett D'Amore.Fn vforkx "int flags"
22aaec9ca2SGarrett D'Amore.Sh DESCRIPTION
23aaec9ca2SGarrett D'AmoreThe
24aaec9ca2SGarrett D'Amore.Fn vfork
25aaec9ca2SGarrett D'Amoreand
26aaec9ca2SGarrett D'Amore.Fn vforkx
27aaec9ca2SGarrett D'Amorefunctions create a new process without
28c10c16deSRichard Lowefully copying the address space of the old process. These functions are useful
29aaec9ca2SGarrett D'Amorein instances where the purpose of a
30aaec9ca2SGarrett D'Amore.Xr fork 2
31aaec9ca2SGarrett D'Amoreoperation is to create a new
32aaec9ca2SGarrett D'Amoresystem context for an
33aaec9ca2SGarrett D'Amore.Xr exec 2
34aaec9ca2SGarrett D'Amoreoperation.
35aaec9ca2SGarrett D'Amore.Lp
36aaec9ca2SGarrett D'AmoreUnlike with the
37aaec9ca2SGarrett D'Amore.Fn fork
38aaec9ca2SGarrett D'Amorefunction, the child process borrows the parent's
39aaec9ca2SGarrett D'Amorememory and thread of control until a call to
40aaec9ca2SGarrett D'Amore.Fn execve
41aaec9ca2SGarrett D'Amoreor an exit
42aaec9ca2SGarrett D'Amore.Pq either abnormally or by a call to Xr _exit 2 .
43aaec9ca2SGarrett D'AmoreAny modification
44c10c16deSRichard Lowemade during this time to any part of memory in the child process is reflected
45aaec9ca2SGarrett D'Amorein the parent process on return from
46aaec9ca2SGarrett D'Amore.Fn vfork
47aaec9ca2SGarrett D'Amoreor
48aaec9ca2SGarrett D'Amore.Fn vforkx .
49aaec9ca2SGarrett D'AmoreThe parent process is suspended while the child is using its resources.
50aaec9ca2SGarrett D'Amore.Lp
51aaec9ca2SGarrett D'AmoreIn a multithreaded application,
52aaec9ca2SGarrett D'Amore.Fn vfork
53aaec9ca2SGarrett D'Amoreand
54aaec9ca2SGarrett D'Amore.Fn vforkx
55aaec9ca2SGarrett D'Amoreborrow only the thread of control that called
56aaec9ca2SGarrett D'Amore.Fn vfork
57aaec9ca2SGarrett D'Amoreor
58aaec9ca2SGarrett D'Amore.Fn vforkx
59aaec9ca2SGarrett D'Amorein the parent; that is, the child contains only one thread. The use of
60aaec9ca2SGarrett D'Amore.Fn vfork
61aaec9ca2SGarrett D'Amoreor
62aaec9ca2SGarrett D'Amore.Fn vforkx
63aaec9ca2SGarrett D'Amorein multithreaded applications, however, is unsafe due to race
64c10c16deSRichard Loweconditions that can cause the child process to become deadlocked and
65c10c16deSRichard Loweconsequently block both the child and parent process from execution
66c10c16deSRichard Loweindefinitely.
67aaec9ca2SGarrett D'Amore.Lp
68aaec9ca2SGarrett D'AmoreThe
69aaec9ca2SGarrett D'Amore.Fn vfork
70aaec9ca2SGarrett D'Amoreand
71aaec9ca2SGarrett D'Amore.Fn vforkx
72aaec9ca2SGarrett D'Amorefunctions can normally be used the same way as
73aaec9ca2SGarrett D'Amore.Fn fork
74aaec9ca2SGarrett D'Amoreand
75aaec9ca2SGarrett D'Amore.Fn forkx ,
76aaec9ca2SGarrett D'Amorerespectively. The calling procedure,
77c10c16deSRichard Lowehowever, should not return while running in the child's context, since the
78aaec9ca2SGarrett D'Amoreeventual return from
79aaec9ca2SGarrett D'Amore.Fn vfork
80aaec9ca2SGarrett D'Amoreor
81aaec9ca2SGarrett D'Amore.Fn vforkx
82aaec9ca2SGarrett D'Amorein the parent would be to
83aaec9ca2SGarrett D'Amorea stack frame that no longer exists. The
84aaec9ca2SGarrett D'Amore.Fn _exit
85aaec9ca2SGarrett D'Amorefunction should be used
86aaec9ca2SGarrett D'Amorein favor of
87aaec9ca2SGarrett D'Amore.Xr exit 3C
88aaec9ca2SGarrett D'Amoreif unable to perform an
89aaec9ca2SGarrett D'Amore.Fn execve
90aaec9ca2SGarrett D'Amoreoperation, since
91aaec9ca2SGarrett D'Amore.Fn exit
92aaec9ca2SGarrett D'Amorewill invoke all functions registered by
93aaec9ca2SGarrett D'Amore.Xr atexit 3C
94aaec9ca2SGarrett D'Amoreand will flush and close standard I/O channels, thereby corrupting the parent
95c10c16deSRichard Loweprocess's standard I/O data structures. Care must be taken in the child process
96c10c16deSRichard Lowenot to modify any global or local data that affects the behavior of the parent
97aaec9ca2SGarrett D'Amoreprocess on return from
98aaec9ca2SGarrett D'Amore.Fn vfork
99aaec9ca2SGarrett D'Amoreor
100aaec9ca2SGarrett D'Amore.Fn vforkx ,
101aaec9ca2SGarrett D'Amoreunless such an effect
102c10c16deSRichard Loweis intentional.
103aaec9ca2SGarrett D'Amore.Lp
104aaec9ca2SGarrett D'AmoreUnlike
105aaec9ca2SGarrett D'Amore.Fn fork
106aaec9ca2SGarrett D'Amoreand
107aaec9ca2SGarrett D'Amore.Fn forkx ,
108aaec9ca2SGarrett D'Amorefork handlers are not run when
109aaec9ca2SGarrett D'Amore.Fn vfork
110aaec9ca2SGarrett D'Amoreand
111aaec9ca2SGarrett D'Amore.Fn vforkx
112aaec9ca2SGarrett D'Amoreare called.
113aaec9ca2SGarrett D'Amore.Lp
114aaec9ca2SGarrett D'AmoreThe
115aaec9ca2SGarrett D'Amore.Fn vfork
116aaec9ca2SGarrett D'Amoreand
117aaec9ca2SGarrett D'Amore.Fn vforkx
118aaec9ca2SGarrett D'Amorefunctions are deprecated. Their sole
119c10c16deSRichard Lowelegitimate use as a prelude to an immediate call to a function from the
120aaec9ca2SGarrett D'Amore.Xr exec 2
121aaec9ca2SGarrett D'Amorefamily can be achieved safely by
122aaec9ca2SGarrett D'Amore.Xr posix_spawn 3C
123aaec9ca2SGarrett D'Amoreor
124aaec9ca2SGarrett D'Amore.Xr posix_spawnp 3C .
125aaec9ca2SGarrett D'Amore.Ss "Fork Extensions"
126aaec9ca2SGarrett D'AmoreThe
127aaec9ca2SGarrett D'Amore.Fn vforkx
128aaec9ca2SGarrett D'Amorefunction accepts a
129aaec9ca2SGarrett D'Amore.Fa flags
130aaec9ca2SGarrett D'Amoreargument consisting of a
131c10c16deSRichard Lowebitwise inclusive-OR of zero or more of the following flags, which are defined
132aaec9ca2SGarrett D'Amorein the header
133aaec9ca2SGarrett D'Amore.In sys/fork.h :
134aaec9ca2SGarrett D'Amore.Lp
135aaec9ca2SGarrett D'Amore.Bl -item -compact -offset indent
136aaec9ca2SGarrett D'Amore.It
137aaec9ca2SGarrett D'Amore.Dv FORK_NOSIGCHLD
138aaec9ca2SGarrett D'Amore.It
139aaec9ca2SGarrett D'Amore.Dv FORK_WAITPID
140aaec9ca2SGarrett D'Amore.El
141aaec9ca2SGarrett D'Amore.Lp
142aaec9ca2SGarrett D'AmoreSee
143aaec9ca2SGarrett D'Amore.Xr fork 2
144aaec9ca2SGarrett D'Amorefor descriptions of these flags. If the
145aaec9ca2SGarrett D'Amore.Fa flags
146aaec9ca2SGarrett D'Amoreargument is 0,
147aaec9ca2SGarrett D'Amore.Fn vforkx
148aaec9ca2SGarrett D'Amoreis identical to
149aaec9ca2SGarrett D'Amore.Fn vfork .
150aaec9ca2SGarrett D'Amore.Sh RETURN VALUES
151aaec9ca2SGarrett D'AmoreUpon successful completion,
152aaec9ca2SGarrett D'Amore.Fn vfork
153aaec9ca2SGarrett D'Amoreand
154aaec9ca2SGarrett D'Amore.Fn vforkx
155aaec9ca2SGarrett D'Amorereturn 0 to
156aaec9ca2SGarrett D'Amorethe child process and return the process ID of the child process to the parent
157aaec9ca2SGarrett D'Amoreprocess. Otherwise, \(mi1 is returned to the parent process, no child
158aaec9ca2SGarrett D'Amoreprocess is created, and
159aaec9ca2SGarrett D'Amore.Va errno
160aaec9ca2SGarrett D'Amoreis set to indicate the error.
161aaec9ca2SGarrett D'Amore.Sh ERRORS
162aaec9ca2SGarrett D'AmoreThe
163aaec9ca2SGarrett D'Amore.Fn vfork
164aaec9ca2SGarrett D'Amoreand
165aaec9ca2SGarrett D'Amore.Fn vforkx
166aaec9ca2SGarrett D'Amorefunctions will fail if:
167aaec9ca2SGarrett D'Amore.Bl -tag -width Er
168aaec9ca2SGarrett D'Amore.It Er EAGAIN
169c10c16deSRichard LoweThe system-imposed limit on the total number of processes under execution
170c10c16deSRichard Lowe(either system-quality or by a single user) would be exceeded. This limit is
171c10c16deSRichard Lowedetermined when the system is generated.
172aaec9ca2SGarrett D'Amore.
173aaec9ca2SGarrett D'Amore.It Er ENOMEM
174c10c16deSRichard LoweThere is insufficient swap space for the new process.
175aaec9ca2SGarrett D'Amore.El
176aaec9ca2SGarrett D'Amore.Lp
177aaec9ca2SGarrett D'AmoreThe
178aaec9ca2SGarrett D'Amore.Fn vforkx
179aaec9ca2SGarrett D'Amorefunction will fail if:
180aaec9ca2SGarrett D'Amore.Bl -tag -width Er
181aaec9ca2SGarrett D'Amore.It Er EINVAL
182aaec9ca2SGarrett D'AmoreThe
183aaec9ca2SGarrett D'Amore.Va flags
184aaec9ca2SGarrett D'Amoreargument is invalid.
185aaec9ca2SGarrett D'Amore.El
186aaec9ca2SGarrett D'Amore.Sh INTERFACE STABILITY
187aaec9ca2SGarrett D'AmoreThe
188aaec9ca2SGarrett D'Amore.Fn vfork
189aaec9ca2SGarrett D'Amorefunction is
190aaec9ca2SGarrett D'Amore.Sy Obsolete Standard .
191aaec9ca2SGarrett D'Amore.Lp
192aaec9ca2SGarrett D'AmoreThe
193aaec9ca2SGarrett D'Amore.Fn vforkx
194aaec9ca2SGarrett D'Amorefunction is
195aaec9ca2SGarrett D'Amore.Sy Obsolete Uncommitted .
196aaec9ca2SGarrett D'Amore.Sh MT-LEVEL
197aaec9ca2SGarrett D'Amore.Sy Unsafe .
198aaec9ca2SGarrett D'Amore.Sh SEE ALSO
199aaec9ca2SGarrett D'Amore.Xr exec 2 ,
200aaec9ca2SGarrett D'Amore.Xr exit 2 ,
201aaec9ca2SGarrett D'Amore.Xr fork 2 ,
202aaec9ca2SGarrett D'Amore.Xr ioctl 2 ,
203aaec9ca2SGarrett D'Amore.Xr atexit 3C ,
204aaec9ca2SGarrett D'Amore.Xr exit 3C ,
205aaec9ca2SGarrett D'Amore.Xr posix_spawn 3C ,
206aaec9ca2SGarrett D'Amore.Xr posix_spawnp 3C ,
207aaec9ca2SGarrett D'Amore.Xr wait 3C ,
208*3a005aadSYuri Pankov.Xr signal.h 3HEAD ,
209aaec9ca2SGarrett D'Amore.Xr standards 5
210aaec9ca2SGarrett D'Amore.Sh NOTES
211c10c16deSRichard LoweTo avoid a possible deadlock situation, processes that are children in the
212aaec9ca2SGarrett D'Amoremiddle of a
213aaec9ca2SGarrett D'Amore.Fn vfork
214aaec9ca2SGarrett D'Amoreor
215aaec9ca2SGarrett D'Amore.Fn vforkx
216aaec9ca2SGarrett D'Amoreare never sent
217aaec9ca2SGarrett D'Amore.Dv SIGTTOU
218aaec9ca2SGarrett D'Amoreor
219aaec9ca2SGarrett D'Amore.Dv SIGTTIN
220aaec9ca2SGarrett D'Amoresignals; rather, output or ioctls are allowed and input attempts
221aaec9ca2SGarrett D'Amoreresult in an
222aaec9ca2SGarrett D'Amore.Dv EOF
223aaec9ca2SGarrett D'Amoreindication.
224aaec9ca2SGarrett D'Amore.Lp
225c10c16deSRichard LoweTo forestall parent memory corruption due to race conditions with signal
226aaec9ca2SGarrett D'Amorehandling,
227aaec9ca2SGarrett D'Amore.Fn vfork
228aaec9ca2SGarrett D'Amoreand
229aaec9ca2SGarrett D'Amore.Fn vforkx
230aaec9ca2SGarrett D'Amoretreat signal handlers in the child
231aaec9ca2SGarrett D'Amoreprocess in the same manner as the
232aaec9ca2SGarrett D'Amore.Xr exec 2
233aaec9ca2SGarrett D'Amorefunctions: signals set to be
234aaec9ca2SGarrett D'Amorecaught by the parent process are set to the default action
235aaec9ca2SGarrett D'Amore.Pq Dv SIG_DFL
236aaec9ca2SGarrett D'Amorein the child process
237aaec9ca2SGarrett D'Amore.Pq see Xr signal.h 3HEAD .
238aaec9ca2SGarrett D'AmoreAny attempt to set a signal
239aaec9ca2SGarrett D'Amorehandler in the child before
240aaec9ca2SGarrett D'Amore.Fn execve
241aaec9ca2SGarrett D'Amoreto anything other than
242aaec9ca2SGarrett D'Amore.Dv SIG_DFL
243aaec9ca2SGarrett D'Amoreor
244aaec9ca2SGarrett D'Amore.Dv SIG_IGN
245aaec9ca2SGarrett D'Amoreis disallowed and results in setting the handler to
246aaec9ca2SGarrett D'Amore.Dv SIG_DFL .
247aaec9ca2SGarrett D'Amore.Lp
248aaec9ca2SGarrett D'AmoreOn some systems, the implementation of
249aaec9ca2SGarrett D'Amore.Fn vfork
250aaec9ca2SGarrett D'Amoreand
251aaec9ca2SGarrett D'Amore.Fn vforkx
252aaec9ca2SGarrett D'Amorecause
253c10c16deSRichard Lowethe parent to inherit register values from the child. This can create problems
254aaec9ca2SGarrett D'Amorefor certain optimizing compilers if
255aaec9ca2SGarrett D'Amore.In unistd.h
256aaec9ca2SGarrett D'Amoreis not included in the source calling
257aaec9ca2SGarrett D'Amore.Fn vfork
258aaec9ca2SGarrett D'Amoreor if
259aaec9ca2SGarrett D'Amore.In sys/fork.h
260aaec9ca2SGarrett D'Amoreis not included in the
261aaec9ca2SGarrett D'Amoresource calling
262aaec9ca2SGarrett D'Amore.Fn vforkx .
263aaec9ca2SGarrett D'Amore.Sh STANDARDS
264aaec9ca2SGarrett D'AmoreThe
265aaec9ca2SGarrett D'Amore.Fn vfork
266aaec9ca2SGarrett D'Amorefunction is available in the following compilation environments.  See
267aaec9ca2SGarrett D'Amore.Xr standards 5 .
268aaec9ca2SGarrett D'Amore.Lp
269aaec9ca2SGarrett D'Amore.Bl -bullet -compact
270aaec9ca2SGarrett D'Amore.It
271aaec9ca2SGarrett D'Amore.St -xpg4.2
272aaec9ca2SGarrett D'Amore.It
273aaec9ca2SGarrett D'Amore.St -susv2
274aaec9ca2SGarrett D'Amore.It
275aaec9ca2SGarrett D'Amore.St -susv3
276aaec9ca2SGarrett D'Amore.El
277aaec9ca2SGarrett D'Amore.Lp
278aaec9ca2SGarrett D'AmoreIt was marked obsolete in
279aaec9ca2SGarrett D'Amore.St -susv3
280aaec9ca2SGarrett D'Amoreand removed from
281aaec9ca2SGarrett D'Amore.St -p1003.1-2008 .
282aaec9ca2SGarrett D'Amore.Lp
283aaec9ca2SGarrett D'AmoreThe
284aaec9ca2SGarrett D'Amore.Fn vforkx
285aaec9ca2SGarrett D'Amorefunction is a local extension and not available in any strictly
286aaec9ca2SGarrett D'Amorestandards-compliant compilation environment.
287