gpt4 book ai didi

无法在 Ubuntu 中捕获 SIGPIPE 信号

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:09 27 4
gpt4 key购买 nike

我遇到了 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);
}
}

关于无法在 Ubuntu 中捕获 SIGPIPE 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27600434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com