织梦CMS - 轻松建站从此开始!

罗索

遭遇SIGPIPE

落鹤生 发布于 2013-01-14 16:11 点击:次 
最后问题确定为, 对一个对端已经关闭的socket调用两次write, 第二次将会生成SIGPIPE信号, 该信号默认结束进程.
TAG:

我写了一个服务器程序, 在Windows下在cygwin环境编译后执行, 然后用C#写了多线程客户端进行压力测试. 程序一直运行正常. 但当在Linux下测试时, 总是莫名退出. 最后跟踪到是write调用导致退出. 用gdb执行程序, 退出时提示"Broken pipe".

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

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

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

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

  1. signal(SIGPIPE, SIG_IGN); 

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

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

忽略SIGPIPE信号的方法
http://hi.baidu.com/greathongjian/blog/item/2f695643091885139213c65a.html

  1. struct sigaction sa; 
  2. sa.sa_handler = SIG_IGN;//设定接受到指定信号后的动作为忽略 
  3. sa.sa_flags = 0; 
  4. if (sigemptyset(&sa.sa_mask) == -1 ||   //初始化信号集为空 
  5. sigaction(SIGPIPE, &sa, 0) == -1) {   //屏蔽SIGPIPE信号 
  6. perror("failed to ignore SIGPIPE; sigaction"); 
  7. 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的问题,解决方法是每一个线程启动之前时,先执行下面代码:

  1. #ifndef WIN32 
  2. sigset_t signal_mask; 
  3. sigemptyset (&signal_mask); 
  4. sigaddset (&signal_mask, SIGPIPE); 
  5. int rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL); 
  6. if (rc != 0) { 
  7. printf("block sigpipe error\n"); 
  8. }  
  9. #endif 

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

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

[linux] SIGPIPE信号及其处理
http://hi.baidu.com/mckeyzhang/blog/item/d647f26034eee542eaf8f823.html
在linux下写socket的程序的时候,如果尝试send到一个disconnected socket上,就会让底层抛出一个SIGPIPE信号。
这个信号的缺省处理方法是退出进程,大多数时候这都不是我们期望的。因此我们需要重载这个信号的处理方法。调用以下代码,即可安全的屏蔽SIGPIPE:

  1. struct sigaction sa; 
  2. sa.sa_handler = SIG_IGN; 
  3. sigaction( SIGPIPE, &sa, 0 ); 


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


SIGPIPEFrom Wikipedia, the free encyclopedia

Jump 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. Thesymbolic 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 信号时就不会中止程序,直接把这个信号忽略掉。 

自定义处理方法:

  1. void   signal_handle(ing   signo)      
  2. {      
  3.     //do   something;      
  4. }       
  5.    
  6. int   main()      
  7. {   
  8.     signal(SIGPIPE, signal_handle);   
  9.     ......   
  10. }   

 

(技术员)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201301/16458.html]
本文出处:chinaunix 作者:技术员 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容