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?
actually, std::threads provides portability across all platforms that support C++11, whereas POSIX threads is only available on POSIX platforms (or platforms that strive for some minimal compatability).
A signal handler is code, which is shared by all of your threads because all the threads share the process' memory space. Hence, there's no way it will "not have the signal handler".
signal is a per process call, not a per thread one, if you call it it sets the handler for all threads in the process. Signals and threads is a complex topic. You might be better off asking about what you really what to do. Using signal in a multithreaded program is essentially undefined behaviour.
The pthread_kill() function sends the signal sig to thread, a thread in the same process as the caller. The signal is asynchronously directed to thread. If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a thread ID.
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
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