since you already got some answers, my part of wisdom is this.
I was once backfired when my program misbehaved because of calling a whole bunch of code inside a signal handler, please look at this post
Do's and Dont's inside a signal handler, you will get some idea.
Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.