xref: /netbsd-src/lib/libc/sys/fork.2 (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1.\"	$NetBSD: fork.2,v 1.20 2004/05/13 10:20:58 wiz 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 August 11, 2002
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.Sh RETURN VALUES
87Upon successful completion,
88.Fn fork
89returns a value
90of 0 to the child process and returns the process ID of the child
91process to the parent process.
92Otherwise, a value of \-1 is returned to the parent process, no
93child process is created, and the global variable
94.Va errno
95is set to indicate the error.
96.Sh ERRORS
97.Fn fork
98will fail and no child process will be created if:
99.Bl -tag -width [EAGAIN]
100.It Bq Er EAGAIN
101The system-imposed limit on the total
102number of processes under execution would be exceeded.
103This limit is configuration-dependent.
104.It Bq Er EAGAIN
105The limit
106.Dv RLIMIT_NPROC
107on the total number of
108processes under execution by this user id would be exceeded.
109.It Bq Er ENOMEM
110There is insufficient swap space for the new process.
111.El
112.Sh SEE ALSO
113.Xr execve 2 ,
114.Xr setrlimit 2 ,
115.Xr vfork 2 ,
116.Xr wait 2
117.Sh STANDARDS
118The
119.Fn fork
120function conforms to
121.St -p1003.1-90 .
122.Sh HISTORY
123A
124.Fn fork
125system call appeared in
126.At v6 .
127