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