1*47208Scael.\" Copyright (c) 1980, 1991 Regents of the University of California. 2*47208Scael.\" All rights reserved. 320026Smckusick.\" 4*47208Scael.\" %sccs.include.redist.man% 520026Smckusick.\" 6*47208Scael.\" @(#)fork.2 6.5 (Berkeley) 03/10/91 7*47208Scael.\" 8*47208Scael.Dd 9*47208Scael.Dt FORK 2 10*47208Scael.Os BSD 4.0 11*47208Scael.Sh NAME 12*47208Scael.Nm fork 13*47208Scael.Nd create a new process 14*47208Scael.Sh SYNOPSIS 15*47208Scael.Fd #include <unistd.h> 16*47208Scael.Ft pid_t 17*47208Scael.Fn fork void 18*47208Scael.Sh DESCRIPTION 19*47208Scael.Fn Fork 2020027Smckusickcauses creation of a new process. 2120027SmckusickThe new process (child process) is an exact copy of the 22*47208Scaelcalling process (parent process) except for the following: 23*47208Scael.Bl -bullet -offset indent 24*47208Scael.It 2520027SmckusickThe child process has a unique process ID. 26*47208Scael.It 27*47208ScaelThe child process has a different parent 28*47208Scaelprocess ID (i.e., the process ID of the parent process). 29*47208Scael.It 3020027SmckusickThe child process has its own copy of the parent's descriptors. 3120027SmckusickThese descriptors reference the same underlying objects, so that, 3220027Smckusickfor instance, file pointers in file objects are shared between 3328072Skarelsthe child and the parent, so that an 34*47208Scael.Xr lseek 2 3520027Smckusickon a descriptor in the child process can affect a subsequent 36*47208Scael.Xr read 3720027Smckusickor 38*47208Scael.Xr write 3920027Smckusickby the parent. 4020027SmckusickThis descriptor copying is also used by the shell to 4120027Smckusickestablish standard input and output for newly created processes 4220027Smckusickas well as to set up pipes. 43*47208Scael.It 44*47208ScaelThe child processes resource utilizations 45*47208Scaelare set to 0; see 46*47208Scael.Xr setrlimit 2 . 47*47208Scael.El 48*47208Scael.Sh RETURN VALUES 49*47208ScaelUpon successful completion, 50*47208Scael.Fn fork 51*47208Scaelreturns a value 5220027Smckusickof 0 to the child process and returns the process ID of the child 53*47208Scaelprocess to the parent process. Otherwise, a value of -1 is returned 5420027Smckusickto the parent process, no child process is created, and the global 55*47208Scaelvariable 56*47208Scael.Va errno 57*47208Scaelis set to indicate the error. 58*47208Scael.Sh ERRORS 59*47208Scael.Fn Fork 60*47208Scaelwill fail and no child process will be created if: 61*47208Scael.Bl -tag -width [EAGAIN] 62*47208Scael.It Bq Er EAGAIN 6328072SkarelsThe system-imposed limit on the total 6420027Smckusicknumber of processes under execution would be exceeded. 6528072SkarelsThis limit is configuration-dependent. 66*47208Scael.It Bq Er EAGAIN 67*47208ScaelThe system-imposed limit 68*47208Scael.Dv MAXUPRC 69*47208Scael.Pq Aq Pa sys/param.h 70*47208Scaelon the total number of 7120027Smckusickprocesses under execution by a single user would be exceeded. 72*47208Scael.It Bq Er ENOMEM 7324439SmckusickThere is insufficient swap space for the new process. 74*47208Scael.El 75*47208Scael.Sh SEE ALSO 76*47208Scael.Xr execve 2 , 77*47208Scael.Xr wait 2 78*47208Scael.Sh HISTORY 79*47208ScaelA 80*47208Scael.Nm 81*47208Scaelfunction call appeared in Version 6 AT&T UNIX. 82