which running thread gests the external signal

Daniel Eischen deischen at freebsd.org
Tue May 2 18:15:18 UTC 2006


On Tue, 2 May 2006, Alin-Adrian Anton wrote:

> Daniel Eischen wrote:
>> POSIX states any thread that is in sigwait() (with the specified
>> signal in the wait mask), or has the signal unmasked (in the threads
>> signal mask) can receive the signal.  If you want a certain thread
>> to receive a process-wide signal, then the only sure way (POSIX) to
>> do that is to block the signal in all the threads with the exception
>> of the thread that is to receive the signal.
>> 
>
> OK, I was able to delegate a single thread for handling all the signals, by 
> using sigprocmask to block all signals at the beggining, then using 
> pthread_sigmask to unblock the needed signals inside the delegated thread. 
> This seemed to be the cleanest way of doing it..
>
> However, this is not fully clean: all the other threads should *ignore* the 
> signals, not *block* them. Blocking a signal means the signal will be queued 
> and the queue will eventually fill, and so on. In my scenario I get the 
> result without running into problems (because each thread seems to have it's 
> own signal queue), however it's not... "clean". The other threads need to 
> simply 'drop' the signals, not cause them to be queued forever (consider an 
> uptime of 1 year?). I don't know the impact, however I want it to be clean..

You are entirely confused.  You should go back to the POSIX standard
and get Dave Butenhof's Programming with POSIX Threads book.

One and only one thread gets _a_ signal.  Blocking a signal in
a thread does not mean it is queued on that thread, unless the
signal is sent specifically to that thread (using
pthread_kill(tid, sig), not kill(pid, sig)).  Read the part of
the POSIX standard that talks about signals pending on the process.
Signals pending on the process, stay pending (or queued if they
are queued signals) until a thread either unblocks the signal,
calls sigwait() selecting that signal, or ignores the signal
(using sigaction()).

A signal handler runs in the context of the thread that is
receiving (handling) the signal.

> So ... would it be a way to ignore the signals from all the other threads 
> except the delegated one for handling them? (I'm sorry, I don't notice it, 
> even if it's obvious)
>
> Thanks for the advices and the tips, it's been really usefull.
>
> PS: Without using sigprocmask and pthread_sigmask, one random thread is 
> stopped in order for the signal handler to execute. Doesn't this mean that 
> the other threads are not 'seeing' the signal? In order to force a thread to 
> receive/see the signal, I need to block the signal inside all the other 
> threads (either with sigprocmask in main, or with pthread_sigmask). On Linux, 
> the signal gets delivered to all the running threads, unless specifically 
> blocked :). And I think that conformes to your mentioning of POSIX standards.

No, if Linux delivers one signal to multiple threads, then it
is entirely wrong and not compliant with POSIX behavior.  I
think Linux' NPT threads (or whatever they are called) have
correct behavior.

-- 
DE


More information about the freebsd-hackers mailing list