xref: /illumos-gate/usr/src/man/man2/vfork.2 (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
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
2872d3dbb9SYuri Pankovfully copying the address space of the old process.
2972d3dbb9SYuri PankovThese functions are useful in 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
5972d3dbb9SYuri Pankovin the parent; that is, the child contains only one thread.
6072d3dbb9SYuri PankovThe use of
61aaec9ca2SGarrett D'Amore.Fn vfork
62aaec9ca2SGarrett D'Amoreor
63aaec9ca2SGarrett D'Amore.Fn vforkx
64aaec9ca2SGarrett D'Amorein multithreaded applications, however, is unsafe due to race
65c10c16deSRichard Loweconditions that can cause the child process to become deadlocked and
66c10c16deSRichard Loweconsequently block both the child and parent process from execution
67c10c16deSRichard Loweindefinitely.
68aaec9ca2SGarrett D'Amore.Lp
69aaec9ca2SGarrett D'AmoreThe
70aaec9ca2SGarrett D'Amore.Fn vfork
71aaec9ca2SGarrett D'Amoreand
72aaec9ca2SGarrett D'Amore.Fn vforkx
73aaec9ca2SGarrett D'Amorefunctions can normally be used the same way as
74aaec9ca2SGarrett D'Amore.Fn fork
75aaec9ca2SGarrett D'Amoreand
76aaec9ca2SGarrett D'Amore.Fn forkx ,
7772d3dbb9SYuri Pankovrespectively.
7872d3dbb9SYuri PankovThe calling procedure, however, should not return while running in the child's
7972d3dbb9SYuri Pankovcontext, since the eventual return from
80aaec9ca2SGarrett D'Amore.Fn vfork
81aaec9ca2SGarrett D'Amoreor
82aaec9ca2SGarrett D'Amore.Fn vforkx
83aaec9ca2SGarrett D'Amorein the parent would be to
8472d3dbb9SYuri Pankova stack frame that no longer exists.
8572d3dbb9SYuri PankovThe
86aaec9ca2SGarrett D'Amore.Fn _exit
87aaec9ca2SGarrett D'Amorefunction should be used
88aaec9ca2SGarrett D'Amorein favor of
89aaec9ca2SGarrett D'Amore.Xr exit 3C
90aaec9ca2SGarrett D'Amoreif unable to perform an
91aaec9ca2SGarrett D'Amore.Fn execve
92aaec9ca2SGarrett D'Amoreoperation, since
93aaec9ca2SGarrett D'Amore.Fn exit
94aaec9ca2SGarrett D'Amorewill invoke all functions registered by
95aaec9ca2SGarrett D'Amore.Xr atexit 3C
96aaec9ca2SGarrett D'Amoreand will flush and close standard I/O channels, thereby corrupting the parent
9772d3dbb9SYuri Pankovprocess's standard I/O data structures.
9872d3dbb9SYuri PankovCare must be taken in the child process not to modify any global or local data
9972d3dbb9SYuri Pankovthat affects the behavior of the parent process on return from
100aaec9ca2SGarrett D'Amore.Fn vfork
101aaec9ca2SGarrett D'Amoreor
102aaec9ca2SGarrett D'Amore.Fn vforkx ,
103aaec9ca2SGarrett D'Amoreunless such an effect
104c10c16deSRichard Loweis intentional.
105aaec9ca2SGarrett D'Amore.Lp
106aaec9ca2SGarrett D'AmoreUnlike
107aaec9ca2SGarrett D'Amore.Fn fork
108aaec9ca2SGarrett D'Amoreand
109aaec9ca2SGarrett D'Amore.Fn forkx ,
110aaec9ca2SGarrett D'Amorefork handlers are not run when
111aaec9ca2SGarrett D'Amore.Fn vfork
112aaec9ca2SGarrett D'Amoreand
113aaec9ca2SGarrett D'Amore.Fn vforkx
114aaec9ca2SGarrett D'Amoreare called.
115aaec9ca2SGarrett D'Amore.Lp
116aaec9ca2SGarrett D'AmoreThe
117aaec9ca2SGarrett D'Amore.Fn vfork
118aaec9ca2SGarrett D'Amoreand
119aaec9ca2SGarrett D'Amore.Fn vforkx
12072d3dbb9SYuri Pankovfunctions are deprecated.
12172d3dbb9SYuri PankovTheir sole legitimate use as a prelude to an immediate call to a function from
12272d3dbb9SYuri Pankovthe
123aaec9ca2SGarrett D'Amore.Xr exec 2
124aaec9ca2SGarrett D'Amorefamily can be achieved safely by
125aaec9ca2SGarrett D'Amore.Xr posix_spawn 3C
126aaec9ca2SGarrett D'Amoreor
127aaec9ca2SGarrett D'Amore.Xr posix_spawnp 3C .
128aaec9ca2SGarrett D'Amore.Ss "Fork Extensions"
129aaec9ca2SGarrett D'AmoreThe
130aaec9ca2SGarrett D'Amore.Fn vforkx
131aaec9ca2SGarrett D'Amorefunction accepts a
132aaec9ca2SGarrett D'Amore.Fa flags
133aaec9ca2SGarrett D'Amoreargument consisting of a
134c10c16deSRichard Lowebitwise inclusive-OR of zero or more of the following flags, which are defined
135aaec9ca2SGarrett D'Amorein the header
136aaec9ca2SGarrett D'Amore.In sys/fork.h :
137aaec9ca2SGarrett D'Amore.Lp
138aaec9ca2SGarrett D'Amore.Bl -item -compact -offset indent
139aaec9ca2SGarrett D'Amore.It
140aaec9ca2SGarrett D'Amore.Dv FORK_NOSIGCHLD
141aaec9ca2SGarrett D'Amore.It
142aaec9ca2SGarrett D'Amore.Dv FORK_WAITPID
143aaec9ca2SGarrett D'Amore.El
144aaec9ca2SGarrett D'Amore.Lp
145aaec9ca2SGarrett D'AmoreSee
146aaec9ca2SGarrett D'Amore.Xr fork 2
14772d3dbb9SYuri Pankovfor descriptions of these flags.
14872d3dbb9SYuri PankovIf the
149aaec9ca2SGarrett D'Amore.Fa flags
150aaec9ca2SGarrett D'Amoreargument is 0,
151aaec9ca2SGarrett D'Amore.Fn vforkx
152aaec9ca2SGarrett D'Amoreis identical to
153aaec9ca2SGarrett D'Amore.Fn vfork .
154aaec9ca2SGarrett D'Amore.Sh RETURN VALUES
155aaec9ca2SGarrett D'AmoreUpon successful completion,
156aaec9ca2SGarrett D'Amore.Fn vfork
157aaec9ca2SGarrett D'Amoreand
158aaec9ca2SGarrett D'Amore.Fn vforkx
159aaec9ca2SGarrett D'Amorereturn 0 to
160aaec9ca2SGarrett D'Amorethe child process and return the process ID of the child process to the parent
16172d3dbb9SYuri Pankovprocess.
16272d3dbb9SYuri PankovOtherwise, \(mi1 is returned to the parent process, no child process is created,
16372d3dbb9SYuri Pankovand
164aaec9ca2SGarrett D'Amore.Va errno
165aaec9ca2SGarrett D'Amoreis set to indicate the error.
166aaec9ca2SGarrett D'Amore.Sh ERRORS
167aaec9ca2SGarrett D'AmoreThe
168aaec9ca2SGarrett D'Amore.Fn vfork
169aaec9ca2SGarrett D'Amoreand
170aaec9ca2SGarrett D'Amore.Fn vforkx
171aaec9ca2SGarrett D'Amorefunctions will fail if:
172aaec9ca2SGarrett D'Amore.Bl -tag -width Er
173aaec9ca2SGarrett D'Amore.It Er EAGAIN
174c10c16deSRichard LoweThe system-imposed limit on the total number of processes under execution
17572d3dbb9SYuri Pankov(either system-quality or by a single user) would be exceeded.
17672d3dbb9SYuri PankovThis limit is determined when the system is generated.
177aaec9ca2SGarrett D'Amore.
178aaec9ca2SGarrett D'Amore.It Er ENOMEM
179c10c16deSRichard LoweThere is insufficient swap space for the new process.
180aaec9ca2SGarrett D'Amore.El
181aaec9ca2SGarrett D'Amore.Lp
182aaec9ca2SGarrett D'AmoreThe
183aaec9ca2SGarrett D'Amore.Fn vforkx
184aaec9ca2SGarrett D'Amorefunction will fail if:
185aaec9ca2SGarrett D'Amore.Bl -tag -width Er
186aaec9ca2SGarrett D'Amore.It Er EINVAL
187aaec9ca2SGarrett D'AmoreThe
188aaec9ca2SGarrett D'Amore.Va flags
189aaec9ca2SGarrett D'Amoreargument is invalid.
190aaec9ca2SGarrett D'Amore.El
191aaec9ca2SGarrett D'Amore.Sh INTERFACE STABILITY
192aaec9ca2SGarrett D'AmoreThe
193aaec9ca2SGarrett D'Amore.Fn vfork
194aaec9ca2SGarrett D'Amorefunction is
195aaec9ca2SGarrett D'Amore.Sy Obsolete Standard .
196aaec9ca2SGarrett D'Amore.Lp
197aaec9ca2SGarrett D'AmoreThe
198aaec9ca2SGarrett D'Amore.Fn vforkx
199aaec9ca2SGarrett D'Amorefunction is
200aaec9ca2SGarrett D'Amore.Sy Obsolete Uncommitted .
201aaec9ca2SGarrett D'Amore.Sh MT-LEVEL
202aaec9ca2SGarrett D'Amore.Sy Unsafe .
203aaec9ca2SGarrett D'Amore.Sh SEE ALSO
204aaec9ca2SGarrett D'Amore.Xr exec 2 ,
205aaec9ca2SGarrett D'Amore.Xr exit 2 ,
206aaec9ca2SGarrett D'Amore.Xr fork 2 ,
207aaec9ca2SGarrett D'Amore.Xr ioctl 2 ,
208aaec9ca2SGarrett D'Amore.Xr atexit 3C ,
209aaec9ca2SGarrett D'Amore.Xr exit 3C ,
210aaec9ca2SGarrett D'Amore.Xr posix_spawn 3C ,
211aaec9ca2SGarrett D'Amore.Xr posix_spawnp 3C ,
212aaec9ca2SGarrett D'Amore.Xr wait 3C ,
2133a005aadSYuri Pankov.Xr signal.h 3HEAD ,
214*bbf21555SRichard Lowe.Xr standards 7
215aaec9ca2SGarrett D'Amore.Sh NOTES
216c10c16deSRichard LoweTo avoid a possible deadlock situation, processes that are children in the
217aaec9ca2SGarrett D'Amoremiddle of a
218aaec9ca2SGarrett D'Amore.Fn vfork
219aaec9ca2SGarrett D'Amoreor
220aaec9ca2SGarrett D'Amore.Fn vforkx
221aaec9ca2SGarrett D'Amoreare never sent
222aaec9ca2SGarrett D'Amore.Dv SIGTTOU
223aaec9ca2SGarrett D'Amoreor
224aaec9ca2SGarrett D'Amore.Dv SIGTTIN
225aaec9ca2SGarrett D'Amoresignals; rather, output or ioctls are allowed and input attempts
226aaec9ca2SGarrett D'Amoreresult in an
227aaec9ca2SGarrett D'Amore.Dv EOF
228aaec9ca2SGarrett D'Amoreindication.
229aaec9ca2SGarrett D'Amore.Lp
230c10c16deSRichard LoweTo forestall parent memory corruption due to race conditions with signal
231aaec9ca2SGarrett D'Amorehandling,
232aaec9ca2SGarrett D'Amore.Fn vfork
233aaec9ca2SGarrett D'Amoreand
234aaec9ca2SGarrett D'Amore.Fn vforkx
235aaec9ca2SGarrett D'Amoretreat signal handlers in the child
236aaec9ca2SGarrett D'Amoreprocess in the same manner as the
237aaec9ca2SGarrett D'Amore.Xr exec 2
238aaec9ca2SGarrett D'Amorefunctions: signals set to be
239aaec9ca2SGarrett D'Amorecaught by the parent process are set to the default action
240aaec9ca2SGarrett D'Amore.Pq Dv SIG_DFL
241aaec9ca2SGarrett D'Amorein the child process
242aaec9ca2SGarrett D'Amore.Pq see Xr signal.h 3HEAD .
243aaec9ca2SGarrett D'AmoreAny attempt to set a signal
244aaec9ca2SGarrett D'Amorehandler in the child before
245aaec9ca2SGarrett D'Amore.Fn execve
246aaec9ca2SGarrett D'Amoreto anything other than
247aaec9ca2SGarrett D'Amore.Dv SIG_DFL
248aaec9ca2SGarrett D'Amoreor
249aaec9ca2SGarrett D'Amore.Dv SIG_IGN
250aaec9ca2SGarrett D'Amoreis disallowed and results in setting the handler to
251aaec9ca2SGarrett D'Amore.Dv SIG_DFL .
252aaec9ca2SGarrett D'Amore.Lp
253aaec9ca2SGarrett D'AmoreOn some systems, the implementation of
254aaec9ca2SGarrett D'Amore.Fn vfork
255aaec9ca2SGarrett D'Amoreand
256aaec9ca2SGarrett D'Amore.Fn vforkx
257aaec9ca2SGarrett D'Amorecause
25872d3dbb9SYuri Pankovthe parent to inherit register values from the child.
25972d3dbb9SYuri PankovThis can create problems for certain optimizing compilers if
260aaec9ca2SGarrett D'Amore.In unistd.h
261aaec9ca2SGarrett D'Amoreis not included in the source calling
262aaec9ca2SGarrett D'Amore.Fn vfork
263aaec9ca2SGarrett D'Amoreor if
264aaec9ca2SGarrett D'Amore.In sys/fork.h
265aaec9ca2SGarrett D'Amoreis not included in the
266aaec9ca2SGarrett D'Amoresource calling
267aaec9ca2SGarrett D'Amore.Fn vforkx .
268aaec9ca2SGarrett D'Amore.Sh STANDARDS
269aaec9ca2SGarrett D'AmoreThe
270aaec9ca2SGarrett D'Amore.Fn vfork
27172d3dbb9SYuri Pankovfunction is available in the following compilation environments.
27272d3dbb9SYuri PankovSee
273*bbf21555SRichard Lowe.Xr standards 7 .
274aaec9ca2SGarrett D'Amore.Lp
275aaec9ca2SGarrett D'Amore.Bl -bullet -compact
276aaec9ca2SGarrett D'Amore.It
277aaec9ca2SGarrett D'Amore.St -xpg4.2
278aaec9ca2SGarrett D'Amore.It
279aaec9ca2SGarrett D'Amore.St -susv2
280aaec9ca2SGarrett D'Amore.It
281aaec9ca2SGarrett D'Amore.St -susv3
282aaec9ca2SGarrett D'Amore.El
283aaec9ca2SGarrett D'Amore.Lp
284aaec9ca2SGarrett D'AmoreIt was marked obsolete in
285aaec9ca2SGarrett D'Amore.St -susv3
286aaec9ca2SGarrett D'Amoreand removed from
287aaec9ca2SGarrett D'Amore.St -p1003.1-2008 .
288aaec9ca2SGarrett D'Amore.Lp
289aaec9ca2SGarrett D'AmoreThe
290aaec9ca2SGarrett D'Amore.Fn vforkx
291aaec9ca2SGarrett D'Amorefunction is a local extension and not available in any strictly
292aaec9ca2SGarrett D'Amorestandards-compliant compilation environment.
293