c - mmap MAP_SHARED 不工作

标签 c file unix mmap

<分区>

我正在尝试使用 mmap 打开一个文件: 它在使用 MAP_PRIVATE 时工作正常,但 MAP_SHARED 导致无效参数错误: mmap 文件 ist 读/写

int size;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDWR);
int pagesize = sysconf(_SC_PAGE_SIZE);

/* Get the size of the file. */
int status = fstat (fd, & s);
size = s.st_size;
size += pagesize-(size%pagesize);

//mmap memory
d = mmap (0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
//error handeling
if(d == -1)
{
    perror("mmap");
    printf("Error opening file %s\n",argv[1]);
    return -1;
}

我做错了什么?

最佳答案

我发现错误的原因是我在我的本地系统上运行代码时在 VM (Parallels) 中使用 Ubuntu Linux,一切正常。似乎 Parallels 没有在它的文件系统驱动程序中实现这种内存修改...

这个问题对我帮助很大: Invalid argument for read-write mmap?

关于c - mmap MAP_SHARED 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578800/

相关文章:

bash - 使用变量作为文件名并在 bash 中写入文件

c - C 中指针的大小是否有所不同?

c - 函数范围结束后局部变量在内存中的持久性?

C编程: how do you print the contents of a file after it's been dynamically allocated?

java - 如何仅复制目录java中不存在的文件

c# - 如何使用C#将WAV文件拆分为两个或更多部分

Java - 读取文本文件时出现 FilenotfoundException

python - 在哪里找到python-2.6.0-8.9.28的xml.dom python包,我有一个Linux版本的suse/x86_64版本

c++ - 管理C++大型静态库

c - 如何在 OpenMP 中的线程之间分发数据?