Why doesn't my signal handler (which throws an exception) trigger more than once?

后端 未结 2 1894
星月不相逢
星月不相逢 2020-12-12 00:53

I am trying to set up an exception handler using sigaction. It works well for the first exception. But the sigaction handler is not called after the 1st exception and the pr

相关标签:
2条回答
  • 2020-12-12 01:41

    Signals and exceptions are not related to each other. What you're using (throwing exceptions from async signal handlers) is only portable between the few compilers that support it, such as GCC and Intel C/C++ with -fnon-call-exceptions.

    That said, what you forgot to do is unblock the signal: when a signal handler is executing, the delivery of the same signal is blocked, and it does not become unblocked when the signal handler is exited through an exception. Change the signal handler as follows:

    void SigactionHookHandler( int iSignal, siginfo_t * psSiginfo, void * psContext
    {
       cout << "Signal Handler Exception Caught: std::exception -- signal : " << iSignal << " from SigactionHookHandler()" << endl;
    
       sigset_t x;
       sigemptyset (&x);
       sigaddset(&x, SIGSEGV);
       sigprocmask(SIG_UNBLOCK, &x, NULL);
    
       throw std::exception();
    }
    
    0 讨论(0)
  • 2020-12-12 01:43

    Standard C++ says nothing about signals, or about how they interact with exceptions. What you are trying to do will be completely specific to the OS platform you are using and possibly the specific compiler you compile your code with.

    0 讨论(0)
提交回复
热议问题