Reputation: 907
I have a handler for SIGCHLD
in a process that spawns off many children.
This code is inside my handler and pid
is always 1
. I've read that pid
should be the child process id
. Why do I keep getting back 1
?
int status;
pid_t pid;
while(pid = waitpid(-1, &status, WNOHANG) > 0)
{
if (WIFEXITED(status))
printf("Child %ld: Ended properly and returned %d\n", pid, WEXITSTATUS(status));
else
printf("Child crashed\n");
}
Code to initialize the handler.
void initSIGCHLDSignalHandler()
{
struct sigaction act;
memset(&act,'\0',sizeof(struct sigaction));
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
act.sa_handler = SIGCHLD_SignalHandler;
sigaction(SIGCHLD, &act, NULL);
}
Upvotes: 0
Views: 175
Reputation: 1761
I imagine you want
while((pid = waitpid(-1, &status, WNOHANG)) > 0)
since right now all you're getting is "true". (Your compiler probably warns about this, so be reading your compiler warnings.)
Upvotes: 1