1.\" $NetBSD: fork1.9,v 1.8 2002/10/14 13:43:23 wiz Exp $ 2.\" 3.\" Copyright (c) 1998 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8.\" NASA Ames Research Center. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the NetBSD 21.\" Foundation, Inc. and its contributors. 22.\" 4. Neither the name of The NetBSD Foundation nor the names of its 23.\" contributors may be used to endorse or promote products derived 24.\" from this software without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36.\" POSSIBILITY OF SUCH DAMAGE. 37.\" 38.Dd January 6, 1998 39.Dt FORK1 9 40.Os 41.Sh NAME 42.Nm fork1 43.Nd create a new process 44.Sh SYNOPSIS 45.Fd #include \*[Lt]sys/types.h\*[Gt] 46.Fd #include \*[Lt]sys/proc.h\*[Gt] 47.Ft int 48.Fn "fork1" "struct proc *p1" "int flags" "register_t *retval" "struct proc **rnewprocp" 49.Sh DESCRIPTION 50.Fn fork1 51creates a new process out of 52.Ar p1 , 53which is assumed to be the current process. 54This function is used primarily to implement the 55.Xr fork 2 56and 57.Xr vfork 2 58system calls. 59.Pp 60The 61.Ar flags 62argument controls the semantics of the fork operation, and is made up of 63the bitwise-OR of the following values: 64.Bl -tag -width FORK_SHAREFILES 65.It FORK_PPWAIT 66The parent process will sleep until the child process successfully calls 67.Xr execve 2 68or exits (either by a call to 69.Xr _exit 2 70or abnormally). 71.It FORK_SHAREVM 72The child process will share the parent's virtual address space. 73If this flag is not specified, the child will get a copy-on-write 74snapshot of the parent's address space. 75.It FORK_SHARECWD 76The child process will share the parent's current directory, root directory, 77and file creation mask. 78.It FORK_SHAREFILES 79The child process will share the parent's file descriptors. 80.It FORK_SHARESIGS 81The child process will share the parent's signal actions. 82.It FORK_NOWAIT 83The child process will at creation time be inherited by the init process. 84.It FORK_CLEANFILES 85The child process will not copy or share the parent's descriptors, but 86rather will start out with a clean set. 87.El 88.Pp 89A 90.Ar flags 91value of 0 indicates a standard fork operation. 92.Pp 93The 94.Ar retval 95argument is provided for the use of system call stubs. 96If 97.Ar retval 98is not NULL, it will hold the following values after successful completion 99of the fork operation: 100.Bl -tag -width retval[0] 101.It Ar retval[0] 102This will contain the pid of the child process. 103.It Ar retval[1] 104In the parent process, this will contain the value 0. 105In the child process, this will contain 1. 106.El 107.Pp 108User level system call stubs typically subtract 1 from 109.Ar retval[1] 110and bitwise-AND it with 111.Ar retval[0] , 112thus returning the pid to the parent process and 0 to the child. 113.Pp 114If 115.Ar rnewprocp 116is not NULL, 117.Ar *rnewprocp 118will point to the newly created process upon successful completion of 119the fork operation. 120.Sh RETURN VALUES 121Upon successful completion of the fork operation, 122.Fn fork1 123returns 0. 124Otherwise, the following error values are returned: 125.Bl -tag -width [EAGAIN] 126.It Bq Er EAGAIN 127The limit on the total number of system processes would be exceeded. 128.It Bq Er EAGAIN 129The limit 130.Dv RLIMIT_NPROC 131on the total number of processes under execution by this 132user id would be exceeded. 133.El 134.Sh SEE ALSO 135.Xr execve 2 , 136.Xr fork 2 , 137.Xr vfork 2 138