我想知道是否有Posix标准保证对文件的修改通过重复保证是可见的open
/close
调用相同的文件名。为了便于说明,请考虑以下 Bash 脚本:
#!/bin/bash
FILE=$(mktemp)
echo "Some data" >> $FILE
cat $FILE
是否保证到时候echo
完成后,文件中的所有数据都可用吗?
就 Posix 函数而言,一个例子可能是这样的:
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 行的读取可见,或者操作系统是否可以缓存该写入,以便它不会及时传播?我们可以假设没有其他进程知道文件名或以其他方式破坏文件查找。
是的。例如。来自 write() 规范(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.
请注意,它说“任何”read(),即您可以 open() 一个新的 fd 并通过它 read(),并且提供了相同的保证。
顺便说一句,虽然这种强一致性使得语义推理变得容易,但它也使得提供具有良好性能的 POSIX 兼容分布式文件系统变得非常困难,甚至是不可能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)