sigwait() and signal handler
c, linux, multithreading, pthreads, signals
Solution
From `sigwait()` documentation :
The sigwait() function suspends execution of the calling thread until one of the signals specified in the signal set becomes pending.
A pending signal means a blocked signal waiting to be delivered to one of the thread/process. Therefore, you need not to unblock the signal like you did with your `pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)` call.
This should work :
static void* WaitForAbortThread(void* v){
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGABRT);
sigwait( &signal_set, &sig );
TellAllThreadsWeAreGoingDown();
sleep(10);
return null;
}
Problem
If I setup and signal handler for SIGABRT and meanwhile I have a thread that waits on sigwait() for SIGABRT to come (I have a blocked SIGABRT in other threads by pthread_sigmask). So which one will be processed first ? Signal handler or sigwait() ? [I am facing some issues that sigwait() is get blocked for ever. I am debugging it currently] ``` main() { sigset_t signal_set; sigemptyset(&signal_set); sigaddset(&signal_set, SIGABRT); sigprocmask(SIG_BLOCK, &signal_set, NULL); // Dont deliver SIGABORT while running this thread and it's kids. pthread_sigmask(SIG_BLOCK, &signal_set, NULL); pthread_create(&tAbortWaitThread, NULL, WaitForAbortThread, NULL); .. Create all other threads ... } static void* WaitForAbortThread(void* v) { sigset_t signal_set; int stat; int sig; sigfillset( &signal_set); pthread_sigmask( SIG_BLOCK, &signal_set, NULL ); // Dont want any signals sigemptyset(&signal_set); sigaddset(&signal_set, SIGABRT); // Add only SIGABRT // This thread while executing , will handle the SIGABORT signal via signal handler. pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL); stat= sigwait( &signal_set, &sig ); // lets wait for signal handled in CatchAbort(). while (stat == -1) { stat= sigwait( &signal_set, &sig ); } TellAllThreadsWeAreGoingDown(); sleep(10); return null; } // Abort signal handler executed via sigaction(). static void CatchAbort(int i, siginfo_t* info, void* v) { sleep(20); // Dont return , hold on till the other threads are down. } ``` Here at sigwait(), i will come to know that SIGABRT is received. I will tell other threads about it. Then will hold abort signal handler so that process is not terminated. I wanted to know the interaction of sigwait() and the signal handler.