我正在使用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);
}
}
来自man 2 mmap
:
MAP_FILE
Compatibility flag. Ignored.
...
EINVAL flags contained neither MAP_PRIVATE or MAP_SHARED, or contained
both of these values.
您需要传递MAP_PRIVATE
或MAP_SHARED
之一,并且您应停止传递MAP_FILE
。 (您甚至认为它做了什么?)