首页 > 解决方案 > c中的mmap errno 22读取openmpi数据类型说明

问题描述

我正在使用 mmap 从文件中读取。
mmap 返回 errno 22,无效参数。
在这种情况下 stat.st_size 是 400 我不认为它“太大”。
我认为我没有遇到“我们不喜欢 addr、length 或 offset”。
我在 Intel Xeon E5 上运行这个程序(我认为它不相关)。
我在这里想念什么?

if( argc > 1 ) {
    struct stat stat;

    for( int i = 1; i < argc; i++ ) {
        if( access(argv[i], R_OK) == -1 ) {
            printf("\n Cannot access datatype description file %s\n", argv[i]);
            continue;
        }
        int fd = open(argv[i], O_RDONLY);
        if( fd == -1 ) {
            printf("\n Cannot open datatype description from file %s\n", argv[i]);
            continue;
        }
        if( fstat(fd, &stat) == -1 ) {
            printf("\n Cannot stat the %s file\n", argv[i]);
            continue;
        }

        void* addr = mmap(NULL, stat.st_size, PROT_READ, MAP_FILE, fd, 0);

        if( MAP_FAILED == addr ) {
            printf("\nCannot map the datatype description file %s\n", argv[i]);
            printf("%s %d stat.st_size %d\n", strerror(errno), errno, stat.st_size );
            perror("mmap");
            close(fd);
            continue;
        }

        munmap(addr, stat.st_size);
        free(addr);
        close(fd);
    }
}

标签: cmmap

解决方案


来自man 2 mmap

       MAP_FILE
              Compatibility flag.  Ignored.
...
       EINVAL flags  contained neither MAP_PRIVATE or MAP_SHARED, or contained
              both of these values.

您需要通过其中一个MAP_PRIVATEor MAP_SHARED,并且您应该停止通过MAP_FILE。(你甚至认为它做了什么?)


推荐阅读