Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/sys/fork.2
Show All 22 Lines | |||||||||||
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||||||||
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||||||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||||||||
.\" SUCH DAMAGE. | .\" SUCH DAMAGE. | ||||||||||
.\" | .\" | ||||||||||
.\" @(#)fork.2 8.1 (Berkeley) 6/4/93 | .\" @(#)fork.2 8.1 (Berkeley) 6/4/93 | ||||||||||
.\" $FreeBSD$ | .\" $FreeBSD$ | ||||||||||
.\" | .\" | ||||||||||
.Dd December 1, 2017 | .Dd April 17, 2021 | ||||||||||
.Dt FORK 2 | .Dt FORK 2 | ||||||||||
.Os | .Os | ||||||||||
.Sh NAME | .Sh NAME | ||||||||||
.Nm fork | .Nm fork | ||||||||||
.Nd create a new process | .Nd create a new process | ||||||||||
.Sh LIBRARY | .Sh LIBRARY | ||||||||||
.Lb libc | .Lb libc | ||||||||||
.Sh SYNOPSIS | .Sh SYNOPSIS | ||||||||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | |||||||||||
returns a value | returns a value | ||||||||||
of 0 to the child process and returns the process ID of the child | of 0 to the child process and returns the process ID of the child | ||||||||||
process to the parent process. | process to the parent process. | ||||||||||
Otherwise, a value of -1 is returned | Otherwise, a value of -1 is returned | ||||||||||
to the parent process, no child process is created, and the global | to the parent process, no child process is created, and the global | ||||||||||
variable | variable | ||||||||||
.Va errno | .Va errno | ||||||||||
is set to indicate the error. | is set to indicate the error. | ||||||||||
.Sh EXAMPLES | |||||||||||
The following example shows a common pattern of how | |||||||||||
.Fn fork | |||||||||||
is used in practice. | |||||||||||
.Bd -literal -offset indent | |||||||||||
#include <err.h> | |||||||||||
#include <stdio.h> | |||||||||||
#include <stdlib.h> | |||||||||||
#include <unistd.h> | |||||||||||
int | |||||||||||
main(void) | |||||||||||
{ | |||||||||||
yuripvUnsubmitted Done Inline Actions
yuripv: | |||||||||||
pid_t pid; | |||||||||||
switch (pid = fork()) { | |||||||||||
case -1: | |||||||||||
Done Inline Actionsi'd use err(3) instead, but that's just my preference yuripv: i'd use err(3) instead, but that's just my preference | |||||||||||
err(1, "Failed to fork"); | |||||||||||
case 0: | |||||||||||
printf("Hello from child process!\en"); | |||||||||||
exit(0); | |||||||||||
default: | |||||||||||
break; | |||||||||||
} | |||||||||||
printf("Hello from parent process (child's PID: %d)!\en", pid); | |||||||||||
return (0); | |||||||||||
} | |||||||||||
.Ed | |||||||||||
.Pp | |||||||||||
The output of such a program is along the lines of: | |||||||||||
.Bd -literal -offset indent | |||||||||||
Hello from parent (child's PID: 27804)! | |||||||||||
Hello from child process! | |||||||||||
.Ed | |||||||||||
.Sh ERRORS | .Sh ERRORS | ||||||||||
The | The | ||||||||||
.Fn fork | .Fn fork | ||||||||||
system call will fail and no child process will be created if: | system call will fail and no child process will be created if: | ||||||||||
.Bl -tag -width Er | .Bl -tag -width Er | ||||||||||
.It Bq Er EAGAIN | .It Bq Er EAGAIN | ||||||||||
The system-imposed limit on the total | The system-imposed limit on the total | ||||||||||
number of processes under execution would be exceeded. | number of processes under execution would be exceeded. | ||||||||||
Show All 39 Lines |