fflush has no use?

Peter Dufault dufault at hda.com
Thu Nov 7 12:57:53 UTC 2013

On Nov 6, 2013, at 22:56 , Rui Zhengxin <ruizx at qq.com> wrote:

> But unfortunately, **"fflush" seems have no use.**
> The test code is like this.

> -------------------------------------------------------
> When the above code is executed, 
> the console output is always print ok, fflush returns good.
> But when restart the board,
> then use "cat" command to display the log file, it has nothins.
> The length of the log file is zero too.
> [/mnt] # ls -l
> total 7973
> -rw-r--r--  1 root  root  1767821 Jan  1 00:03 hello.exe
> -rw-r--r--  1 root  root        0 Jan  1 00:08 log.txt
> We also find that if we use "cat" command after fflush, the file content can be stored.
What do you mean in the final sentence?  Do you mean you do see the file content when you issue the "cat" command?  You should see it.  System reads and writes can be satisfied in a a system buffer cache, they don't need to actually write through to the underlying media.

Calling fflush() will force a write to the file, it doesn't guarantee it will get written through to the media.  If you issue fsync() after the fflush() you should see it.

If you close the file before restarting the program you should see it.

I think RTEMS supports O_DSYNC. If you set that flag using fcntl() then the data should get written through when you issue the fflush() without calling fsync().

Peter Dufault
HD Associates, Inc.      Software and System Engineering

More information about the users mailing list