[英]SIGCHLD not caught in epoll_wait?
I wanted to understand the behavior of signals on fork. 我想了解分叉信号的行为。 I wrote a small program to catch SIGCHLD with epoll_wait but when I do a "kill -9" on the forked child, I am not getting any signal and the child is in defunct state (I have a handler that does a wait()).
我编写了一个小程序来用epoll_wait捕获SIGCHLD,但是当我对派生的孩子执行“ kill -9”操作时,我没有收到任何信号,并且孩子处于已退出状态(我有一个处理程序,它执行wait()) 。
Here is the code. 这是代码。
//....
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
signal_fd = signalfd(-1, &mask, 0);
memset(&tev, 0, sizeof(tev));
tev.events = EPOLLIN | EPOLLONESHOT;
tev.data.fd = signal_fd;
epoll_ctl(efd_, EPOLL_CTL_MOD, signal_fd, &tev);
while (1) {
child_pid_ = fork();
if (child_pid_ == 0) {
close(signal_fd);
close(efd_);
make_grand_child(); //just sleeps in while(1) and never returns.
} else {
memset(&event, 0, sizeof(event));
while (1) {
epoll_wait(efd_, &event, 1, -1);
deliver_events = (event.events & EPOLLERR|EPOLLHUP|EPOLLIN|EPOLLONESHOT);
if (deliver_events) {
parent_sig_handler(SIGCHLD);
break;
}
}
}
}
UPDATE: 更新:
Used a EPOLL_CTL_MOD without first doing an add (EPOLL_CTL_ADD). 使用EPOLL_CTL_MOD而不先进行添加(EPOLL_CTL_ADD)。 After I changed that, it worked like a charm.
在我更改了它之后,它就像是一种魅力。
The default disposition of SIGCHLD is to be ignored. SIGCHLD的默认配置将被忽略。 Blocking the signal won't change the disposition it just gives
signalfd
the opportunity to process it synchronously. 阻塞信号不会改变其配置,而只是给
signalfd
同步处理它的机会。
Add a signal handler for SIGCHLD with signal
or sigaction
. 使用
signal
或sigaction
为SIGCHLD添加信号处理程序。 It doesn't have to do anything since it won't be executed. 它不需要执行任何操作,因为它不会被执行。 Because SIGCHLD is blocked,
signalfd
will consume and process it before the handler. 由于SIGCHLD被阻止,
signalfd
将在处理程序之前使用并处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.