xref: /freebsd/lib/libsys/fork.2 (revision 566c039d1e7555343fcf6439a10e56f5a632c0fe)
18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
288269e767SBrooks Davis.Dd August 5, 2021
298269e767SBrooks Davis.Dt FORK 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm fork
338269e767SBrooks Davis.Nd create a new process
348269e767SBrooks Davis.Sh LIBRARY
358269e767SBrooks Davis.Lb libc
368269e767SBrooks Davis.Sh SYNOPSIS
378269e767SBrooks Davis.In unistd.h
388269e767SBrooks Davis.Ft pid_t
398269e767SBrooks Davis.Fn fork void
408269e767SBrooks Davis.Ft pid_t
418269e767SBrooks Davis.Fn _Fork void
428269e767SBrooks Davis.Sh DESCRIPTION
438269e767SBrooks DavisThe
448269e767SBrooks Davis.Fn fork
458269e767SBrooks Davisfunction causes creation of a new process.
468269e767SBrooks DavisThe new process (child process) is an exact copy of the
478269e767SBrooks Daviscalling process (parent process) except for the following:
488269e767SBrooks Davis.Bl -bullet -offset indent
498269e767SBrooks Davis.It
508269e767SBrooks DavisThe child process has a unique process ID.
518269e767SBrooks Davis.It
528269e767SBrooks DavisThe child process has a different parent
538269e767SBrooks Davisprocess ID (i.e., the process ID of the parent process).
548269e767SBrooks Davis.It
558269e767SBrooks DavisThe child process has its own copy of the parent's descriptors,
568269e767SBrooks Davisexcept for descriptors returned by
578269e767SBrooks Davis.Xr kqueue 2 ,
588269e767SBrooks Daviswhich are not inherited from the parent process.
598269e767SBrooks DavisThese descriptors reference the same underlying objects, so that,
608269e767SBrooks Davisfor instance, file pointers in file objects are shared between
618269e767SBrooks Davisthe child and the parent, so that an
628269e767SBrooks Davis.Xr lseek 2
638269e767SBrooks Davison a descriptor in the child process can affect a subsequent
648269e767SBrooks Davis.Xr read 2
658269e767SBrooks Davisor
668269e767SBrooks Davis.Xr write 2
678269e767SBrooks Davisby the parent.
688269e767SBrooks DavisThis descriptor copying is also used by the shell to
698269e767SBrooks Davisestablish standard input and output for newly created processes
708269e767SBrooks Davisas well as to set up pipes.
718269e767SBrooks Davis.It
728269e767SBrooks DavisThe child process' resource utilizations
738269e767SBrooks Davisare set to 0; see
748269e767SBrooks Davis.Xr setrlimit 2 .
758269e767SBrooks Davis.It
768269e767SBrooks DavisAll interval timers are cleared; see
778269e767SBrooks Davis.Xr setitimer 2 .
788269e767SBrooks Davis.It
798269e767SBrooks DavisThe robust mutexes list (see
808269e767SBrooks Davis.Xr pthread_mutexattr_setrobust 3 )
818269e767SBrooks Davisis cleared for the child.
828269e767SBrooks Davis.It
838269e767SBrooks DavisThe atfork handlers established with the
848269e767SBrooks Davis.Xr pthread_atfork 3
858269e767SBrooks Davisfunction are called as appropriate before fork in the parent process,
868269e767SBrooks Davisand after the child is created, in parent and child.
878269e767SBrooks Davis.It
888269e767SBrooks DavisThe child process has only one thread,
898269e767SBrooks Daviscorresponding to the calling thread in the parent process.
908269e767SBrooks DavisIf the process has more than one thread,
918269e767SBrooks Davislocks and other resources held by the other threads are not released
928269e767SBrooks Davisand therefore only async-signal-safe functions
938269e767SBrooks Davis(see
948269e767SBrooks Davis.Xr sigaction 2 )
958269e767SBrooks Davisare guaranteed to work in the child process until a call to
968269e767SBrooks Davis.Xr execve 2
978269e767SBrooks Davisor a similar function.
988269e767SBrooks DavisThe
998269e767SBrooks Davis.Fx
1008269e767SBrooks Davisimplementation of
1018269e767SBrooks Davis.Fn fork
1028269e767SBrooks Davisprovides a usable
1038269e767SBrooks Davis.Xr malloc 3 ,
1048269e767SBrooks Davisand
1058269e767SBrooks Davis.Xr rtld 1
1068269e767SBrooks Davisservices in the child process.
1078269e767SBrooks Davis.El
1088269e767SBrooks Davis.Pp
1098269e767SBrooks DavisThe
1108269e767SBrooks Davis.Fn fork
1118269e767SBrooks Davisfunction is not async-signal safe and creates a cancellation point
1128269e767SBrooks Davisin the parent process.
1138269e767SBrooks DavisIt cannot be safely used from signal handlers, and the atfork handlers
1148269e767SBrooks Davisestablished by
1158269e767SBrooks Davis.Xr pthread_atfork 3
1168269e767SBrooks Davisdo not need to be async-signal safe either.
1178269e767SBrooks Davis.Pp
1188269e767SBrooks DavisThe
1198269e767SBrooks Davis.Fn _Fork
1208269e767SBrooks Davisfunction creates a new process, similarly to
1218269e767SBrooks Davis.Fn fork ,
1228269e767SBrooks Davisbut it is async-signal safe.
1238269e767SBrooks Davis.Fn _Fork
1248269e767SBrooks Davisdoes not call atfork handlers, and does not create a cancellation point.
1258269e767SBrooks DavisIt can be used safely from signal handlers, but then no userspace
1268269e767SBrooks Davisservices (
1278269e767SBrooks Davis.Xr malloc 3
1288269e767SBrooks Davisor
1298269e767SBrooks Davis.Xr rtld 1 )
1308269e767SBrooks Davisare available in the child if forked from multi-threaded parent.
1318269e767SBrooks DavisIn particular, if using dynamic linking, all dynamic symbols used by the
1328269e767SBrooks Davischild after
1338269e767SBrooks Davis.Fn _Fork
1348269e767SBrooks Davismust be pre-resolved.
1358269e767SBrooks DavisNote: resolving can be done globally by specifying the
1368269e767SBrooks Davis.Ev LD_BIND_NOW
1378269e767SBrooks Davisenvironment variable to the dynamic linker, or per-binary by passing the
1388269e767SBrooks Davis.Fl z Ar now
1398269e767SBrooks Davisoption to the static linker
1408269e767SBrooks Davis.Xr ld 1 ,
1418269e767SBrooks Davisor by using each symbol before the
1428269e767SBrooks Davis.Fn _Fork
1438269e767SBrooks Daviscall to force the binding.
1448269e767SBrooks Davis.Sh RETURN VALUES
1458269e767SBrooks DavisUpon successful completion,
1468269e767SBrooks Davis.Fn fork
1478269e767SBrooks Davisand
1488269e767SBrooks Davis.Fn _Fork
1498269e767SBrooks Davisreturn a value
1508269e767SBrooks Davisof 0 to the child process and return the process ID of the child
1518269e767SBrooks Davisprocess to the parent process.
1528269e767SBrooks DavisOtherwise, a value of -1 is returned
1538269e767SBrooks Davisto the parent process, no child process is created, and the global
1548269e767SBrooks Davisvariable
1558269e767SBrooks Davis.Va errno
1568269e767SBrooks Davisis set to indicate the error.
1578269e767SBrooks Davis.Sh EXAMPLES
1588269e767SBrooks DavisThe following example shows a common pattern of how
1598269e767SBrooks Davis.Fn fork
1608269e767SBrooks Davisis used in practice.
1618269e767SBrooks Davis.Bd -literal -offset indent
1628269e767SBrooks Davis#include <err.h>
1638269e767SBrooks Davis#include <stdio.h>
1648269e767SBrooks Davis#include <stdlib.h>
1658269e767SBrooks Davis#include <unistd.h>
1668269e767SBrooks Davis
1678269e767SBrooks Davisint
1688269e767SBrooks Davismain(void)
1698269e767SBrooks Davis{
1708269e767SBrooks Davis	pid_t pid;
1718269e767SBrooks Davis
1728269e767SBrooks Davis	/*
1738269e767SBrooks Davis	 * If child is expected to use stdio(3), state of
1748269e767SBrooks Davis	 * the reused io streams must be synchronized between
1758269e767SBrooks Davis	 * parent and child, to avoid double output and other
1768269e767SBrooks Davis	 * possible issues.
1778269e767SBrooks Davis	 */
1788269e767SBrooks Davis	fflush(stdout);
1798269e767SBrooks Davis
1808269e767SBrooks Davis	switch (pid = fork()) {
1818269e767SBrooks Davis	case -1:
1828269e767SBrooks Davis		err(1, "Failed to fork");
1838269e767SBrooks Davis	case 0:
1848269e767SBrooks Davis		printf("Hello from child process!\en");
1858269e767SBrooks Davis
1868269e767SBrooks Davis		/*
1878269e767SBrooks Davis		 * Since we wrote into stdout, child needs to use
1888269e767SBrooks Davis		 * exit(3) and not _exit(2).  This causes handlers
1898269e767SBrooks Davis		 * registered with atexit(3) to be called twice,
1908269e767SBrooks Davis		 * once in parent, and once in the child.  If such
1918269e767SBrooks Davis		 * behavior is undesirable, consider
1928269e767SBrooks Davis		 * terminating child with _exit(2) or _Exit(3).
1938269e767SBrooks Davis		 */
1948269e767SBrooks Davis		exit(0);
1958269e767SBrooks Davis	default:
1968269e767SBrooks Davis		break;
1978269e767SBrooks Davis	}
1988269e767SBrooks Davis
1998269e767SBrooks Davis	printf("Hello from parent process (child's PID: %d)!\en", pid);
2008269e767SBrooks Davis
2018269e767SBrooks Davis	return (0);
2028269e767SBrooks Davis}
2038269e767SBrooks Davis.Ed
2048269e767SBrooks Davis.Pp
2058269e767SBrooks DavisThe output of such a program is along the lines of:
2068269e767SBrooks Davis.Bd -literal -offset indent
2078269e767SBrooks DavisHello from parent process (child's PID: 27804)!
2088269e767SBrooks DavisHello from child process!
2098269e767SBrooks Davis.Ed
2108269e767SBrooks Davis.Sh ERRORS
2118269e767SBrooks DavisThe
2128269e767SBrooks Davis.Fn fork
2138269e767SBrooks Davissystem call will fail and no child process will be created if:
2148269e767SBrooks Davis.Bl -tag -width Er
2158269e767SBrooks Davis.It Bq Er EAGAIN
2168269e767SBrooks DavisThe system-imposed limit on the total
2178269e767SBrooks Davisnumber of processes under execution would be exceeded.
2188269e767SBrooks DavisThe limit is given by the
2198269e767SBrooks Davis.Xr sysctl 3
2208269e767SBrooks DavisMIB variable
2218269e767SBrooks Davis.Dv KERN_MAXPROC .
2228269e767SBrooks Davis(The limit is actually ten less than this
2238269e767SBrooks Davisexcept for the super user).
2248269e767SBrooks Davis.It Bq Er EAGAIN
2258269e767SBrooks DavisThe user is not the super user, and
2268269e767SBrooks Davisthe system-imposed limit
2278269e767SBrooks Davison the total number of
2288269e767SBrooks Davisprocesses under execution by a single user would be exceeded.
2298269e767SBrooks DavisThe limit is given by the
2308269e767SBrooks Davis.Xr sysctl 3
2318269e767SBrooks DavisMIB variable
2328269e767SBrooks Davis.Dv KERN_MAXPROCPERUID .
2338269e767SBrooks Davis.It Bq Er EAGAIN
2348269e767SBrooks DavisThe user is not the super user, and
2358269e767SBrooks Davisthe soft resource limit corresponding to the
2368269e767SBrooks Davis.Fa resource
2378269e767SBrooks Davisargument
2388269e767SBrooks Davis.Dv RLIMIT_NPROC
2398269e767SBrooks Daviswould be exceeded (see
2408269e767SBrooks Davis.Xr getrlimit 2 ) .
2418269e767SBrooks Davis.It Bq Er ENOMEM
2428269e767SBrooks DavisThere is insufficient swap space for the new process.
2438269e767SBrooks Davis.El
2448269e767SBrooks Davis.Sh SEE ALSO
2458269e767SBrooks Davis.Xr execve 2 ,
2468269e767SBrooks Davis.Xr rfork 2 ,
2478269e767SBrooks Davis.Xr setitimer 2 ,
2488269e767SBrooks Davis.Xr setrlimit 2 ,
2498269e767SBrooks Davis.Xr sigaction 2 ,
2508269e767SBrooks Davis.Xr vfork 2 ,
2518269e767SBrooks Davis.Xr wait 2 ,
2528269e767SBrooks Davis.Xr pthread_atfork 3
253*566c039dSEd Maste.Sh STANDARDS
254*566c039dSEd MasteThe
255*566c039dSEd Maste.Fn fork
256*566c039dSEd Masteand
257*566c039dSEd Maste.Fn _Fork
258*566c039dSEd Mastefunctions conform to
259*566c039dSEd Maste.St -p1003.1-2024 .
2608269e767SBrooks Davis.Sh HISTORY
2618269e767SBrooks DavisThe
2628269e767SBrooks Davis.Fn fork
2638269e767SBrooks Davisfunction appeared in
2648269e767SBrooks Davis.At v1 .
2658269e767SBrooks DavisThe
2668269e767SBrooks Davis.Fn _Fork
2678269e767SBrooks Davisfunction appeared in
2688269e767SBrooks Davis.Fx 13.1 .
269