这个mmap系统调用有什么问题?

huangapple go评论86阅读模式
英文:

What is wrong with this mmap system call?

问题

这是一个尝试使用mmap映射文件并写入一个字节的示例代码:

package main

import (
  "fmt"
  "os"
  "syscall"
)

func main() {
  file, _ := os.Open("/tmp/data")
  mmap, _ := syscall.Mmap(int(file.Fd()), 0, 100, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
  fmt.Printf("cap is %d", cap(mmap))
  mmap[0] = 0
  syscall.Munmap(mmap)
}

尽管长度设置为100,但mmap的容量始终为0。系统调用出了什么问题?

英文:

This is an attempt to mmap a file and write a single byte:

package main

import (
  "fmt"
	"os"
	"syscall"
)

func main() {
	file, _ := os.Open("/tmp/data")
	mmap, _ := syscall.Mmap(int(file.Fd()), 0, 100, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
	fmt.Printf("cap is %d", cap(mmap))
	mmap[0] = 0
	syscall.Munmap(mmap)
}

Despite length is set to 100, mmap capacity is always 0. What went wrong in the system call?

答案1

得分: 10

始终检查错误!

os.Open仅打开文件以供读取,但mmap调用要求将文件映射为读/写,因此会导致权限被拒绝的错误,并且映射的区域大小为0。

英文:

Always check for error!

os.Open opens a file for reading only, however mmap call asks to map the file read/write, thus giving a permission denied error, and as a result mapped region size is 0.

答案2

得分: -1

文件/tmp/data是否为空?如果是的话:

我认为你不能将任意的length参数(比如你的情况下的100)传递给Mmap。我认为这个参数必须小于等于file.Size(),即由fd引用的文件的大小。如果是这种情况,那么尝试将你的数据文件变为非空,然后再试一次。

英文:

Is the file /tmp/data empty? If so:

I think you cannot pass arbitrary length parameter (like 100 in your case) to Mmap. I think this parameter must be <= file.Size(), ie the the size of the file referred to by fd. If that's the case then try to make your data file non-empty and try again.

huangapple
  • 本文由 发表于 2013年5月17日 20:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/16608754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定