您的位置:首页 > 理论基础 > 计算机网络

linux下网络程序遭遇SIGPIPE的解决(转)

2016-02-22 16:02 459 查看
问题描述:

我的一个服务器程序, 在Windows下运行正常. 

但当在Linux(centos 6.3)下,进行对端未开启的异常测试时,出现莫名退出 . 最后跟踪到是write调用导致退出. 用gdb执行程序, 退出时提示"Broken pipe".

 

问题分析:

对一个对端已经关闭的socket调用两次write, 第二次将会生成SIGPIPE信号, 该信号默认结束进程.

具体的分析可以结合TCP的”四次握手”关闭. TCP是全双工的信道, 可以看作两条单工信道, TCP连接两端的两个端点各负责一条. 当对端调用close时, 虽然本意是关闭整个两条信道, 但本端只是收到FIN包. 按照TCP协议的语义, 表示对端只是关闭了其所负责的那一条单工信道, 仍然可以继续接收数据. 也就是说, 因为TCP协议的限制, 一个端点无法获知对端已经完全关闭.



对一个已经收到FIN包的socket调用read方法, 如果接收缓冲已空, 则返回0, 这就是常说的表示连接关闭. 但第一次对其调用write方法时, 如果发送缓冲没问题, 会返回正确写入(发送). 但发送的报文会导致对端发送RST报文, 因为对端的socket已经调用了close, 完全关闭, 既不发送, 也不接收数据. 所以, 第二次调用write方法(假设在收到RST之后), 会生成SIGPIPE信号, 导致进程退出.

 

 

解决办法:

为了避免进程退出, 可以捕获SIGPIPE信号, 或者忽略它, 给它设置SIG_IGN信号处理函数:

signal(SIGPIPE, SIG_IGN);

这样, 第二次调用write方法时, 会返回-1, 同时errno置为SIGPIPE. 程序便能知道对端已经关闭.

PS: Linux下的SIGALRM似乎会每1秒钟往后偏移1毫秒, 但Windows下经过测试完全准时, 不差1毫秒.

忽略SIGPIPE信号的方法

struct sigaction sa;

sa.sa_handler = SIG_IGN;//设定接受到指定信号后的动作为忽略

sa.sa_flags = 0;

if (sigemptyset(&sa.sa_mask) == -1 ||   //初始化信号集为空

sigaction(SIGPIPE, &sa, 0) == -1) {   //屏蔽SIGPIPE信号

perror("failed to ignore SIGPIPE; sigaction");

exit(EXIT_FAILURE);

}

 

pthread线程里如何屏蔽SIGPIPE异常
hi.baidu.com/ailacy/blog/item/a7eb65f8b8b55707d8f9fdd5.html
http://bbs2.chinaunix.net/viewthread.php?tid=985166&extra=&page=1
在pthread中,可能会遇到Program received signal SIGPIPE, Broken pipe的问题,解决方法是每一个线程启动之前时,先执行下面代码:

#ifndef WIN32

sigset_t signal_mask;

sigemptyset (&signal_mask);

sigaddset (&signal_mask, SIGPIPE);

int rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);

if (rc != 0) {

printf("block sigpipe error\n");



#endif        

当然,这只是多种方法之一~

根据赖半仙的使用经验,只要在main函数一开始就写入上面这段代码,就能屏蔽掉pthread线程中的SIGPIPE

[linux] SIGPIPE信号及其处理
http://hi.baidu.com/mckeyzhang/blog/item/d647f26034eee542eaf8f823.html

在linux下写socket的程序的时候,如果尝试send到一个disconnected socket上,就会让底层抛出一个SIGPIPE信号。

这个信号的缺省处理方法是退出进程,大多数时候这都不是我们期望的。因此我们需要重载这个信号的处理方法。调用以下代码,即可安全的屏蔽SIGPIPE:

struct sigaction sa;

sa.sa_handler = SIG_IGN;

sigaction( SIGPIPE, &sa, 0 );

//======================================================================

SIGPIPEFrom Wikipedia, the free encyclopediaJump to: navigationsearch SIGPIPE
Description Write on a pipe with no one to read it Default action Abnormal termination of the process SA_SIGINFOmacros one 

On POSIX-compliant platforms, SIGPIPE is the signal raised
when a computer program attempts to write to a pipe without
a process connected to the other end. The symbolic constant for SIGPIPE is defined in
the header file signal.h. Symbolic signal names are used because signal numbers can vary across
platforms.

Etymology

SIG is a common prefix for signal
names. PIPE refers to the Unix pipe.

Description 

Unix supports the principle of piping,
which allows processes to send data to other processes without the need for creating temporary files. When a pipe is broken, the process writing to it is sent the SIGPIPE signal. The default reaction to this signal for a process is to terminate.

A simple example of piping is the following.

ps l | head 

This command, when run on a Unix-like machine (including Linux),
returns a list of processes, limited to ten lines.

ps l returns a list of all processes (including those
of other users). 

head selects the first ten lines.

When ps has written ten lines, head has received all it needs and exits. ps will receive a SIGPIPE when it tries to write the remaining lines, causing it to terminate as well: It is no use writing data that no one will use. It is also possible that the reading
process terminates while reading the data. This will also cause SIGPIPE to be sent to the writing process.

One can ignore SIGPIPE (using, for example, the signal system
call). In this case, all system calls that would cause SIGPIPE to be sent will return -1 and set errno to
EPIPE.

Uinx 下 Broken pipe 问题 

www.javaeye.com/topic/456975#

前段时间在处理延时函数时遇到过 "Alarm clock" 信号问题(见我的 "Unix C 延时函数小结")。现在测试中还遇到了 "Broken pipe" 信号问题,同样产生这个信号程序就中止了。 

我的程序产生这个信号的原因是: 

client端通过 pipe 发送信息到server端后,就关闭client端, 这时server端,返回信息给 client 端时就产生Broken pipe 信号了。 

对于产生信号,我们可以在产生信号前利用方法 signal(int signum, sighandler_t handler) 设置信号的处理。如果没有调用此方法,系统就会调用默认处理方法:中止程序,显示提示信息(就是我们经常遇到的问题)。我们可以调用系统的处理方法,也可 以自定义处理方法。 

系统里边定义了三种处理方法: 

1)SIG_DFL    /* Default action */ 

2)SIG_IGN    /* Ignore action */ 

3)SIG_ERR    /* Error return */ 

项目中我调用了 signal(SIGALRM, SIG_IGN) 和 signal(SIGPIPE, SIG_IGN), 这样产生 SIGALAM 和 SIGPIPE 信号时就不会中止程序,直接把这个信号忽略掉。 

自定义处理方法:

 

void   signal_handle(ing   signo)      

{      

    //do   something;      

}       

   

int   main()      

{   

    signal(SIGPIPE, signal_handle);   

    ......   

}  

void signal_handle(ing signo) { //do something; } int main() { signal(SIGPIPE, signal_handle); ...... }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: