Program 1:
#include<stdio.h> #include<signal.h> void handler(int sig); void main() { printf("PID: %d\n",getpid()); signal(SIGABRT,handler); while(1){ printf("Hai\n"); sleep(1); abort(); } } void handler(int sig) { printf("Signal handled\n"); }
Output 1:
$ ./a.out PID: 32235 Hai Signal handled Aborted (core dumped) $
According to the standard, the interrupt function works like raise(SIGABRT) . Thus, the signal generated by the abort() function is SIGABRT. Therefore, for this, I created the above program.
This program processes the SIGABRT signal. After the signal handler is executed, it does not return to the main function from which it is called. Why does it not return to the main function after the exit of the handler?
Program 2:
#include<stdio.h> #include<signal.h> void handler(int sig); void main() { printf("PID: %d\n",getpid()); signal(SIGABRT,handler); while(1){ printf("Hai\n"); sleep(1); } } void handler(int sig) { printf("Signal handled\n"); }
Output 2:
$ ./a.out PID: 32247 Hai Hai Hai Signal handled Hai Signal handled Hai Hai ^C $
Unlike program 1, program 2 executes as expected. In the above program, signals are passed to the process through the command line through the kill command, as shown below.
$ kill -6 32247 $ kill -6 32247
So, as soon as the signal has occurred, the handler function is executed, and then returns to the main function. But this does not happen in program 1. Why does it behave this way? Function abort and SIGABRT are different?
source share