Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the data in fwrite() buffer be flushed when a program exists abnormally?

Tags:

c

file

linux

io

fwrite() is a library call that firstly buffers the data into a user space buffer, and then calls the write() system call later to actually carry out the write operations.

If a program invokes fwrite() to write some data to a file but then exists abnormally, will the buffer of fwrite() be clearedflushed, or the buffered data will be left over in memory?

The OS I am considering is Linux.

like image 524
Qi Zhang Avatar asked Jun 17 '16 15:06

Qi Zhang


People also ask

Does Fclose flush the buffer?

NOTES top. Note that fclose() flushes only the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

Is Fwrite buffered?

The ANSI/ISO fread/fwrite functions are buffered.


1 Answers

If your program exited abnormally, any buffered data will not be flushed. The OS just says "oh dear me, you left a file descriptor open, I better close that for you" when the process terminates; it has no idea there's some random data lying somewhere in memory that the program intended to write to disk but did not.

like image 106
mik1904 Avatar answered Sep 28 '22 08:09

mik1904