From owner-dev-commits-src-main@freebsd.org Sat Apr 17 21:13:06 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B28985E4003; Sat, 17 Apr 2021 21:13:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FN5QV4f1fz3Ms5; Sat, 17 Apr 2021 21:13:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9271F1F614; Sat, 17 Apr 2021 21:13:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 13HLD6l2014138; Sat, 17 Apr 2021 21:13:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 13HLD6UI014137; Sat, 17 Apr 2021 21:13:06 GMT (envelope-from git) Date: Sat, 17 Apr 2021 21:13:06 GMT Message-Id: <202104172113.13HLD6UI014137@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Piotrowski <0mp@FreeBSD.org> Subject: git: c4207d867c20 - main - fork.2: Add a simple use pattern MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: 0mp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c4207d867c201a726aa3157e09262f72166c89c4 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Apr 2021 21:13:06 -0000 The branch main has been updated by 0mp (doc, ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=c4207d867c201a726aa3157e09262f72166c89c4 commit c4207d867c201a726aa3157e09262f72166c89c4 Author: Mateusz Piotrowski <0mp@FreeBSD.org> AuthorDate: 2021-04-17 21:10:48 +0000 Commit: Mateusz Piotrowski <0mp@FreeBSD.org> CommitDate: 2021-04-17 21:12:06 +0000 fork.2: Add a simple use pattern It seems to be a nice idea to show how fork() is usually used in practice. This may act as a guide to developers who want to quickly recall how to use the fork() function. Reviewed by: bcr, yuripv MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D27626 --- lib/libc/sys/fork.2 | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/libc/sys/fork.2 b/lib/libc/sys/fork.2 index 3e55530827c5..d43ade6a483e 100644 --- a/lib/libc/sys/fork.2 +++ b/lib/libc/sys/fork.2 @@ -28,7 +28,7 @@ .\" @(#)fork.2 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd December 1, 2017 +.Dd April 17, 2021 .Dt FORK 2 .Os .Sh NAME @@ -99,6 +99,42 @@ to the parent process, no child process is created, and the global variable .Va errno 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 +#include +#include +#include + +int +main(void) +{ + pid_t pid; + + switch (pid = fork()) { + case -1: + 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 The .Fn fork