Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signal handler function in multithreaded environment

In my multithreaded GUI application I have the following signal handling code. I want to improve this code so that it will be correct and threading safe but there are some things I don't fully understand in signal handling:

  • is signal handled at the process or thread level (can I have thread-specific signal handlers) ?
  • in which thread context is signal_handler function executed ?
  • is it possible to send many SIGTERM signals in a short time ?
  • does it make sense to use a mutex to prevent parallel execution of signal_handler ?

void signal_handler(int sig)
{
        switch (sig)
        {
        case SIGTERM:
            ::wxLogMessage(wxT("SIGTERM signal received ..."));
            break;
        case SIGINT:
            ::wxLogMessage(wxT("SIGINT signal received ..."));
            break;
        case SIGUSR1:
            ::wxLogMessage(wxT("SIGUSR1 signal received ..."));
            break;
        default:
            ::wxLogMessage(wxT("Unknown signal received ..."));
        }

        // send wxCloseEvent to main application window
        ::wxGetApp().GetTopWindow()->Close(true);
}

I register signal handlers in my init function:

// register signal handlers
signal(SIGTERM, signal_handler);
signal(SIGINT,  signal_handler);
signal(SIGUSR1, signal_handler);
like image 777
tommyk Avatar asked Oct 18 '12 10:10

tommyk


People also ask

What is the purpose of a signal handler?

A signal handler is a function which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp() . Signal handlers can be set with signal() or sigaction() .

Are signal handlers shared across threads?

All threads in a process share the set of signal handlers set up by sigaction(2) and its variants. A thread in one process cannot send a signal to a specific thread in another process.

Do threads inherit signal handler?

A signal handler is code, which is shared by all of your threads because all the threads share the process' memory space. Hence, there's no way it will "not have the signal handler".

When a multi threaded process receives a signal to what thread should that signal be delivered?

However the signal can only be delivered to one thread, which is generally the first thread that is accepting that particular signal.


1 Answers

  • Signal handlers are per-process state - that is, all the threads in a process share the same set of installed signal handler functions.
  • Signal masks are per-thread state. Signals can be blocked or unblocked on a per-thread basis.
  • Signals can be process- or thread-directed. If a signal is process-directed, then an arbitrary thread which does not currently have the signal type blocked is chosen to handle it.

A simple way to handle signals in a multi-threaded application is to create one thread as a dedicated signal-handling thread. All signals of interest are blocked in every thread; no signal handlers are established; and the signal-handling thread calls sigwaitinfo() in a loop, acting on the signals as they're received.

This means that you don't need to worry about whether the functions you want to call are async-signal-safe or not, because signals aren't handled in signal handlers - they're handled synchronously by your dedicated signal-handling thread, which can call any function it likes (for example, it can use the ordinary pthreads synchronisation functions to wake up another thread).

like image 72
caf Avatar answered Sep 18 '22 20:09

caf