How to use SIGFPE with signal?

后端 未结 2 1980
天涯浪人
天涯浪人 2021-01-27 13:55

I just informed myselve about \"signals\" in C/C++ and played around. But i have a problem to understand the logic of SIGFPE.

I wrote a little program which

2条回答
  •  [愿得一人]
    2021-01-27 14:22

    Divide by zero is undefined behaviour. So whether you have installed a handler for SIGFPE or not is of little significance when your program invokes undefined behaviour.

    POSIX says:

    Delivery of the signal shall have no effect on the process. The behavior of a process is undefined after it ignores a SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal that was not generated by kill(), sigqueue(), or raise().

    A signal is raised as a result of an event (e.g. sending SIGINT by pressing CTRL+C) which can be handled by the process if said event non-fatal. SIGFPE is an erroneous condition in the program and you can't handle that. A similar case would be attempting to handle SIGSEGV, which is equivalent to this (undefined behaviour). When your process attempts to access some memory for which it doesn't have access. It would be silly if you could just ignore it and carry on as if nothing happened.

提交回复
热议问题