1*61181Sbostic.\" Copyright (c) 1980, 1991, 1993 2*61181Sbostic.\" The Regents of the University of California. All rights reserved. 320026Smckusick.\" 450792Scael.\" %sccs.include.redist.roff% 520026Smckusick.\" 6*61181Sbostic.\" @(#)fork.2 8.1 (Berkeley) 06/04/93 747208Scael.\" 847208Scael.Dd 947208Scael.Dt FORK 2 1050792Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm fork 1347208Scael.Nd create a new process 1447208Scael.Sh SYNOPSIS 1547208Scael.Fd #include <unistd.h> 1647208Scael.Ft pid_t 1747208Scael.Fn fork void 1847208Scael.Sh DESCRIPTION 1947208Scael.Fn Fork 2020027Smckusickcauses creation of a new process. 2120027SmckusickThe new process (child process) is an exact copy of the 2247208Scaelcalling process (parent process) except for the following: 2347208Scael.Bl -bullet -offset indent 2447208Scael.It 2520027SmckusickThe child process has a unique process ID. 2647208Scael.It 2747208ScaelThe child process has a different parent 2847208Scaelprocess ID (i.e., the process ID of the parent process). 2947208Scael.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 3447208Scael.Xr lseek 2 3520027Smckusickon a descriptor in the child process can affect a subsequent 3647208Scael.Xr read 3720027Smckusickor 3847208Scael.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. 4347208Scael.It 4447208ScaelThe child processes resource utilizations 4547208Scaelare set to 0; see 4647208Scael.Xr setrlimit 2 . 4747208Scael.El 4847208Scael.Sh RETURN VALUES 4947208ScaelUpon successful completion, 5047208Scael.Fn fork 5147208Scaelreturns a value 5220027Smckusickof 0 to the child process and returns the process ID of the child 5347208Scaelprocess to the parent process. Otherwise, a value of -1 is returned 5420027Smckusickto the parent process, no child process is created, and the global 5547208Scaelvariable 5647208Scael.Va errno 5747208Scaelis set to indicate the error. 5847208Scael.Sh ERRORS 5947208Scael.Fn Fork 6047208Scaelwill fail and no child process will be created if: 6147208Scael.Bl -tag -width [EAGAIN] 6247208Scael.It Bq Er EAGAIN 6328072SkarelsThe system-imposed limit on the total 6420027Smckusicknumber of processes under execution would be exceeded. 6528072SkarelsThis limit is configuration-dependent. 6647208Scael.It Bq Er EAGAIN 6747208ScaelThe system-imposed limit 6847208Scael.Dv MAXUPRC 6947208Scael.Pq Aq Pa sys/param.h 7047208Scaelon the total number of 7120027Smckusickprocesses under execution by a single user would be exceeded. 7247208Scael.It Bq Er ENOMEM 7324439SmckusickThere is insufficient swap space for the new process. 7447208Scael.El 7547208Scael.Sh SEE ALSO 7647208Scael.Xr execve 2 , 7747208Scael.Xr wait 2 7847208Scael.Sh HISTORY 7947208ScaelA 8050792Scael.Fn fork 2 8150792Scaelfunction call appeared in 8250792Scael.At v6 . 83