I read the man page that when calling exit() all threads are automatically cleared and closed. At first I was skeptical about how this was done and whether it was really reliable, but, seeing that I canโt find out more, I agree that it just works - weโll see that something explodes. In any case, if this behavior of closing a stream is present in exit() , is this behavior also present in the default handler for SIGINT (the interrupt signal is usually triggered using Ctrl + C)? Or, it would be necessary to do something like this:
#include <signal.h> #include <stdlib.h> void onInterrupt(int dummy) { exit(0); } int main() { signal(SIGINT, onInterrupt); FILE *file = fopen("file", "a"); for (;;) { fprintf(file, "bleh"); } }
to get file to close properly? Or is it safe to skip the lines of signal(SIG... and void onInterrupt(... ?
Please limit any answers to C, C99, and POSIX since I do not use GNU libc. Thanks.
source share