0

I have a test program for part of a C library where I handle signals. There is a lot of code in this test program - so much that I feel that posting it here would only make it more difficult to answer my question.

Basically what's happening is that I am forcing the system to send me a SIGSEGV inside a background thread. I handle that signal, and then the test is over. The main thread will be waiting for this and part of the signal handler is to restart the main thread. When the main thread restarts I restore the default signal handler by signal(SIGSEGV, SIG_DFL) and then I get another SIGSEGV.

I understand that I have a lot of code and this could very well be my fault, but is there a common explanation of why a SIGSEGV would fire twice? It is not happening when I cause other signals (though I haven't tried all of them).

To clarify I am on OS X, writing in C, compiling with gcc.

William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37

1 Answers1

0

If you have any problem inside the signal handler, for example, memory overwrite, trying to access beyond the allocated area, then a new signal will be raised. Check the following page for details.

http://www.gnu.org/software/libc/manual/html_node/Signals-in-Handler.html

You should also use reentrant functions inside the signal handlers, For example localtime_r instead of localtime.

Moreover it is recommended to use sigaction() instead of signal().

More details here: What is the difference between sigaction and signal?

Community
  • 1
  • 1
Umamahesh P
  • 1,224
  • 10
  • 14