我一直在使用openSUSE 11.2 x86_64处理大型稀疏文件。当我尝试对一个1TB的稀疏文件进行mmap()时,会出现ENOMEM错误。我本以为64位地址空间足以映射到1TB,但似乎不是这样。进一步实验表明,1GB的文件可以正常工作,但2GB的文件(以及任何更大的文件)都会失败。我猜测可能有某个设置需要调整,但广泛搜索没有发现任何信息。
以下是一些示例代码,显示了问题-有什么线索吗?
以下是一些示例代码,显示了问题-有什么线索吗?
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char * filename = argv[1];
int fd;
off_t size = 1UL << 40; // 30 == 1GB, 40 == 1TB
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
ftruncate(fd, size);
printf("Created %ld byte sparse file\n", size);
char * buffer = (char *)mmap(NULL, (size_t)size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if ( buffer == MAP_FAILED ) {
perror("mmap");
exit(1);
}
printf("Done mmap - returned 0x0%lx\n", (unsigned long)buffer);
strcpy( buffer, "cafebabe" );
printf("Wrote to start\n");
strcpy( buffer + (size - 9), "deadbeef" );
printf("Wrote to end\n");
if ( munmap(buffer, (size_t)size) < 0 ) {
perror("munmap");
exit(1);
}
close(fd);
return 0;
}
1 << 38
),而超过这个大小会返回EINVAL
。这是在RHEL4(内核2.6.9-42.0.3.ELsmp)上进行的测试。 - caf