You have to set the signal handler before opening the FIFO. My Unix FIFO's are a little rusty :) but I suspect that the open
is blocking for the peer to open it also. So when you kill the process, it is not actually waiting inside your loop, but inside the open
and so the signal handler does not go off.
On a different note, fclose
in a signal handler is a bad idea. What if an I/O library operation is interrupted by the signal? The library is not reentrant from signal handlers. Only certain "async safe" functions can be used from signal handlers. Antoher one is exit
, which does complicated things inside the library, many of which are not safe from an asynchronous signal handler. The _exit
function should be used instead.
Worrying whether memory leaked, if you're causing the process to exit from a signal handler, is pointless, and impossible to get "right". For one thing, malloc
itself might be interrupted by the signal handler; it is not safe to call free
to liberate anything.