From owner-dev-commits-src-all@freebsd.org Sun Aug 8 19:51:30 2021 Return-Path: Delivered-To: dev-commits-src-all@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 08C26662B56; Sun, 8 Aug 2021 19:51:30 +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 4GjVG96tNHz3hL6; Sun, 8 Aug 2021 19:51:29 +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 D4C341AB7C; Sun, 8 Aug 2021 19:51:29 +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 178JpTHC030425; Sun, 8 Aug 2021 19:51:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 178JpTuo030424; Sun, 8 Aug 2021 19:51:29 GMT (envelope-from git) Date: Sun, 8 Aug 2021 19:51:29 GMT Message-Id: <202108081951.178JpTuo030424@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 2a51e8823a60 - main - fork(2): comment about doubtful use of stdio and exit(3) in example MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2a51e8823a60180feb534176bc41d5d10e2a01b1 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Aug 2021 19:51:30 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=2a51e8823a60180feb534176bc41d5d10e2a01b1 commit 2a51e8823a60180feb534176bc41d5d10e2a01b1 Author: Konstantin Belousov AuthorDate: 2021-08-05 16:03:03 +0000 Commit: Konstantin Belousov CommitDate: 2021-08-08 19:38:59 +0000 fork(2): comment about doubtful use of stdio and exit(3) in example Add fflush(stdout) as the common idiom. Explain the need to use exit() but advise against it. Reviewed by: emaste, markj Sponsored by: The FreeBSD Foundation MFC after: 3 days Differential revision: https://reviews.freebsd.org/D31425 --- lib/libc/sys/fork.2 | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/libc/sys/fork.2 b/lib/libc/sys/fork.2 index d841b0bc38e1..dbde8f5275aa 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 August 2, 2021 +.Dd August 5, 2021 .Dt FORK 2 .Os .Sh NAME @@ -172,11 +172,28 @@ main(void) { pid_t pid; + /* + * If child is expected to use stdio(3), state of + * the reused io streams must be synchronized between + * parent and child, to avoid double output and other + * possible issues. + */ + fflush(stdout); + switch (pid = fork()) { case -1: err(1, "Failed to fork"); case 0: printf("Hello from child process!\en"); + + /* + * Since we wrote into stdout, child needs to use + * exit(3) and not _exit(2). This causes handlers + * registered with atexit(3) to be called twice, + * once in parent, and once in the child. If such + * behavior is undesirable, consider + * terminating child with _exit(2) or _Exit(3). + */ exit(0); default: break;