douwei3280
2013-05-17 12:14
采纳率: 0%
浏览 47
已采纳

此mmap系统调用有什么问题?

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?

图片转代码服务由CSDN问答提供 功能建议

这是尝试 mmap 一个文件并写入一个字节:

 包main 
 
import(
“ fmt” 
“ os” 
“ syscall” 
)
 
func main(){
文件,_:  = 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)
} 
   
 
 <  p>尽管长度设置为100, mmap 容量始终为0。系统调用出了什么问题? 
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpcj32769 2013-05-17 12:21
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxing1412 2013-05-17 12:20

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题