Can I ignore SIGFPE as a result of division by zero?

I have a program that intentionally performs division by zero (and stores the result in a mutable variable) to stop under certain circumstances. However, I would like to disable this stop without changing the macro, which performs division by zero.

Is there any way to ignore it?

I tried using

#include <signal.h>
...
int main(void) {
  signal(SIGFPE, SIG_IGN);
  ...
}

but he is still dying with the message "Core dumped exception."

I really don't use a value, so I don't care what is assigned to the variable; 0, random, undefined ...

EDIT: I know that this is not the most portable, but it is intended for an embedded device that runs on different operating systems. The default stop action is division by zero; other platforms require different tricks to force a reboot (for example, an endless loop with interrupts disabled). For a PC test environment (linux), I wanted to disable the division stop by zero, without relying on things like assert.

+3
source share
8 answers

Why do you intentionally divide by zero to stop? Could you exit(-1)or some equivalent?

Especially if you do not need the result of dividing by 0, it just does not make sense.

+5
source

, sigaction (2) signal().

-, SIGFPE , , SIGTERM SIGUSR1, , . , sigaction (2) NOTES SIGFPE, , .

, raise (3) , . sa_handler sigaction, , (SIG_IGN) (SIG_DFL) .

int main(void) {
  struct sigaction my_action;

  my_action.sa_handler = SIG_IGN;
  my_action.sa_flags = SA_RESTART;
  sigaction(SIGUSR1, &my_action, NULL);

  raise(SIGUSR1);  /* Ignored */

  my_action.sa_handler = SIG_DFL;
  sigaction(SIGUSR1, &my_action, NULL);

  raise(SIGUSR1); /* Terminates */

  return 0;
}

, , exit().

+5

signal(SIGFPE, SIG_IGN);

SIG_ERR, errno, , . , .

, , . , coredump - , ? assert NDEBUG.

+2

, x86 FPU:

gcc Linux ( , ):

#include <fpu_control.h>

_FPU_SETCW (_FPU_DEFAULT);

_FPU_DEFAULT _FPU_MASK_ZM (ZM ).

+1
void handler(int trapId)
{
   // Whatever
}

signal(SIGFPE, handler);
+1

? :

#include <signal.h>

#ifdef DEBUG
#define DIE_HERE raise(SIGFPE)
#else
#define DIE_HERE
#endif

, , - . , , , . , gdb, gcore .

0

, , . , (.. signal(SIGFPE, &EmptyFunction)), - undefined, , C.

0
source

lnmiit l above the failure code, then I also recommend changing the code to die in a different way. If you do not, you can try installing

0
source

All Articles