Domanda

According to UNIX advance programming documentation, the SIGFPE signal terminates the program and generates a core file.

Here is my program

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

static void sig_fpe(int signo);

int
main()
{
        int pid;

        if(signal(SIGFPE, sig_fpe) == SIG_ERR)
                printf("Signal error\n");

        pid = 10;
        pid = pid / 0;

        exit(0);
}

static void
sig_fpe(int signo)
{
        if(signo == SIGFPE){

                printf("SIGFPE signal catch\n");
        }
}

When I run this program in linux machine, it prints "SIGFPE signal catch" repeatedly and does not terminate. The sample output is

SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
SIGFPE signal catch
.
.
.
.

What is the problem?

È stato utile?

Soluzione

It's a bit tricky with SIGFPE. When SIGFPE is caught the instruction will be re-executed if you simply return from handler - that's very platform dependant but the man page for signal mentions undefined behavior.

See here: Unix signals doubt - on the execution of the below progem

If you want core file as well, you will need to re-install the default signal handler, see Linux: handling a segmentation fault and getting a core dump, and enable core files (on Unix it is done with ulimit -c ...)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top