本文介绍了在epoll_wait SIGCHLD没有抓到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的叉信号的行为。我对派生的子写了一个小程序,以赶上与SIGCHLD但epoll_wait当我做了一个杀-9,我没有得到任何信号和孩子正处于倒闭状态(我有一个处理程序,做一个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()).

下面是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;
            }
        }
    }
}

更新:

用于一个EPOLL_CTL_MOD没有首先做一个插件(EPOLL_CTL_ADD)。我改变了这一切之后,它的工作就像一个魅力。

Used a EPOLL_CTL_MOD without first doing an add (EPOLL_CTL_ADD). After I changed that, it worked like a charm.

推荐答案

SIGCHLD的默认配置是被忽略。阻断信号不会改变它只是提供了配置 signalfd 有机会同步处理。

The default disposition of SIGCHLD is to be ignored. Blocking the signal won't change the disposition it just gives signalfd the opportunity to process it synchronously.

添加SIGCHLD的信号处理与信号的sigaction 。它没有,因为它不会被执行做任何事情。由于SIGCHLD受阻, signalfd 将消耗和处理程序之前处理它。

Add a signal handler for SIGCHLD with signal or sigaction. 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.

这篇关于在epoll_wait SIGCHLD没有抓到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:39