I developed a C application in linux that contains an infinite loop while(1)
.
There are some pointers that are dynamically allocated and are useful under the infinite loop, so the only time to deallocate memory is after interrupting the while(1)
by ctrl-z
, ctrl-c
, kill -9 apppid
, killall appname
.
So the idea is that I associate new handler that deallocates memory to the interruption events signals.
void deallocatehandler(int signal){ printf("Memory Deallocation\n"); exit(0);}
int main(){
signal(SIGINT, &deallocatehandler);
signal(SIGTSTP, &deallocatehandler);
signal(SIGKILL, &deallocatehandler);
while(1){
/**
Some code here
**/
}
}
If I press ctrl-c or ctrl-z the handler is called but the problem is with SIGKILL. The commands kill -9
and killall
doesn't launch the handler.
Has someone an idea why? and is there suggestions to correct it?
malloc()
,free()
, and evenprintf()
are not async-signal-safe and shouldn't be called from a signal handler. – Mhd