I would like to catch signal sent to my program and do simple action (e.g. exit with specified code). But if process received a signal before my signal handler set, it exited abnormally like no handler existed.
Source code of my simple program:
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void handler(int sig) { exit(0); }
int main()
{
signal(SIGTERM, handler);
while(1) { sleep(1); }
return 0;
}
Compiled with g++ my.c
command.
Script to run program continuously:
while true; do ./a.out; echo $?; done
If I manually send a signal to process then most of time everything is ok - exited with zero code. But if I use script below:
Script for frequent sending signal to process:
while true; do slay -sSIGTERM a.out; done
I've got many of Terminated 143
messages.
But if I use gcc (instead of g++) I could not get the situation with a non-zero exit code.
In this regard, the question of how do I get a C++ (g++ compiled) program similar behavior with the C (gcc compiled) program?
Preferably in the maximum standardized approach.
Tested on QNX 6.5, gcc 4.7.2