如何以编程方式确定“写入”是否被写入?系统调用在特定文件上是原子的吗?
在某些情况下,编码器不能依赖原子的系统调用,例如,如果文件位于 NFS 文件系统上。 (参见 NFS 概述、常见问题解答和 HOWTO 文档)。但大多数数据库工作最终都需要原子系统调用。 (参见数据库系统的原子性)。
是否有一种标准(且独立于操作系统)方法来确认 C(或 python)中特定文件的写入(和其他系统调用)是原子的。
有什么建议吗?
后续说明:管道上的原子性将在下面讨论:
请特别注意专门处理 O_APPEND 的“man”页面摘录:
如果文件状态标志的 O_APPEND 标志被设置,则文件 偏移量应在每次写入之前设置为文件末尾,并且不 干预文件修改操作应在更改之间发生 文件偏移量和写入操作。
In some cases the coder cannot rely on system calls being atomic, e.g. if the file is on a NFS filesystem. (c.f. NFS Overview, FAQ and HOWTO Documents). But atomic system calls are ultimately required for most database work. (c.f. Atomicity of database systems).
Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python).
Any suggestions?
Subsequent notes: Atomicity on pipes is discussed in the following:
- unix pipe multiple writers
- What happens if a write system call is called on same file by 2 different processes simultaneously
Note in-particular the "man" page extract dealing specifically with O_APPEND:
If the O_APPEND flag of the file status flags is set, the file
offset shall be set to the end of the file prior to each write and no
intervening file modification operation shall occur between changing
the file offset and the write operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
POSIX 中定义的
write
调用具有根本没有原子性保证。所以你不需要确认任何事情,它不是原子的。如果成功完成,它甚至不保证数据将到达硬盘驱动器(如果有驱动器的话)。成功读回数据也不能为您提供任何保证。
您需要使用
sync
系列函数来获得一些持久性保证。The
write
call as defined in POSIX has no atomicity guarantee at all. So you don't need to confirm anything, it's not atomic.It doesn't even guarantee that the data will have reached the hard drive (if there is a drive at all) if it completes successfully. Successfully reading back the data doesn't give you any guarantees either.
You'll need to use the
sync
family of functions to get some durability guarantees.