views:

572

answers:

1

Hello

I want to use setitimer() (or alarm()) in multithreaded process in linux 2.6+ with NPTL-enabled libc. Which thread will receive sigalarm (SIGALRM) from kernel?

Thanks.

+6  A: 

From signal(7) man page:

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.

Now, alarm(2) man page says that:

alarm() arranges for a SIGALRM signal to be delivered to the process in seconds seconds.

So, the signal is delivered to a process (a signal might be directed at certain thread too) and thus you do not know which of the threads will receive it.

The same with setitimer(2):

When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

You could block SIGALARM in all your threads except one, then you could be certain that it will be delivered to that only thread. Assuming you are using pthreads, you can block signals with pthread_sigmask().

pajton
and what about setitimer?
osgx
It's the same for all signals.
nos
@nos Basically, but you can deliver signals straight to certain thread with for instance `pthread_kill()`
pajton
Also, signals raised from hardware exception are delivered to the thread
osgx