2009-11-23 18:32:54     Problem with pthreads (linuxthreads) and signals

Document created by Aaronwu Employee on Aug 19, 2013
Version 1Show Document
  • View in full screen mode

2009-11-23 18:32:54     Problem with pthreads (linuxthreads) and signals

Øyvind Kaurstad (NORWAY)

Message: 82671   




I'm working on a multithreaded daemon in which I want to be able to terminate gracefully by catching the SIGTERM signal. I need to close several threads, and I've followed the recommendation to block all signals in all threads (ie block in the parent thread before creating child threads) and have a dedicated thread sitting on sigwait. This dedicated thread can then set a global flag to shut down.


The theory is that this thread should then get all signals (well, almost), since the other threads are blocking them. However, this does not work as expected. The only way to stop the process is to specifically kill the thread that is actually waiting for the signal. Trying to kill any other threads has no effect, which according to the linuxthreads FAQ should work.


Is this a known problem with (blackfin)-uClinux? I'm using the 2009R1 and toolchain.


I'm doing it as described in paragraph 8.11 here:










2009-11-29 21:42:19     Re: Problem with pthreads (linuxthreads) and signals


Message: 82876   




Do you have a simple test case for me to reproduce your issue?






2009-11-30 07:31:28     Re: Problem with pthreads (linuxthreads) and signals

Øyvind Kaurstad (NORWAY)

Message: 82888   


Attached is a simple program to demonstrate the issue.


Compiled like this:


bfin-uclinux-gcc -Wl,-elf2flt sigtestfail.c -lpthread -o sigtestfail


If started like this (on the target):


root:/> sigtestfail &


you will get three pids for this program. The first is the main "thread", the second is the auto-created pthreads managment thread, and the third is the signal handler thread.


According to the documentation, since all signals are blocked in all threads (beacause of inheritance) except the signal handler thread, this thread should get all signals (except a few, but that's irrelevant here).


Specifically, I want that if any of my threads (I have more in my real application) receive the kill signal, I want the single signal handler thread to get that signal, and this will in turn trigger an orderly shutdown of my other threads.


If you compile and run my example, you will see that trying to kill the first or second pids (lowest numbered) will do exactly nothing, but if you kill the last pid it works.










2009-12-10 23:19:41     Re: Problem with pthreads (linuxthreads) and signals

Srinivas Nagathihalli (INDIA)

Message: 83372   


Hello Kaurstad,


  The behaviour you see seems to be consistent with the behaviour of linuxthreads implementation. Please see man page pf pthreads.


The man page says


"LinuxThreads does not support the notion of process-directed signals: signals may only be sent to specific threads"


The behaviour what you expect is true for NPTL implementation, where the signal is delivered to any one of the threads where that signal is not blocked.


I am also new to this and experimenting. I have one more question and I will start a new thread.






2009-12-14 06:28:47     Re: Problem with pthreads (linuxthreads) and signals

Øyvind Kaurstad (NORWAY)

Message: 83486   




That piece of information had slipped past me, thanks for pointing it out.


It does explain the behaviour I am seeing, but unfortunately it also means that to get the behaviour I wanted I will need separate signal handling for each thread. I don't think I want that, so I'll just live with my current implementation instead.