I am using pthread library under Linux for creating threads and I have two questions about signal handling in such applications.
I know that signal handlers are process-wide, which means that if I set handler in process every thread will have this signal handler, also I know that there is pthread_kill
function for sending signals to particular threads.
I have a question about sending signals using for example shell kill
command, as far as I understand, if I type for example kill -INT PID
I will send SIGINT
to process with this PID
, if this is multithreaded program the signal will be delivered to one of the threads in this process.
First question, I won't have any guarantee to which of the threads this signal will be delivered, I can only be sure that it will be delivered to one thread without this signal in signal mask?
If so what about few signals that are delivered to particular thread, like 'SIGFPE', 'SIGSEGV', if I will send them using kill
shell command they will be delivered to random thread or will they be delivered to the thread that created other threads?
Citing man pthreads
POSIX.1 distinguishes the notions of signals that are directed to the process as a whole and signals that are directed to individual threads. According to POSIX.1, a process-directed signal (sent using kill(2), for example) should be handled by a single, arbitrarily selected thread within the process.
There were some problems in Linux at days of glibc 2.2 and older (linuxthreads was used as pthread implementation); but since glibc 2.3-2.4 there is NPTL which is more accurate in POSIX conformance about signals.
I can only be sure that it will be delivered to one thread without this signal in signal mask?
If you are using kill - yes; to random thread which doesn't block this signal.
If so what about few signals that are delivered to particular thread, like 'SIGFPE', 'SIGSEGV',
They are delivered to particular thread, only when generated by CPU/kernel (by particular instruction in some context); not by kill
utility with PID argument
if I will send them using kill shell command they will be delivered to random thread or will they be delivered to the thread that created other threads?
They will be delivered to random thread of process, kill usually sends process-wide signals. But if signal is deadly, all threads in process will be destroyed.
PS: http://www.linuxprogrammingblog.com/all-about-linux-signals?page=11