signal vs. sigaction and SIGCHLD
Mateusz Guzik
mjguzik at gmail.com
Wed May 22 23:09:47 UTC 2013
On Thu, May 23, 2013 at 08:48:28AM +1000, Noel Hunt wrote:
> I have a small test program which simply forks and execs
> its command line arguments, but after the fork and before
> the exec, it sends a SIGSTOP to the child. The parent then
> sleeps for 3 seconds before exiting. However, a signal
> handler for SIGCHLD has been installed and I was expecting
> the parent to be notified of the SIGSTOP sent to the child,
> but with the `signal' interface this doesn't appear to work.
>
> If I change the code to use `sigaction' and `sigprocmask'
> (to unblock any blocked SIGCHLD), this program works the
> way intended, that is, the signal handler is called:
>
> 12 static void waithandler(int i){
> 13 int pid, cursig;
> 14 int tstat;
> 15
> 16 #ifdef SIGACTION
> 17 pid = waitpid(-1, &tstat, WUNTRACED);
> 18 #else
> 19 pid = wait(&tstat);
> 20 signal(SIGCHLD, waithandler);
You wait differently in case both cases. wait(2) waits for any child to
exit, which clearly is not happening here. If you perform the same
waitpid in both cases it should work fine.
> If I recompile with `#undef SIGACTION', waithandler is not
> called.
>
As noted earlier it is called. You can easly check that by printfing
something at the beginning.
> I should add that even with the sigaction(2) interface, without
> the `sigprocmask' call, it still doesn't work, which suggests
> that SIGCHLD is being blocked.
>
It could be that inherited signal mask blocks it.
--
Mateusz Guzik <mjguzik gmail.com>
More information about the freebsd-hackers
mailing list