qfile的flush函数与linux系统的sync区别?

我爱逗瞿瞿 2014-09-21 12:27:19
使用qt在linux上开发对外接存储设备的读写程序,但如果不使用文件同步,可能在拔掉外设时实际数据并未同步到外设存储设备上,听说用sync,但又听说sync并不是在同步完成才返回,所以也可以不保险,不知qt的flush函数是否可以,是否是在刷入数据后才返回,保证了数据完整
...全文
794 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 woaidongdongqu 的回复:
还是不确定,flush最终引起sync,但何时引起,是数据同步后才返回吗?倒是sync命令的讲解很透彻,回去查系统内核版本就可以了,如果是1.3.20之后应该可以
I took a look into qt source codes. The situation could probably be worse than we thought. Here is the function that actually handles the flush in qfsfileengine.cpp:

bool QFSFileEnginePrivate::flushFh()
{
    Q_Q(QFSFileEngine);
 
    // Never try to flush again if the last flush failed. Otherwise you can
    // get crashes on some systems (AIX).
    if (lastFlushFailed)
        return false;
 
    int ret = fflush(fh);
 
    lastFlushFailed = (ret != 0);
    lastIOCommand = QFSFileEnginePrivate::IOFlushCommand;
 
    if (ret != 0) {
        q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError,
                    qt_error_string(errno));
        return false;
    }
    return true;
}
qt only invokes fflush, which is a STD c library call (I could be wrong, but I did see stdio.h included). On its man page: Note that fflush() only flushes the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2). From what I have seen, qt's implementation doesn't guarantee anything for data integrity in this context. You may want to follow the recommendation above to explicitly call fsync instead. Just my two cents. I am no a QT expert. BTW, here is the source code browser where I read the above codes... https://qt.gitorious.org/qt/kde-qt/source/983e244eca6cca1e11402b3af5470a07c2b22fc2:src/corelib/io/qfsfileengine.cpp#L520
我爱逗瞿瞿 2014-09-21
  • 打赏
  • 举报
回复
还是不确定,flush最终引起sync,但何时引起,是数据同步后才返回吗?倒是sync命令的讲解很透彻,回去查系统内核版本就可以了,如果是1.3.20之后应该可以
  • 打赏
  • 举报
回复
qt's flush or standard c library's flush will eventually invokes the system call sync. According to the man page: According to the standard specification (e.g., POSIX.1-2001), sync() schedules the writes, but may return before the actual writing is done. However, since version 1.3.20 Linux does actually wait. (This still does not guarantee data integrity: modern disks have large caches.) the note there regarding to the disk cache depicts a scenario that is out of hand of Linux. If a disk array has a controller with cache, when and how the data are written back to the physical disks are determined by the cache policy. Generally speaking, there are two possible policies: write back and write through. Write trough Caching. When the controller receives a write request from the host, it stores the data in its cache module, writes the data to the disk drives, then notifies the host when the write operation is complete. This process is calles write-trough caching because the data actually passes through-and is stored in- the cache memory on its way to the disk drives. Write back caching This caching technique improves the subsystem's response time to write requests by allowing the controller to declare the write operation 'complete' as soon as the data reaches its cache memory. The controller performs the slower operation of writing the data to the disk drives at a later time.

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧