上文件名POSIX I / O操作的顺序一致? [英] Are Posix I/O operations on filenames sequentially coherent?

查看:165
本文介绍了上文件名POSIX I / O操作的顺序一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在修改的文件都保证是可见经过反复打开 / 接近一个POSIX标准保证呼吁相同的文件名。为表述,考虑这个Bash脚本:

I would like to know whether there is a Posix standard guarantee that modifications to a file are guaranteed to be visible through repeated open/close calls on the same file name. For exposition, consider this Bash script:

#!/bin/bash

FILE=$(mktemp)

echo "Some data" >> $FILE
cat $FILE

它是保证通过结束时间呼应,所有的数据文件中的可用?

Is it guaranteed that by the time echo finishes, all data is available in the file?

在POSIX函数而言,一个例子可能是这样的:

In terms of Posix functions, an example could be like this:

const char fn[] = "/tmp/somefile";
const char data[] = "hello world";

// Stage 1
{
   int fd = open(fn, O_CREAT);
   write(fd, data, sizeof data); // #1
   close(fd);
}

// Stage 2
{
   int fd = open(fn);
   read(fd, ...);                // #2
   close(fd);
}

时它保证在线路#1的写入是可见于读#2,或可以在操作系统的缓存的写入以便它没有及时传播?我们可以假设,没有其他进程知道文件名或以其他方式颠覆了文件查找。

Is it guaranteed that the write at line #1 is visible to the read #2, or could the OS cache the write so that it does not propagate in time? We can assume that no other process knows the file name or otherwise subverts the file lookup.

推荐答案

是的。例如。从写()规范( http://pubs.opengroup.org/onlinepubs/ 9699919799 /功能/ write.html ):

Yes. E.g. from the write() specification ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html ):


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.

请注意,它说任何读(),即可以打开()一个新的FD,并通过读取(),并提供那些相同的保障。

Note that it says "Any" read(), i.e. you can open() a new fd and read() via that, and those same guarantees are provided.

顺便说一句,而这样的强一致性使得它很容易推理的语义,这也使得提供具有不俗的性能与POSIX兼容的分布式FS非常困难,甚至是不可能的。

Incidentally, while this kind of strong consistency makes it easy to reason about the semantics, it also makes providing a POSIX compliant distributed FS with decent performance very difficult, if not impossible.

这篇关于上文件名POSIX I / O操作的顺序一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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