Bug report from : Michael Kerrisk , self
(Note that the reply-to line automatically redirects
to yyyyyyyyyyyyyy@xxxxxxxxxxxxx for further discussion on bug reports)
@ page 1 line 49061 section wait() editorial {XSH-wait-example}
Problem:
This is an EXAMPLE for the waitpid() man page. The example is somewhat long,
but it could also be cited as an EXAMPLE for fork() (line 12809).
I haven’t much experience submitting Aardvark’s, hopefully everything here is
done right. Line number references are to TC1.
I have included error handling for fork() and waitpid() – this may or may not
be desired (for space reasons).
Action:
Change
"None"
to the following:
[[
The following example demonstrates the use of waitpid(), fork(), and the macros
used to interpret the status value returned by waitpid() (and wait()). The
code segment creates a child process which does some unspecified work.
Meanwhile the parent loops performing calls to waitpid() to monitor the status
of the child. The loop terminates when child termination is detected.
pid_t child_pid, wpid;
int status;
child_pid = fork();
if (child_pid == -1) { /* fork() failed */
perror("fork");
exit(EXIT_FAILURE);
}
if (child_pid == 0) { /* This is the child */
/* Child does some work and then terminates */
} else { /* This is the parent */
do {
wpid = waitpid(child_pid, &status, WUNTRACED
#ifdef WCONTINUED /* Not all implementations have this */
| WCONTINUED
#endif
);
if (wpid == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("child exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("child killed (signal %d)\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("child stopped (signal %d)\n", WSTOPSIG(status));
#ifdef WIFCONTINUED /* Not all implementations have this */
} else if (WIFCONTINUED(status)) {
printf("child continued\n");
#endif
} else { /* Non-standard case -- may never happen */
printf("Unexpected status (0x%x)\n", status);
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
|