Why my linux signal handler run only once

Posted by Henry Fané on Stack Overflow See other posts from Stack Overflow or by Henry Fané
Published on 2010-05-27T10:43:56Z Indexed on 2010/05/27 12:11 UTC
Read the original article Hit count: 183

Filed under:
|
|
#include <iostream>
#include <signal.h>
#include <fenv.h>
#include <string.h>

void signal_handler(int sig, siginfo_t *siginfo, void* context) 
{ 
  std::cout << " signal_handler " << fetestexcept(FE_ALL_EXCEPT) << std::endl;
  throw "exception"; 
}

void divide() {
  float a = 1000., b = 0.,  c, f = 1e-300;
  c = a / b;

  std::cout << c << " and f = " << f << std::endl;  
}

void init_sig_hanlder() {
  feenableexcept(FE_ALL_EXCEPT);

  struct sigaction sa, initial_sa;

  sa.sa_sigaction   = &signal_handler ;
  sigemptyset( &sa.sa_mask ) ;
  sa.sa_flags   = SA_SIGINFO;   // man sigaction(3) // allows for void(*)(int,siginfo_t*,void*) handler

  sigaction(SIGFPE, &sa, &initial_sa);

}

int main(int argc, char** argv) {
  init_sig_hanlder();

  while(true)
    {
      try {
    sleep(1);
    divide();
      }
      catch(const char * a) {
    std::cout << "Exception in catch: " << a << std::endl;
      }    
      catch(...) {
    std::cout << "Exception in ..." << std::endl;
      }    
    }

  return 0;
}

Produce the following results on Linux/g++4.2:

signal_handler 0
Exception in catch: exception
inf and f = 0
inf and f = 0
inf and f = 0
inf and f = 0

So, signal handler is executed the first time but the next fp exception does not trigger the handler again. Where am I wrong ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about linux