SIGFPE
|
|
It has been suggested that this article or section be merged into Unix signal . (Discuss) Proposed since January 2012. |
| Description | Erroneous arithmetic operation | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Default action | Abnormal termination of the process | ||||||||||||||||
| SA_SIGINFO macros | |||||||||||||||||
|
|||||||||||||||||
On POSIX compliant platforms, SIGFPE is the signal sent to a process when it performs an erroneous arithmetic operation. The symbolic constant for SIGFPE is defined in the header file signal.h.
Contents |
[edit] Etymology
SIG is a common prefix for signal names; FPE is an acronym for floating-point exception. Although SIGFPE does not necessarily involve floating-point arithmetic, there is no way to change its name without breaking backward compatibility.
[edit] Description
SIGFPE is sent to processes for a variety of reasons. A common example might be an unexpected type overflow (e.g., signed integer) owing to exceptional input, or an error in a program construct.
SIGFPE can be handled. That is, programmers can specify the action they want to occur on receiving the signal, such as calling a subroutine, ignoring the event, or restoring the default action.
Under certain circumstances, ignoring SIGFPE can result in undefined behaviour. In particular, the program may hang as the offending operation is forever retried. However, it is safe to ignore SIGFPE signals not actually resulting from computation, such as those sent via the kill system call.
A common oversight is to consider division by zero the only source of SIGFPE conditions. On some architectures (IA-32 included[1]), integer division of INT_MIN (the most negative representable integer value) by −1 triggers the signal because the quotient, a positive number, is not representable.
In contrast to that in C, SIGFPE (or any other signal) will never be issued for unsigned integer types. These are guaranteed by the C standard to wrap around silently. Computation involving such types is always defined modulo M+1, where M is the maximum value representable in the type.
[edit] Example
Here is an example of an ANSI C program that attempts to perform an erroneous arithmetic operation, namely integer division by zero, or FPE_INTDIV.
int main(void)
{
/* "volatile" needed to eliminate compile-time optimizations */
volatile int x = 42;
volatile int y = 0;
x=x/y;
return 0; /* Never reached */
}
Another example:
#include <limits.h>
int main(void)
{
volatile int x=INT_MIN;
volatile int y=-1;
x=x/y;
return 0;
}
Compiling and running either example on IA-32 with Linux produces the following:
$ gcc -o sigfpe sigfpe.c sigfpe.c: In function ‘main’: sigfpe.c:3: warning: division by zero $ ./sigfpe Floating point exception (core dumped)
A backtrace from gdb shows that the SIGFPE signal occurred in the main function:
Program received signal SIGFPE, Arithmetic exception. 0x08048373 in main ()
Compare the output from SIGFPE with that of a segmentation fault or the SIGILL signal for illegal instructions.
[edit] References
[edit] See also
|
||||||||