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