which signal will flush the file buffer in C/C++

which signal will flush the file buffer in C/C++?
eg. send a signal to flush all the file buffer to a file/stdout with out invoking fflush().
-INT, -TERM?
someone please help me, thanks in advance!
waiting online...

That's an unusual question... why?

There is no signal to specifically flush all buffers, unless (i) a particular application includes a facility for specifically doing that or (ii) you don't mind terminating the program, in which case any signal that causes normal termination will flush buffers before exiting. See "man 7 signal".

1 Like

en, I mean flush the buffer and then terminate the process.
sometime, some log info will miss if sending a -TERM signal.

Your application has to handle what occurs when it gets a TERM signal otherwise the default behavior occurs.

1 Like

yes, then what is the defaut behavior in detail of threse signals?
for example, the default action of TERM is to terminate the process, then my question is what the detail procedures of the default action are , will it do something extra before actually killing the process?

yes, then what is the defaut behavior in detail of threse signals?
for example, the default action of TERM is to terminate the process, then my question is what the detail procedures of the default action are , will it do something extra before actually killing the process?

Unless otherwise handled? Gracelessly kill the program. A very few signals have special hardwired behavior, like SIGSTOP and SIGCONT, but these trigger behavior that happens outside the program and are uncatchable. And no, before you ask a third time, none of them are for flushing the queues(which happens in userspace anyway).

If you want a special "flush the queues" signal you have to make it yourself. Better yet, fix your application so that it catches SIGTERM properly and quits gracefully instead of having to be killed hard.

1 Like

OK,I think I'll write a small program to find out what'll happen.
you guys,thanks any way.

Overkill when you don't know what files are open:

void sig_handler(int sig)
{
    int i=0;
    if(sig==SIGTERM)
   {
       signal(SIGTERM, SIG_IGN); 
       sync();
       for(i=0; i< getdtablesize(); i++) close(i);
       exit(1);
   }
}

...........
int main()
{

    signal(SIGTERM, sig_handler);
}

exit() flushes all buffers, closes files, and removes tempfiles, so the loop is complete overkill.