Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sigwait() and signal handler

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.

like image 765
Lunar Mushrooms Avatar asked Feb 04 '14 12:02

Lunar Mushrooms


People also ask

Does signal () call the signal handler?

The signal() system call just installs the handler: "signal() sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer-defined function (a "signal handler")." There you go.

What does a signal handler do?

24.4 Defining Signal Handlers A signal handler is just a function that you compile together with the rest of the program. Instead of directly invoking the function, you use signal or sigaction to tell the operating system to call it when a signal arrives.

What is Sigwait?

The sigwait() function selects a pending signal from set, clears it from the set of pending signals for the thread or process, and returns that signal number in the location that is referenced by sig.

Does Sigwait block?

Usually, you use sigwait() to create one or more threads that wait for signals. Because sigwait() can retrieve even masked signals, be sure to block the signals of interest in all other threads so they are not accidentally delivered.


2 Answers

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;
}
like image 198
jebeaudet Avatar answered Sep 21 '22 18:09

jebeaudet


I got some information from this <link>

It says :

To allow a thread to wait for asynchronously generated signals, the threads library provides the sigwait subroutine. The sigwait subroutine blocks the calling thread until one of the awaited signals is sent to the process or to the thread. There must not be a signal handler installed on the awaited signal using the sigwait subroutine.

I will remove the sigaction() handler and try only sigwait().

like image 43
Lunar Mushrooms Avatar answered Sep 23 '22 18:09

Lunar Mushrooms