最近在做网络编程,有时候程序会挂掉,调试发现程序收到了SIGPIPE,造成这个信号的原因可以自行google,因为程序收到SIGPIPE的默认动作是终止程序,所以我们需要对这个信号进行屏蔽。
在多进程的环境下,下面代码可以解决问题:
1 2 3 4 5 6 7 8 9 |
<span style="color: #993333;">struct</span> sigaction sa<span style="color: #339933;">;</span> sa.<span style="color: #202020;">sa_handler</span> <span style="color: #339933;">=</span> SIG_IGN<span style="color: #339933;">;</span> sa.<span style="color: #202020;">sa_flags</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> sigemptyset<span style="color: #009900;">(</span><span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>sa.<span style="color: #202020;">sa_mask</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> sigaddset<span style="color: #009900;">(</span><span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>sa.<span style="color: #202020;">sa_mask</span><span style="color: #339933;">,</span>SIGPIPE<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span>sigaction<span style="color: #009900;">(</span>SIGPIPE<span style="color: #339933;">,</span> <span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>sa<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> perror<span style="color: #009900;">(</span><span style="color: #ff0000;">"failed to ignore SIGPIPE"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> exit<span style="color: #009900;">(</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> |
如果是在多线程的环境下,在创建线程之前需要:
1 2 3 4 5 6 |
sigset_t signal_mask<span style="color: #339933;">;</span> sigemptyset<span style="color: #009900;">(</span><span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>signal_mask<span style="color: #009900;">)</span><span style="color: #339933;">;</span> sigaddset<span style="color: #009900;">(</span><span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>signal_mask<span style="color: #339933;">,</span> SIGPIPE<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span> pthread_sigmask<span style="color: #009900;">(</span>SIG_BLOCK<span style="color: #339933;">,</span> <span style="color: #339933;">&</span>amp<span style="color: #339933;">;</span>signal_mask<span style="color: #339933;">,</span> NULL<span style="color: #009900;">)</span> <span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000066;">printf</span><span style="color: #009900;">(</span><span style="color: #ff0000;">"block sigpipe error<span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> |