Ziqi Liu
Ziqi Liu

Reputation: 3171

C: is signal() process-level?

I'm writing a tiny web server in C, and I want to ignore SIGPIPE signal so that my server won't terminate by trying to write to closed connection.

Signal(SIGPIPE, SIG_IGN);

I create thread to handle each request, and I set the signal ignore at main thread. But I think signal is process-level, so I believe this should work for any child thread created.(i.e, I don't have to call this signal ignore again in my child thread.) But still, my server will terminated for some reason. I still debugging on that, but I just want to make sure that it's not the SIGPIPE problem.....

Upvotes: 0

Views: 71

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60068

Technically, the C standard says that using the signal function in a multithreaded environment results in undefined behavior (which is crazy, IMHO, especially if your calling it with SIG_IGN).

Practically, signal dispositions are process-wide, and it is recommended that new POSIX programs use sigaction instead of signal, as sigaction has better defined semantic (not that it matters for SIG_IGN).

In C99 and newer, you could call sigaction like so:

sigaction(SIGPIPE, &(struct sigaction){ .sa_handler = SIG_IGN }, 0);

(Note that if you call it correctly, you shouldn't have to check the return status since the only possible errors a, which arere EINVAL and EFAULT, which you'll only get if you provide ill-formed arguments.)

Upvotes: 2

Related Questions