在MMAP ENODEV错误 [英] ENODEV error in MMAP

查看:396
本文介绍了在MMAP ENODEV错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个新的文本文件(作为参数)的一个简单的映射和我得到的MMAP调用一个ENODEV错误。在FD是确定(以公开征集没有错误)。

根据文档这个错误的意思是指定的文件的基础文件系统不支持内存映射。或从其他来源我发现,它可能意味着fd是一个特殊的文件的文件描述符(一​​个可能被用于映射任一I / O或设备存储器)。我不明白为什么所有的这些原因会出现这种情况。

 的#include< SYS / mman.h>
#包括LT&; SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&; SYS / types.h中>#定义尺寸1 10240INT主(INT ARGC,CHAR *的argv []){
    字符*地址;
INT的fd;mode_t模式= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;如果(FD =打开(的argv [1],O_RDWR | O_CREAT | O_TRUNC,模式)== -1){
    的printf(错误@开\\ n);
}地址=(字符*)MMAP(NULL,尺寸1,PROT_READ | PROT_WRITE,MAP_SHARED,FD,0);
...
在munmap(地址,尺寸1);
返回0;
}


解决方案

这行被打破:

如果(FD =打开(的argv [1],O_RDWR | O_CREAT | O_TRUNC,模式)== -1){

您需要添加分配加上括号,因为比较操作符 == 具有更高的precedence比赋值运算符 = 。试试这个:

IF((FD =打开(的argv [1],O_CREAT | O_TRUNC,模式))== -1){

I'm trying to do a simple mapping of a new text file (given as a parameter) and I'm getting an ENODEV error in the mmap call. The fd is ok (no error in open call).

According to the documentation this error means "The underlying file system of the specified file does not support memory mapping." or from another source I found that it can mean that fd is a file descriptor for a special file (one that might be used for mapping either I/O or device memory). I don't understand why any of these reasons would be the case.

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>

#define SIZE1 10240

int main(int argc, char *argv[]){
    char *addr;
int fd;

mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

if(fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, mode) == -1){
    printf("error @ open\n");       
}

addr = (char*) mmap(NULL, SIZE1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
...
munmap(addr, SIZE1);
return 0;
}

解决方案

This line is broken:

if(fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, mode) == -1){

You need to add parentheses around the assignment, because the comparison operator == has a higher precedence than the assignment operator =. Try this instead:

if ((fd = open(argv[1], O_CREAT | O_TRUNC, mode)) == -1) {

这篇关于在MMAP ENODEV错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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