我遇到了一个SIGPIPE崩溃问题,我想记录它并尝试存在。
但我无法通过以下代码捕捉到SIGPIPE。
我尝试使用“杀死-s信号处理”来验证我的代码,它与信号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;
}我的环境是Ubuntu12.04LTS
发布于 2014-12-22 11:51:46
这个完整的代码示例确实适用于杀害-13。我没有Ubuntu12.04LTS在这里测试它,但它是好的,在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);
}
}https://stackoverflow.com/questions/27600434
复制相似问题