fsync()

来源:互联网 发布:c语言最大公倍数 编辑:程序博客网 时间:2024/05/16 05:23

fsync


fsync函数同步内存中所有已修改的文件数据到储存设备。

目录

  1. 1头文件
  2. 2函数原型
  3. 3说明
  4. 4范例

头文件

编辑
#include <unistd.h>

函数原型

编辑
int fsync(int fd);

说明

编辑
参数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);
}
0 0
原创粉丝点击