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
This commit is contained in:
Konstantin Belousov 2021-08-05 19:03:03 +03:00
parent 47363e99d3
commit 2a51e8823a

View File

@ -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;