Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Fsync for windows c++

Tags:

c++

windows

fsync

Is there an alternative to fsync for windows? (C++ builder)

Fsync required to include unistd.h and it's only for unix systems

Thanks!

like image 537
user687459 Avatar asked Oct 14 '15 18:10

user687459


People also ask

What is fsync in C?

The fsync is used to coordinate a file's in-core state with any storage device. The fsync() system call passes (“flushes”) all altered in-core content of (i.e., altered buffer for) the file descriptor fd to a disc machine (or any other perpetual storing device) where such a file remains.

Does Fflush call fsync?

But is that the case: is there a call to fsync from fflush ? No, calling fflush on a POSIX system does not imply that fsync will be called. Saying that the data is to be written, rather than that is is written implies that further buffering by the host environment is permitted.

What Fflush and fsync functions do?

For z/OS UNIX file system files, the fflush() function copies the data from the runtime buffer to the file system. The fsync() function copies the data from the file system buffer to the storage device.

What does fsync do in Linux?

The fsync() function is intended to force a physical write of data from the buffer cache, and to assure that after a system crash or other failure that all data up to the time of the fsync() call is recorded on the disk.


2 Answers

From the man-page:

fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.

The mentioned write function tells the operating system what the contents of the file should be. At this point all changes will be held in filesystem caches before actually being committed to disk.

The POSIX function fsync() tells the operating system to sync all changes from its caches to disk. As others have said you can use FlushFileBuffers on the Windows platform.

like image 51
Ruud Althuizen Avatar answered Sep 22 '22 18:09

Ruud Althuizen


_commit looks about right, it takes a file descriptor just like fsync does:

The _commit function forces the operating system to write the file associated with fd to disk. This call ensures that the specified file is flushed immediately, not at the operating system's discretion.

like image 42
James Clark Avatar answered Sep 20 '22 18:09

James Clark