Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Aug 2021 12:39:29 GMT
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 5d7b612949c1 - stable/13 - fork(2): comment about doubtful use of stdio and exit(3) in example
Message-ID:  <202108121239.17CCdTqm059020@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=5d7b612949c1c7f8a284d69e6db48a6c65cd16d3

commit 5d7b612949c1c7f8a284d69e6db48a6c65cd16d3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-08-05 16:03:03 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-08-12 12:37:54 +0000

    fork(2): comment about doubtful use of stdio and exit(3) in example
    
    (cherry picked from commit 2a51e8823a60180feb534176bc41d5d10e2a01b1)
---
 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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202108121239.17CCdTqm059020>