[英]Cannot catch SIGPIPE signal in 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
這個完整的代碼示例確實使用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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.