【发布时间】:2013-11-18 19:26:34
【问题描述】:
我正在阅读 golang 的内存映射文件实现之一,https://github.com/edsrzf/mmap-go/。首先他描述了几种访问模式:
// RDONLY maps the memory read-only.
// Attempts to write to the MMap object will result in undefined behavior.
RDONLY = 0
// RDWR maps the memory as read-write. Writes to the MMap object will update the
// underlying file.
RDWR = 1 << iota
// COPY maps the memory as copy-on-write. Writes to the MMap object will affect
// memory, but the underlying file will remain unchanged.
COPY
但是在 gommap 测试文件中我看到了这个:
func TestReadWrite(t *testing.T) {
mmap, err := Map(f, RDWR, 0)
... omitted for brevity...
mmap[9] = 'X'
mmap.Flush()
那么,如果访问模式是RDWR,为什么他需要调用Flush来确保将内容写入文件呢?
或者是操作系统管理这个,所以它只在它认为应该写的时候写?
如果是最后一个选项,请您更详细地解释一下 - 我读到的是,当操作系统内存不足时,它会写入文件并释放内存。这是正确的吗?它仅适用于 RDWR 还是仅适用于 COPY?
谢谢
【问题讨论】:
标签: go mmap memory-mapped-files