| 2013年5月21日
在gdb调试程序的时候发现一个错误信息, Program received signal SIGPIPE, Broken pipe,原来是客户端程序在给服务器进行写入操作的时候,如果服务器执行了close操作,这时候根据tcp协议规定服务器要给客户端返回一个RST响应,如果client再用之前的fd往这个服务器写数据的时候,系统救护发一个SIGPIPE信号给进程,告诉进程链接已经断开。但是SIGPIPE信号的默认执行动作是terminate,所以客户端会退出,如果要想客户端不响应这个信号,则直接将其屏蔽忽略即可。
把SIGPIPE设为SIG_IGN
signal(SIGPIPE,SIG_IGN);
这里顺便再复习以下linux信号函数的一些知识吧。看看man手册,东西还不少,先摘取一些
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
修改信号的处理函数,系统中定义的信号处理有默认处理函数和忽略处理,默认处理类型可以分为下面几种:
Term Default action is to terminate the process.
Ign Default action is to ignore the signal.
Core Default action is to terminate the process and dump core (see
Stop Default action is to stop the process.
Cont Default action is to continue the process if it is currently
stopped.
linux支持的信号有以下 POSIX.1-1990 标准种的信号有
Signal Value Action Comment
----------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at terminal
SIGTTIN 21,21,26 Stop Terminal input for background process
SIGTTOU 22,22,27 Stop Terminal output for background process
其中SIGKILL和SIGSTOP这两个信号是不可以被捕捉,忽略,阻塞的。 SUSv2 and POSIX.1-2001中增加的。
Signal Value Action Comment
--------------------------------------------------------------------
SIGBUS 10,7,10 Core Bus error (bad memory access)
SIGPOLL Term Pollable event (Sys V).
Synonym for SIGIO
SIGPROF 27,27,29 Term Profiling timer expired
SIGSYS 12,31,12 Core Bad argument to routine (SVr4)
SIGTRAP 5 Core Trace/breakpoint trap
SIGURG 16,23,21 Ign Urgent condition on socket (4.2BSD)
SIGVTALRM 26,26,28 Term Virtual alarm clock (4.2BSD)
SIGXCPU 24,24,30 Core CPU time limit exceeded (4.2BSD)
SIGXFSZ 25,25,31 Core File size limit exceeded (4.2BSD)
关注「黑光技术」,关注大数据+微服务