As far as I know if waitpid returns -1 then it is error condition. How it possible to get success (EXIT_SUCCUSS) from child process in WEXITSTATUS(childStatus)?
What is the difference between childStatus in waitpid & return value from WEXITSTATUS(childStatus)? Does it same?
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
printf("return value = %d", returnValue);
printf("return value = %d", childStatus);
if (WIFEXITED(childStatus))
{
printf("Exit Code: _ WEXITSTATUS(childStatus)") ;
//Proceed with other calculation.
}
When using the option WNOHANG
, I would expect that most of the time waitpid
will return -1
, with errno
set to ECHILD
.
In any case, whenever waitpid
does return -1
, you shouldn't be looking at childStatus
, which (for all I know) could be just garbage. Instead, look at errno
, and handle it appropriately.
Otherwise, your code seems to be ok, as far as it goes, and you should be able to extract a 0
or EXIT_SUCCESS
from childStatus
.
The man page for waitpid
suggests the following sample code:
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
although it might be a good idea to add a final else printf("oops?\n")
statement to this.