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

我爱逗瞿瞿 2014-09-21 12:27:19
使用qt在linux上开发对外接存储设备的读写程序,但如果不使用文件同步,可能在拔掉外设时实际数据并未同步到外设存储设备上,听说用sync,但又听说sync并不是在同步完成才返回,所以也可以不保险,不知qt的flush函数是否可以,是否是在刷入数据后才返回,保证了数据完整
...全文
1110 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.
内容概要:本文系统研究了基于雅可比伪逆迭代的双臂协同轨迹规划与跟踪误差收敛性问题,结合机械臂正向与逆向运动学建模、蒙特卡洛法分析工作空间、笛卡尔空间内的圆弧轨迹生成等关键技术,利用Matlab实现了双臂系统的协同控制与轨迹跟踪仿真。重点探讨了雅可比矩阵伪逆法在解决冗余自由度机械臂逆运动学问题中的应用,深入分析了轨迹跟踪过程中误差的收敛特性,并通过仿真实验验证了该方法在复杂任务场景下的有效性、稳定性与鲁棒性,为多臂机器人在工业自动化、精密操作等领域的协同作业提供了理论依据与技术支持。; 适合人群:具备机器人学、自动控制理论基础及Matlab编程能力的科研人员与工程技术人员,特别适合从事机械臂控制、轨迹规划、多机器人协同等方向的硕士、博士研究生及相关领域研究人员。; 使用场景及目标:①掌握基于雅可比伪逆法的机械臂逆运动学求解原理与实现方法;②理解双臂系统在笛卡尔空间中协同轨迹规划的机制;③分析并优化轨迹跟踪过程中的误差收敛性能;④为多机器人系统在复杂动态环境下的高精度协同控制提供算法设计与仿真验证支持。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,按照文档目录循序渐进地学习,重点关注算法实现细节、仿真参数设置与结果分析部分,同时可进一步探索将智能优化算法或深度学习方法引入轨迹规划与误差补偿的研究方向。

23,224

社区成员

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

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