1*0a6a1f1dSLionel Sambuc.\" $NetBSD: vfork.2,v 1.27 2014/07/18 16:02:50 dholland Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1980, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)vfork.2 8.1 (Berkeley) 6/4/93 312fe8fb19SBen Gras.\" 32*0a6a1f1dSLionel Sambuc.Dd July 18, 2014 332fe8fb19SBen Gras.Dt VFORK 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm vfork 372fe8fb19SBen Gras.Nd spawn new process in a virtual memory efficient way 382fe8fb19SBen Gras.Sh LIBRARY 392fe8fb19SBen Gras.Lb libc 402fe8fb19SBen Gras.Sh SYNOPSIS 412fe8fb19SBen Gras.In unistd.h 422fe8fb19SBen Gras.Ft pid_t 432fe8fb19SBen Gras.Fn vfork void 442fe8fb19SBen Gras.Sh DESCRIPTION 452fe8fb19SBen GrasThe 462fe8fb19SBen Gras.Nm 472fe8fb19SBen Grassystem call creates a new process that does not have a new 482fe8fb19SBen Grasvirtual address space, but rather shares address space with the 492fe8fb19SBen Grasparent, thus avoiding potentially expensive copy-on-write operations 502fe8fb19SBen Grasnormally associated with creating a new process. 512fe8fb19SBen GrasIt is useful when the purpose of 522fe8fb19SBen Gras.Xr fork 2 532fe8fb19SBen Graswould have been to create a new system context for an 542fe8fb19SBen Gras.Xr execve 2 . 552fe8fb19SBen GrasThe 562fe8fb19SBen Gras.Nm 572fe8fb19SBen Grassystem call differs from 582fe8fb19SBen Gras.Xr fork 2 592fe8fb19SBen Grasin that the child borrows the parent's memory and thread of 602fe8fb19SBen Grascontrol until a call to 612fe8fb19SBen Gras.Xr execve 2 622fe8fb19SBen Grasor an exit (either by a call to 632fe8fb19SBen Gras.Xr _exit 2 642fe8fb19SBen Grasor abnormally). 652fe8fb19SBen GrasThe parent process is suspended while the child is using its resources. 662fe8fb19SBen Gras.Pp 672fe8fb19SBen GrasThe 682fe8fb19SBen Gras.Nm 692fe8fb19SBen Grassystem call returns 0 in the child's context and (later) the pid 702fe8fb19SBen Grasof the child in the parent's context. 712fe8fb19SBen Gras.Pp 722fe8fb19SBen GrasThe 732fe8fb19SBen Gras.Nm 742fe8fb19SBen Grassystem call can normally be used just like 752fe8fb19SBen Gras.Xr fork 2 . 76*0a6a1f1dSLionel SambucIt does not work, however, to return while running in the child's context 772fe8fb19SBen Grasfrom the procedure that called 782fe8fb19SBen Gras.Fn vfork 792fe8fb19SBen Grassince the eventual return from 802fe8fb19SBen Gras.Fn vfork 81*0a6a1f1dSLionel Sambucwould then return to a no longer existing stack frame. 822fe8fb19SBen GrasBe careful, also, to call 832fe8fb19SBen Gras.Xr _exit 2 842fe8fb19SBen Grasrather than 852fe8fb19SBen Gras.Xr exit 3 862fe8fb19SBen Grasif you can't 872fe8fb19SBen Gras.Xr execve 2 , 882fe8fb19SBen Grassince 892fe8fb19SBen Gras.Xr exit 3 902fe8fb19SBen Graswill flush and close standard I/O channels, and thereby mess up the 912fe8fb19SBen Grasstandard I/O data structures 922fe8fb19SBen Grasin the parent process. 932fe8fb19SBen Gras(Even with 942fe8fb19SBen Gras.Xr fork 2 952fe8fb19SBen Grasit is wrong to call 962fe8fb19SBen Gras.Xr exit 3 972fe8fb19SBen Grassince buffered data would then be flushed twice.) 982fe8fb19SBen Gras.Sh RETURN VALUES 992fe8fb19SBen GrasSame as for 1002fe8fb19SBen Gras.Xr fork 2 . 1012fe8fb19SBen Gras.Sh ERRORS 1022fe8fb19SBen GrasSame as for 1032fe8fb19SBen Gras.Xr fork 2 . 1042fe8fb19SBen Gras.Sh SEE ALSO 1052fe8fb19SBen Gras.Xr execve 2 , 1062fe8fb19SBen Gras.Xr fork 2 , 1072fe8fb19SBen Gras.Xr sigaction 2 , 1082fe8fb19SBen Gras.Xr wait 2 1092fe8fb19SBen Gras.Sh HISTORY 1102fe8fb19SBen GrasThe 1112fe8fb19SBen Gras.Fn vfork 1122fe8fb19SBen Grasfunction call appeared in 1132fe8fb19SBen Gras.Bx 3.0 . 1142fe8fb19SBen GrasIn 1152fe8fb19SBen Gras.Bx 4.4 , 116*0a6a1f1dSLionel Sambucthe semantics were changed to only suspend the parent and not share 117*0a6a1f1dSLionel Sambucthe address space. 1182fe8fb19SBen GrasThe original semantics were reintroduced in 1192fe8fb19SBen Gras.Nx 1.4 . 1202fe8fb19SBen Gras.Sh BUGS 121*0a6a1f1dSLionel SambucPortable applications should not depend on the memory sharing 122*0a6a1f1dSLionel Sambucsemantics of 1232fe8fb19SBen Gras.Fn vfork 124*0a6a1f1dSLionel Sambucas implementations exist that implement 125*0a6a1f1dSLionel Sambuc.Fn vfork 126*0a6a1f1dSLionel Sambucas plain 127*0a6a1f1dSLionel Sambuc.Xr fork 2 . 1282fe8fb19SBen Gras.Pp 1292fe8fb19SBen GrasTo avoid a possible deadlock situation, processes that are children 1302fe8fb19SBen Grasin the middle of a 1312fe8fb19SBen Gras.Fn vfork 1322fe8fb19SBen Grasare never sent 1332fe8fb19SBen Gras.Dv SIGTTOU 1342fe8fb19SBen Grasor 1352fe8fb19SBen Gras.Dv SIGTTIN 1362fe8fb19SBen Grassignals; rather, output or 1372fe8fb19SBen Gras.Xr ioctl 2 1382fe8fb19SBen Grascalls are allowed and input attempts result in an end-of-file indication. 139