tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

sigwait() and SIGSEGV



Is it possible to catch SIGSEGV in a multithreaded program?

I have a test program, the main thread creates 3 threads - 1 signal catching 
thread and 2 normal threads.

The main thread masks all signals and creates the threads. The new threads 
inherit the signal mask from the main thread.

int main(void)
{
        ...
        pthread_t stid; /* signal thread id */
        sigset_t sigset;

        sigfillset(&sigset);
        pthread_sigmask(SIG_SETMASK, &sigset, NULL);

        pthread_create(&stid, NULL, &sig_thread, &sigset);

        /* Create other threads */
        ....
}

Below is the signal catching thread, which calls sigwait() in a loop and for 
each signal it prints the name:

/* Signal handling thread */
void *sig_thread(void *arg)
{
        sigset_t *sigset = (sigset_t *)arg;
        int signum;

        while(1)
        {
                sigwait(sigset, &signum);
                switch(signum)
                {
                        case SIGUSR1:
                                printf("SIGUSR1\n");
                        break;
                        case SIGSEGV:
                                /*printf("SIGSEGV\n");*/
                                exit(10);
                        break;
                        default:
                                printf("Got signal %d, exiting\n", signum);
                                exit(1);
                }
        }
}

If one of the other threads dereferences a NULL pointer, the program hangs. I 
don't understand why sigwait() does not catch SIGSEGV. Does sigwait() only 
work with asynchronous signals?


Home | Main Index | Thread Index | Old Index