1

我遇到了一个 SIGPIPE 崩溃问题,我想记录它并尝试存在。
但我无法通过后续代码捕获 SIGPIPE。
我尝试使用“kill -s signal process”来验证我的代码,它适用于信号 SIGINT、SIGSTOP、SIGSEGV 和 SIGALRM。
但在 SIGPIPE 上失败了。
如果我在这里错过任何东西,请您告知。

void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    fooServer * pfooServer = new fooServer();
    while(1)
    {
        pfooServer->DoEvents();
    }
    delete pfooServer;
}

我的环境是 Ubuntu 12.04 LTS

4

1 回答 1

1

这个完整的代码示例确实适用于 kill -13。我这里没有 ubuntu 12.04 LTS 来测试它,但在 RHEL 6.5 上没问题。尝试这个。如果它按预期工作,那么您的“fooServer”中肯定有一些东西正在改变 SIGPIPE 行为

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>



void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    while(1)
    {
        sleep(1);
    }
}
于 2014-12-22T11:51:46.663 回答