xref: /openbsd/lib/libc/sys/fork.2 (revision db3296cf)
1.\"	$OpenBSD: fork.2,v 1.13 2003/06/02 20:18:39 millert Exp $
2.\"	$NetBSD: fork.2,v 1.6 1995/02/27 12:32:36 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"	@(#)fork.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd June 4, 1993
34.Dt FORK 2
35.Os
36.Sh NAME
37.Nm fork
38.Nd create a new process
39.Sh SYNOPSIS
40.Fd #include <sys/types.h>
41.Fd #include <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.It
74All interval timers are cleared; see
75.Xr setitimer 2 .
76.El
77.Pp
78In general, the child process should call
79.Xr _exit 2
80rather than
81.Xr exit 3 .
82Otherwise, any stdio buffers that exist both in the parent and child
83will be flushed twice.
84Similarly,
85.Xr _exit 2
86should be used to prevent
87.Xr atexit 3
88routines from being called twice (once in the parent and once in the child).
89.Sh RETURN VALUES
90Upon successful completion,
91.Fn fork
92returns a value
93of 0 to the child process and returns the process ID of the child
94process to the parent process.
95Otherwise, a value of \-1 is returned to the parent process,
96no child process is created, and the global variable
97.Va errno
98is set to indicate the error.
99.Sh ERRORS
100.Fn fork
101will fail and no child process will be created if:
102.Bl -tag -width [EAGAIN]
103.It Bq Er EAGAIN
104The system-imposed limit on the total
105number of processes under execution would be exceeded.
106This limit is configuration-dependent.
107.It Bq Er EAGAIN
108The limit
109.Dv RLIMIT_NPROC
110on the total number of processes under execution by the user ID
111would be exceeded.
112.It Bq Er ENOMEM
113There is insufficient swap space for the new process.
114.El
115.Sh SEE ALSO
116.Xr execve 2 ,
117.Xr setrlimit 2 ,
118.Xr wait 2
119.Sh STANDARDS
120The
121.Fn fork
122function conforms to
123.St -p1003.1-90 .
124.Sh HISTORY
125A
126.Fn fork
127function call appeared in
128.At v2 .
129