用于读写的mmap无效的参数? [英] Invalid argument for read-write mmap?

查看:757
本文介绍了用于读写的mmap无效的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 -EINVAL 出于某种原因,这不是很清楚,我为什么。在此处,我打开并试图 MMAP 文件:

I'm getting -EINVAL for some reason, and it's not clear to me why. Here's where I open and attempt to mmap the file:

if ((fd = open(argv[1], O_RDWR)) < 0)
{
    fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
    return 1;
}

struct stat statbuf;
if (fstat(fd, &statbuf))
{
    fprintf(stderr, "stat filed: %s\n", strerror(errno));
    return 1;
}

char* fbase = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fbase == MAP_FAILED)
{
    fprintf(stderr, "mmap failed: %s\n", strerror(errno));
    return 1;
}

编辑:我要补充,在错误中出现的 MMAP

推荐答案

<击>原来改变了 MAP_SHARED MAP_PRIVATE 允许这种成功。

Turns out changing the MAP_SHARED to MAP_PRIVATE allows this to succeed.

这个原因,这是失败是微妙:我的code是VirtualBox的VM内运行,而我试图 MMAP 该文件是在一个共享目录上我的主机。 VirtualBox的虚拟文件系统显然不落实 MMAP 与整个虚拟机管理程序的边界 MAP_SHARED 选项。

This reason this was failing is subtle: My code is running inside a VirtualBox VM, and the file I was attempting to mmap was in a shared directory on my host machine. The VirtualBox virtual filesystem apparently doesn't implement mmap with the MAP_SHARED option across the boundary of the hypervisor.

如果你阅读我的两个问题,他的回答JXH的有益的意见,事实证明,这code的工作他,因为他很可能试图 MMAP 主机文件系统文件到主机内存。

If you'll read jxh's helpful comments on both my question and on his answer, it turns out that this code was working for him because he was likely attempting to mmap a host filesystem file into the host memory.

我的观察,从 MAP_SHARED 切换到 MAP_PRIVATE 也与此一致:因为私下映射内存是看不见其他进程,虚拟文件​​系统驱动程序可能会不反对映射内存。

My observation that switching from MAP_SHARED to MAP_PRIVATE is also consistent with this: since privately mapped memory is invisible to other processes, the virtual filesystem driver will probably have no objection to mapping the memory.

解决方案是移动,我想映射到客人的硬盘驱动器,并在那里执行操作的文件。

The solution was to move the file I wanted to map into the guest's hard drive and perform manipulation from there.

这篇关于用于读写的mmap无效的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆