I know the individual uses of SIGPIPE
and SIGIGN
.
What does
signal(SIGPIPE, SIG_IGN);
exactly do?
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With