----------------------------------------------------------------------------------------
头文件
#include <unistd.h>
函数原型
int fsync(int fd);
说明
fsync函数同步内存中所有已修改的文件数据到储存设备。参数fd是该进程打开来的文件描述符。 函数成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EBADF: 文件描述词无效
EIO : 读写的过程中发生错误
EROFS, EINVAL:文件所在的文件系统不支持同步
调用 fsync 可以保证文件的修改时间也被更新。fsync 系统调用可以使您精确的强制每次写入都被更新到磁盘中。您也可以使用同步(synchronous)I/O 操作打开一个文件,这将引起所有写数据都立刻被提交到磁盘中。通过在 open 中指定 O_SYNC 标志启用同步I/O。
----------------------------------------------------------------------------------------
范例
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
const char* journal_filename = “journal.log”;
void write_journal_entry (char* entry)
{
int fd = open (journal_filename, O_WRONLY | O_CREAT | O_APPEND, 0660);
write (fd, entry, strlen (entry));
write (fd, “\n”, 1);
fsync (fd);
close (fd);
}