如何以编程方式确定是否“写入"数据?系统调用在特定文件上是原子的? [英] How does one programmatically determine if "write" system call is atomic on a particular file?

查看:97
本文介绍了如何以编程方式确定是否“写入"数据?系统调用在特定文件上是原子的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,编码人员不能依赖于原子的系统调用,例如如果文件在NFS文件系统上. (参见 NFS概述,常见问题解答和HOWTO文档).但是,大多数数据库工作最终都需要原子系统调用. (请参见数据库系统的原子性).

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).

在C(或python)中的特定FILE上是否存在一种标准的(且与操作系统无关的)确认写入(和其他系统调用)是原子的方式.

Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python).

有什么建议吗?

后续注释:管道的原子性将在下面讨论:

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

请特别注意专门处理O_APPEND的"man"页面摘录:

Note in-particular the "man" page extract dealing specifically with O_APPEND:

如果设置了文件状态标志的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.

推荐答案

调用完全没有原子性保证.因此,您不需要确认任何事情,这不是原子的.

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.

您需要使用sync系列功能来获得一些持久性保证.

You'll need to use the sync family of functions to get some durability guarantees.

这篇关于如何以编程方式确定是否“写入"数据?系统调用在特定文件上是原子的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆