aboutsummaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2021-08-05 16:03:03 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2021-08-08 19:38:59 +0000
commit2a51e8823a60180feb534176bc41d5d10e2a01b1 (patch)
tree7480d387501d0e58bc4b6b5879a9da4fcbc2df88 /lib/libc
parent47363e99d3d312c510902b68d5cf3094ddc7bb76 (diff)
downloadsrc-2a51e8823a60180feb534176bc41d5d10e2a01b1.tar.gz
src-2a51e8823a60180feb534176bc41d5d10e2a01b1.zip
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
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/sys/fork.219
1 files changed, 18 insertions, 1 deletions
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;