//
// sigfpe.c
//
// Chen Qiao
// ID: 110020789
//
/*
Study the SIGFPE signal.
Write a simple program to demonstrate generating the SIGFPE signal (divide by zero for example). Your program should catch the signal and display a message.
If your signal handler simply displays a message but do not terminate the program, what would happen? Investigate your observation (google online) and provide a brief answer to what you observed.
*/
/*
Answer of question
Observation:
If the signal handler simply displays a message but do not terminate the program,
the statements inside the signal handler would be executed for infinite times.
In my program, “Got SIGFPE signal (8)” is repeatedly printed to standard output.
Explanation:
While the function of signal handler is called, the enter point of this function
in main is saved. If a statement of exiting the process is not included in the
signal handler, the enter point will be restored after execution of signal handler
function, causing an infinite loop of triggering and executing the signal handler.
*/
#include
#include
#include
void sigfpe_handler(int signal){
printf(“Got SIGFPE signal (%d)\n”, signal);
sleep(1);
}
int main(int argc, char *argv[]){
int a=1;
int b=0;
signal(SIGFPE, sigfpe_handler);
int c=a/b;
return 0;
}