waitpid returns pid=0 and WIFEXITED=1 how to get pid?

c, linux, pid, waitpid

Solution

The status is meaningless if the pid returned was 0. Think about it. A return of 0 means you have one or more children that have yet to change state. What would the state of a child that has yet to change state be? If there were multiple children, which child is the status code referencing?

This is analogous to checking errno on a successful call. Anything from a previous call can be in errno but it has nothing to do with the most recent successful call because errno is usually not set on success.

Problem

Steps: Fork and start process in a different program group Stop process with SIGTSTP Restart process with SIGCONT Process ends Problem: The SIGCHLD handler has: ``` waitpid(-1, &status, WNOHANG | WUNTRACED); ``` upon return pid=0 and WIFEXITED=1 so, the process exited, but I can't get the pid? I need the pid. From the man page: "if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned" But it seems the status has changed to exited.

Original source