Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does signal(SIGPIPE, SIG_IGN); do?

Tags:

c

unix

signals

I know the individual uses of SIGPIPE and SIGIGN.

What does

signal(SIGPIPE, SIG_IGN);

exactly do?

like image 492
xennygrimmato Avatar asked Jun 12 '15 09:06

xennygrimmato


People also ask

What is the use of SIGPIPE signal?

The rule that applies is: When a process writes to a socket that has received an RST, the SIGPIPE signal is sent to the process. The default action of this signal is to terminate the process, so the process must catch the signal to avoid being involuntarily terminated.

Should I ignore SIGPIPE?

You generally want to ignore the SIGPIPE and handle the error directly in your code. This is because signal handlers in C have many restrictions on what they can do. The most portable way to do this is to set the SIGPIPE handler to SIG_IGN . This will prevent any socket or pipe write from causing a SIGPIPE signal.

What is Sig_dfl?

SIG_DFL specifies the default action for the particular signal. The default actions for various kinds of signals are stated in Standard Signals. SIG_IGN. SIG_IGN specifies that the signal should be ignored.


1 Answers

signal(SIGPIPE, SIG_IGN);

simply ignores the signal SIGPIPE. Passing SIG_IGN as handler ignores a given signal (except the signals SIGKILL and SIGSTOP which can't caught or ignored).

As an aside, it's generally recommended to use sigaction(2) over signal(2) as sigaction offers better control and also don't need to "reset" the handler on some systems (which follow the System V signal behaviour).

like image 103
P.P Avatar answered Oct 13 '22 00:10

P.P