当尺寸>时,我的mmap在errno 12失败Linux 2.6.37.6上1GB。
有什么想法吗?我有64GB的ram
# swapon -s
Filename Type Size Used Priority
/dev/sda1 partition 65537160 0 -1
和
# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 495694
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 495694
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
编辑:添加了uname -a
#root@maverick-5:~# uname -a
Linux maverick-5 2.6.37.6 #5 SMP Fri Feb 14 16:21:40 PST 2014 x86_64 GNU/Linux
编辑:免费添加
#free
total used free shared buffers cached
Mem: 63459224 317692 63141532 0 25800 36280
-/+ buffers/cache: 255612 63203612
Swap: 65537160 0 65537160
编辑:它是64位系统,64GB RAM:
# cat /proc/meminfo
MemTotal: 63459224 kB ...
编辑:添加mmap代码
...
*fd = open(file_path, O_RDWR, 0644);
if (*fd==-1){
printf("open file failed \n");
exit();
}
system("ulimit -m -v");
mem = mmap(0,0x80000000,PROT_READ|PROT_WRITE,MAP_NORESERVE|MAP_PRIVATE,*fd,0);
if (mem == MAP_FAILED){
printf("map failed failed %d %d\n", errno, mem);
exit();
}
答案 0 :(得分:0)
您应该始终测试系统调用的结果。阅读mmap(2)代码
mem = mmap(0,size_of_segment, PROT_READ|PROT_WRITE,
MAP_NORESERVE|MAP_PRIVATE,*fd,0);
if (mem == MMAP_FAILED)
{ perror("mmap size_of_segment"); exit(EXIT_FAILURE); };
我们不知道size_of_segment
包含什么,*fd
是什么。实际上,我不确定在MAP_NORESERVE
时使用mmap
- 文件是一件明智的事情。使用MAP_NORESERVE
和MAP_ANONYMOUS
(保留地址空间而不占用交换空间)或省略MAP_NORESERVE
;如果要访问和修改文件,请使用MAP_SHARED
:
mem = mmap(0,size_of_segment, PROT_READ|PROT_WRITE,
MAP_SHARED,*fd,0);
if (mem == MMAP_FAILED)
{ fprintf (stderr, "mmap size_of_segment=%ld *fd=%d failed %s\n",
(long)size_of_segment, *fd, strerror(errno));
exit(EXIT_FAILURE); };
在我的系统ENOMEM
上是12,然后perror
打印(或strerror
给出)Cannot allocate memory
这是不言自明的。 mmap(2)手册页告诉:
ENOMEM No memory is available, or the process's maximum number of mappings would have been exceeded.
但是Posix mmap讲述了一个不同的故事;也许你有太多的内存映射。查看/proc/1234/maps
了解pid 1234的过程。
答案 1 :(得分:0)
问题是每进程虚拟内存限制将设置为仅1GB(使用ulimit -a检查)。 ulimit -v可以用来将它设置为60g左右,然后你的mmap()调用就会成功。