Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX Threads/Signals: Portable way to determine to which thread a signal was delivered?

I've got a multithreaded server (using POSIX threads), with one thread for each persistent connection. In one of the threads the other end of the connection closes, resulting in a SIGPIPE being delivered. Is there a (preferably portable) to determine which thread (and so which connection) this happened for so I can have my signal handler either do the thread/connection cleanup work itself or set a flag so that the main and worker thread respectively see that they need to do it later?

EDIT: I'm wondering if I could perhaps use &errno, store it in a global array and associate it with the server's identifier for the thread, and then search for &errno in the signal handler. Would the thread's specific errno be visible to the signal handler? Is my understanding of how the threadsafe errno works even in the ballpark?

like image 225
Steely Dan Avatar asked Dec 27 '11 21:12

Steely Dan


People also ask

What is a POSIX signal?

The POSIX signal implementation ensures that if a process is already. handling a signal, other incoming signals are suspended until the. handler returns.

Where should a signal be delivered for multithreaded?

A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal. Save this answer.

Are signals shared between threads?

A signal mask is associated with each thread. The list of actions associated with each signal number is shared among all threads in the process. If the signal action specifies termination, stop, or continue, the entire process, thus including all its threads, is respectively terminated, stopped, or continued.

Do threads inherit signal handlers?

As mentioned earlier, a thread inherits its signal mask from the thread which creates it. The main() function sets the signal mask to block all signals, so all threads created after this point will have all signals blocked, including the signal-handling thread.


1 Answers

I don't think so, no. A better solution for multithreaded servers is to suppress the SIGPIPE signal (by calling signal(SIGPIPE, SIG_IGN) as part of the program's startup routine) and then have the thread deal with the error value (-1/EPIPE) returned by send() instead.

Signals and multithreading don't mix well.

like image 168
Jeremy Friesner Avatar answered Oct 21 '22 14:10

Jeremy Friesner