I\'d like to know if it is possible/the recommended way to catch the SIGSEGV
signal in multithreaded environment. I am particularly interested in handling the <
Your code does not call sigaction(2), and I believe it should call it. Read also signal(7) and signal-safety(7). And the signal action (thru sa_sigaction
field should do something (machine specific) with its siginfo_t
to skip the offending machine instruction, or to mmap
the offending address, or call siglongjmp
, otherwise when returning from the signal handler you'll get the SIGSEGV
again since the offending machine instruction is restarted.
You cannot handle the SIGSEGV
in another thread, since synchronous signals (such as SIGSEGV
or SIGSYS
) are thread specific (see this answer), so what you try to achieve with sigwaitinfo
cannot work. In particular SIGSEGV
is directed to the offending thread.
Read also all about Linux signals.
PS. An example of clever SIGSEGV
handling is offered by the no-more maintained (in May 2019) Ravenbrook MPS garbage collector library. Notice also the Linux specific and recent userfaultfd(2) and signalfd(2) system calls.