When I wait on a specific running process group that is a child process, WIFEXITED returns true saying the process exited? Is this the way it works? Seems there is something I am not understanding....
if ( waitpid(-pgid, &pstatus, WUNTRACED|WNOHANG ) == -1)
perror("Wait error");
if ( WIFEXITED(pstatus) ) {
strncpy(buf, "Exited", buf_size);
return 0;
WIFEXITED and WEXITSTATUS are two of the options which can be used to know the exit status of the child. WIFEXITED(status) : returns true if the child terminated normally. WEXITSTATUS(status) : returns the exit status of the child.
Returned value If successful, waitpid() returns a value of the process (usually a child) whose status information has been obtained. If WNOHANG was given, and if there is at least one process (usually a child) whose status information is not available, waitpid() returns 0.
From the linux manual : The pid parameter specifies the set of child processes for which to wait. If pid is -1, the call waits for any child process.
It blocks the calling process until a nominated child process exits (or makes some other transition such as being stopped.) Typically you will use waitpid rather than generic wait when you may have more than one process and only care about one.
As you specified WNOHANG
I think waitpid
is returning 0
and pstatus
has the value it had before so WIFEXITED
is not working with updated data.
if
WNOHANG
was specified and one or more child(ren) specified bypid
exist, but have not yet changed state, then0
is returned.
waitpid
returns the reaped child pid if it successfully reaps a child. When used with WNOHANG
, it immediately returns 0 if no children have exited. Thus, you need to check whether the return value is 0 or a pid before you inspect status
. See here for details:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With