1*2fe8fb19SBen Gras.\" $NetBSD: fork.2,v 1.22 2004/06/25 15:29:25 wiz Exp $ 2*2fe8fb19SBen Gras.\" 3*2fe8fb19SBen Gras.\" Copyright (c) 1980, 1991, 1993 4*2fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 5*2fe8fb19SBen Gras.\" 6*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 7*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 8*2fe8fb19SBen Gras.\" are met: 9*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 10*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 11*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 12*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 13*2fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 14*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 15*2fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 16*2fe8fb19SBen Gras.\" without specific prior written permission. 17*2fe8fb19SBen Gras.\" 18*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*2fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*2fe8fb19SBen Gras.\" SUCH DAMAGE. 29*2fe8fb19SBen Gras.\" 30*2fe8fb19SBen Gras.\" @(#)fork.2 8.1 (Berkeley) 6/4/93 31*2fe8fb19SBen Gras.\" 32*2fe8fb19SBen Gras.Dd June 10, 2004 33*2fe8fb19SBen Gras.Dt FORK 2 34*2fe8fb19SBen Gras.Os 35*2fe8fb19SBen Gras.Sh NAME 36*2fe8fb19SBen Gras.Nm fork 37*2fe8fb19SBen Gras.Nd create a new process 38*2fe8fb19SBen Gras.Sh LIBRARY 39*2fe8fb19SBen Gras.Lb libc 40*2fe8fb19SBen Gras.Sh SYNOPSIS 41*2fe8fb19SBen Gras.In unistd.h 42*2fe8fb19SBen Gras.Ft pid_t 43*2fe8fb19SBen Gras.Fn fork void 44*2fe8fb19SBen Gras.Sh DESCRIPTION 45*2fe8fb19SBen Gras.Fn fork 46*2fe8fb19SBen Grascauses creation of a new process. 47*2fe8fb19SBen GrasThe new process (child process) is an exact copy of the 48*2fe8fb19SBen Grascalling process (parent process) except for the following: 49*2fe8fb19SBen Gras.Bl -bullet -offset indent 50*2fe8fb19SBen Gras.It 51*2fe8fb19SBen GrasThe child process has a unique process ID. 52*2fe8fb19SBen Gras.It 53*2fe8fb19SBen GrasThe child process has a different parent 54*2fe8fb19SBen Grasprocess ID (i.e., the process ID of the parent process). 55*2fe8fb19SBen Gras.It 56*2fe8fb19SBen GrasThe child process has its own copy of the parent's descriptors. 57*2fe8fb19SBen GrasThese descriptors reference the same underlying objects, so that, 58*2fe8fb19SBen Grasfor instance, file pointers in file objects are shared between 59*2fe8fb19SBen Grasthe child and the parent, so that an 60*2fe8fb19SBen Gras.Xr lseek 2 61*2fe8fb19SBen Grason a descriptor in the child process can affect a subsequent 62*2fe8fb19SBen Gras.Xr read 2 63*2fe8fb19SBen Grasor 64*2fe8fb19SBen Gras.Xr write 2 65*2fe8fb19SBen Grasby the parent. 66*2fe8fb19SBen GrasThis descriptor copying is also used by the shell to 67*2fe8fb19SBen Grasestablish standard input and output for newly created processes 68*2fe8fb19SBen Grasas well as to set up pipes. 69*2fe8fb19SBen Gras.It 70*2fe8fb19SBen GrasThe child process' resource utilizations 71*2fe8fb19SBen Grasare set to 0; see 72*2fe8fb19SBen Gras.Xr setrlimit 2 . 73*2fe8fb19SBen Gras.El 74*2fe8fb19SBen Gras.Pp 75*2fe8fb19SBen GrasIn general, the child process should call 76*2fe8fb19SBen Gras.Xr _exit 2 77*2fe8fb19SBen Grasrather than 78*2fe8fb19SBen Gras.Xr exit 3 . 79*2fe8fb19SBen GrasOtherwise, any stdio buffers that exist both in the parent and child 80*2fe8fb19SBen Graswill be flushed twice. 81*2fe8fb19SBen GrasSimilarly, 82*2fe8fb19SBen Gras.Xr _exit 2 83*2fe8fb19SBen Grasshould be used to prevent 84*2fe8fb19SBen Gras.Xr atexit 3 85*2fe8fb19SBen Grasroutines from being called twice (once in the parent and once in the child). 86*2fe8fb19SBen Gras.Pp 87*2fe8fb19SBen GrasIn case of a threaded program, only the thread calling 88*2fe8fb19SBen Gras.Fn fork 89*2fe8fb19SBen Grasis still running in the child processes. 90*2fe8fb19SBen Gras.Pp 91*2fe8fb19SBen GrasChild processes of a threaded program have additional restrictions, 92*2fe8fb19SBen Grasa child must only call functions that are async-signal-safe. 93*2fe8fb19SBen GrasVery few functions are asynchronously safe and applications should 94*2fe8fb19SBen Grasmake sure they call 95*2fe8fb19SBen Gras.Xr exec 3 96*2fe8fb19SBen Grasas soon as possible. 97*2fe8fb19SBen Gras.Sh RETURN VALUES 98*2fe8fb19SBen GrasUpon successful completion, 99*2fe8fb19SBen Gras.Fn fork 100*2fe8fb19SBen Grasreturns a value 101*2fe8fb19SBen Grasof 0 to the child process and returns the process ID of the child 102*2fe8fb19SBen Grasprocess to the parent process. 103*2fe8fb19SBen GrasOtherwise, a value of \-1 is returned to the parent process, no 104*2fe8fb19SBen Graschild process is created, and the global variable 105*2fe8fb19SBen Gras.Va errno 106*2fe8fb19SBen Grasis set to indicate the error. 107*2fe8fb19SBen Gras.Sh ERRORS 108*2fe8fb19SBen Gras.Fn fork 109*2fe8fb19SBen Graswill fail and no child process will be created if: 110*2fe8fb19SBen Gras.Bl -tag -width [EAGAIN] 111*2fe8fb19SBen Gras.It Bq Er EAGAIN 112*2fe8fb19SBen GrasThe system-imposed limit on the total 113*2fe8fb19SBen Grasnumber of processes under execution would be exceeded. 114*2fe8fb19SBen GrasThis limit is configuration-dependent. 115*2fe8fb19SBen Gras.It Bq Er EAGAIN 116*2fe8fb19SBen GrasThe limit 117*2fe8fb19SBen Gras.Dv RLIMIT_NPROC 118*2fe8fb19SBen Grason the total number of 119*2fe8fb19SBen Grasprocesses under execution by this user id would be exceeded. 120*2fe8fb19SBen Gras.It Bq Er ENOMEM 121*2fe8fb19SBen GrasThere is insufficient swap space for the new process. 122*2fe8fb19SBen Gras.El 123*2fe8fb19SBen Gras.Sh SEE ALSO 124*2fe8fb19SBen Gras.Xr execve 2 , 125*2fe8fb19SBen Gras.Xr setrlimit 2 , 126*2fe8fb19SBen Gras.Xr vfork 2 , 127*2fe8fb19SBen Gras.Xr wait 2 , 128*2fe8fb19SBen Gras.Xr pthread_atfork 3 129*2fe8fb19SBen Gras.Sh STANDARDS 130*2fe8fb19SBen GrasThe 131*2fe8fb19SBen Gras.Fn fork 132*2fe8fb19SBen Grasfunction conforms to 133*2fe8fb19SBen Gras.St -p1003.1-90 . 134*2fe8fb19SBen Gras.Sh HISTORY 135*2fe8fb19SBen GrasA 136*2fe8fb19SBen Gras.Fn fork 137*2fe8fb19SBen Grassystem call appeared in 138*2fe8fb19SBen Gras.At v6 . 139