Не удается поймать сигнал SIGPIPE в Ubuntu

Я столкнулся с проблемой сбоя 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.


person Evan Lin    schedule 22.12.2014    source источник


Ответы (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);
    }
}
person Vorsprung    schedule 22.12.2014
comment
Привет Vorsprung, Спасибо. Кажется, что-то случилось в файле fooServer. Но поскольку fooServer является сторонней библиотекой, есть ли у вас какие-либо предложения, которые я мог бы поймать такого рода сигнал за пределами fooServer? - person Evan Lin; 23.12.2014