1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)vfork.2 8.1 (Berkeley) 6/4/93 29.\" 30.Dd May 22, 2016 31.Dt VFORK 2 32.Os 33.Sh NAME 34.Nm vfork 35.Nd create a new process without copying the address space 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In unistd.h 40.Ft pid_t 41.Fn vfork void 42.Sh DESCRIPTION 43.Bf -symbolic 44Since this function is hard to use correctly from application software, 45it is recommended to use 46.Xr posix_spawn 3 47or 48.Xr fork 2 49instead. 50.Ef 51.Pp 52The 53.Fn vfork 54system call 55can be used to create new processes without fully copying the address 56space of the old process, which is inefficient in a paged 57environment. 58It is useful when the purpose of 59.Xr fork 2 60would have been to create a new system context for an 61.Xr execve 2 . 62The 63.Fn vfork 64system call 65differs from 66.Xr fork 2 67in that the child borrows the parent process's address space and the 68calling thread's stack 69until a call to 70.Xr execve 2 71or an exit (either by a call to 72.Xr _exit 2 73or abnormally). 74The calling thread is suspended while the child is using its resources. 75Other threads continue to run. 76.Pp 77The 78.Fn vfork 79system call 80returns 0 in the child's context and (later) the pid of the child in 81the parent's context. 82.Pp 83Many problems can occur when replacing 84.Xr fork 2 85with 86.Fn vfork . 87For example, it does not work to return while running in the child's context 88from the procedure that called 89.Fn vfork 90since the eventual return from 91.Fn vfork 92would then return to a no longer existent stack frame. 93Also, changing process state which is partially implemented in user space 94such as signal handlers with 95.Xr libthr 3 96will corrupt the parent's state. 97.Pp 98Be careful, also, to call 99.Xr _exit 2 100rather than 101.Xr exit 3 102if you cannot 103.Xr execve 2 , 104since 105.Xr exit 3 106will flush and close standard I/O channels, and thereby mess up the 107parent processes standard I/O data structures. 108(Even with 109.Xr fork 2 110it is wrong to call 111.Xr exit 3 112since buffered data would then be flushed twice.) 113.Sh RETURN VALUES 114Same as for 115.Xr fork 2 . 116.Sh SEE ALSO 117.Xr _exit 2 , 118.Xr execve 2 , 119.Xr fork 2 , 120.Xr rfork 2 , 121.Xr sigaction 2 , 122.Xr wait 2 , 123.Xr exit 3 , 124.Xr posix_spawn 3 125.Sh HISTORY 126The 127.Fn vfork 128system call appeared in 129.Bx 3 . 130.Sh BUGS 131To avoid a possible deadlock situation, 132processes that are children in the middle 133of a 134.Fn vfork 135are never sent 136.Dv SIGTTOU 137or 138.Dv SIGTTIN 139signals; rather, 140output or 141.Xr ioctl 2 142calls 143are allowed 144and input attempts result in an end-of-file indication. 145