保证文件操作的顺序 [英] Guarantees of order of the operations on file
问题描述
考虑我有一个文件 foo.dat
,我更新如下:
$ $ $ $ $ $ c $ lseek(fd,pos_a,SEEK_SET) ;
write(fd,data_a,size_a); //< - 操作A
lseek(fd,pos_b,SEEK_SET);
write(fd,data_b,size_b); //< - 操作B
lseek(fd,pos_c,SEEK_SET);
write(fd,data_c,size_c); //< - 操作C
这样我可以在文件A,B,C中进行更新。 - 软件崩溃或例如电源故障。
有没有任何担保,如果他们被执行的操作是以相同的顺序完成。
即那就不会有什么或A或A和B或A和B和C而不是A和C或我知道如果我在A和B之间调用 fsync(fd)
并且在A之间是相同的和C但它
也保证实际上在文件系统上。
我不关心数据的松散,而是关于它的一致性。
POSIX标准保证不会有乱序执行吗?
所以:
- 有没有这样的保证?
- 在POSIX平台上
- ?
- 如果没有什么保证(除了
fsync
),我可以拥有?
成功完成write()到一个常规文件之后eturned:
从该文件的每个字节位置成功执行read() (),直到这样的字节位置再次被修改。
任何后续的成功写入()到文件中的相同字节位置将被覆盖该文件数据。
这并不能保证你的数据将按照这个顺序依次打开磁盘。只要应用程序看到与上述两条语句一致,实现就可以重新排序所需的物理写入。
实际上,内核和即使是磁盘子系统(比如SANs)也可以重新排序(通常是因为性能原因)。
所以你不能依赖写入调用的顺序为了一致。您需要
f [data] sync
s。
PostgreSQL邮件列表上有趣的电子邮件线程: POSIX文件更新。阅读数据库如何处理I / O是了解这类问题的好方法。
(对不起,在这方面不了解Windows) / p>
I'd like to know if there any guarantees on the order of the operations on file/file system.
Consider I have a file
foo.dat
and I update it as following:lseek(fd,pos_a,SEEK_SET); write(fd,data_a,size_a); //< - Operation A lseek(fd,pos_b,SEEK_SET); write(fd,data_b,size_b); //< - Operation B lseek(fd,pos_c,SEEK_SET); write(fd,data_c,size_c); //< - Operation C
Such that I do updates in file A, B, C. And some fault may occur - software crash or for example power failure.
Is there any guarantees that the operations if they are executed are done in same order.
i.e. That there would be either nothing or "A" or "A and B" or "A and B and C"
But not situations like "A and C" or "B" only.
I know that if I call between A and B
fsync(fd)
and same between A and C but it also guarantees that is actually on file system.I less care about loose of data but rather about its consistency.
Does POSIX standard guarantees that there will be no out-of-order execution?
So:
- As there any such guarantees?
- On POSIX platform?
- On Windows platform?
- If not what guarantees (besides
fsync
) I can have?解决方案This is what POSIX mandates for write:
After a write() to a regular file has successfully returned:
Any successful read() from each byte position in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.
Any subsequent successful write() to the same byte position in the file shall overwrite that file data.
This does not provide you with a guarantee that your data will hit the disk in that order at all. The implementation can re-order physical writes all it wants as long is what the applications "see" is consistent with the above two statements.
In practice, the kernel, and even the disk subsystem (think SANs for instance) can re-order writes (for performance reasons usually).
So you can't rely on the order of your write calls for consistency. You'll need
f[data]sync
s.Interesting email thread on the PostgreSQL mailing list: POSIX file updates. Reading on how databases handle I/O is a great way to learn about this type of issue.
(Sorry, don't know about Windows in this respect.)
这篇关于保证文件操作的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!