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