Qi Zhang
Qi Zhang

Reputation: 721

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

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.

Upvotes: 7

Views: 1077

Answers (1)

mik1904
mik1904

Reputation: 1365

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.

Upvotes: 9

Related Questions