Subj : Re: Posix timer thread and signal segfault To : comp.programming.threads From : roger.faulkner Date : Wed Jan 19 2005 08:36 pm VK wrote: > Hi, I hope I'm posting to the right group. If not, could you point me > to a correct group. Thanks a lot. > > I have a timer thread that sends timeout signal to the signal handler > when a timer expires. Then the signal handler sends SIGRTMIN to the > thread handler. When creating a timer, I assign the address of the > generated timer id to the sigevent's info. > > The siginfo returned in signalHandler seems to always contain valid > info. However, the siginfo returned in threadHandler from calling > sigwaitinfo() *sometimes* gives me an invalid address of the timer id. > The result is of course a segfault. > > I have no idea why this happens. Any help is really appreciated. I've > been looking at this code for a day now and still can't figure it out. > > Thanks so much. Below is my test code. You are receiving the signal in 'signalHandler()' and then sending it again to the thread in 'threadHandler()': void signalHandler (int signum, siginfo_t* info, void* notused) { pthread_kill(thread, SIGRTMIN ); <======= here ====== std::cout << "sigHandler tid = " << *(timer_t*)(info->si_ptr); std::cout << " @ " << (timer_t*)(info->si_ptr) << std::endl; } Once you send it with pthread_kill(thread, SIGRTMIN), it no longer has an associated sigval, so the thread in 'threadHandler()' dereferences a NULL pointer and the process dies of a SIGSEGV. This is expected behavior. You need to use sigwaitinfo() properly: 1. block SIGRTMIN in all threads. Blocking it in the main thread before creating any other threads will do, since the signal mask is inherited. 2. Forget 'signalHandler'. It is unnecessary. The thread in 'threadHandler()' will accept the signal directly from the firing of the timer. No intermediary is needed. Roger Faulkner roger.faulkner@sun.com .