Force write of a file to disk

后端 未结 3 1038
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 12:08

I\'m currently implementing a ping/pong buffering scheme to safely write a file to disk. I\'m using C++/Boost on a Linux/CentOS machine. Now I\'m facing the problem to force

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 12:10

    fflush (for FILE*), std::flush (for IOStream) to force your program to send to the OS.

    POSIX has

    • sync(2) to ask to schedule writing its buffers, but can return before the writing is done (Linux is waiting that the data is send to the hardware before returning).

    • fsync(2) which is guaranteed to wait for the data to be send to the hardware, but needs a file descriptor (you can get one from a FILE* with fileno(3), I know of no standard way to get one from an IOStream).

    • O_SYNC as a flag to open(2).

    In all cases, the hardware may have it's own buffers (but if it has control on it, a good implementation will try to flush them also and ISTR that some disks are using capacitors so that they are able to flush whatever happens to the power) and network file systems have their own caveat.

提交回复
热议问题