I tried to find an answer to my question at this post: Signal handler and waitpid coexisting but for me isn't very clear at the moment.
I try to explain my problems:
I'm trying to write a C program that concerns IPC between a parent process and its children. The parent process creates N child processes, then it waits for the termination in a loop like this:
while((pid_term = waitpid(-1, &status, 0)) != -1)
After X seconds, parent receives SIGALRM, then with the sigaction system call, it catches the alarm:
struct sigaction act;
act.sa_handler = alarmHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
But, when the handler function returns, the waitpid also returns -1, and the parent process exits from the while loop above. At the moment, the handler function has an empty body.
I ask myself what happened — why did waitpid()
return -1 after the handler invocation even though most of the children are still alive? Why doesn't this happen with signal()
function?