简体   繁体   English

mmap使用shm_open文件对象返回ENOMEM

[英]mmap returns ENOMEM with shm_open file object

experimenting with shm_open in linux and running into problems. 在Linux中试用shm_open并遇到问题。 i'm frequently resizing a shared memory segment with ftrunc and using mmap to remap the resized segment. 我经常使用ftrunc调整共享内存段的大小,并使用mmap重新映射已调整大小的段。 however, right around the 20 megabyte mark i get ENOMEM from mmap. 但是,就在20兆标记附近,我从mmap获得了ENOMEM。

things i have attempted to do to resolve the issue: 我试图解决该问题的事情:

first, i found out about these sysctl parameters. 首先,我发现了这些sysctl参数。 i reconfigured them: 我重新配置了它们:

kernel.shmmax = 268435456
kernel.shmall = 2097152

(shmall is specified in pages) (shmall在页面中指定)

the issue still occurred after this. 此后问题仍然存在。 investigating the details of the resize that causes the issue revealed that the call made to ftrunc to resize the shared memory object succeeded (the corresponding file in /dev/shm had the requested new size). 调查导致该问题的调整大小的详细信息后,发现对ftrunc进行的调整共享内存对象大小的调用成功(/ dev / shm中的相应文件具有请求的新大小)。

documentation from here http://pubs.opengroup.org/onlinepubs/009695399/functions/mmap.html suggests three possible causes for an ENOMEM errno: 此处的文档http://pubs.opengroup.org/onlinepubs/009695399/functions/mmap.html提出了ENOMEM错误的三种可能原因:


[ENOMEM] MAP_FIXED was specified, and the range [addr,addr+len) exceeds that allowed for the address space of a process; [ENOMEM]已指定MAP_FIXED,并且范围[addr,addr + len)超出了进程地址空间的允许范围; or, if MAP_FIXED was not specified and there is insufficient room in the address space to effect the mapping. 或者,如果未指定MAP_FIXED并且地址空间中的空间不足以影响映射。

[ENOMEM] [ML] [Option Start] The mapping could not be locked in memory, if required by mlockall(), because it would require more space than the system is able to supply. [ENOMEM] [ML] [Option Start]如果mlockall()要求,则无法将映射锁定在内存中,因为映射将需要比系统能够提供的空间更多的空间。 [Option End] [选项结束]

[ENOMEM] [TYM] [Option Start] Not enough unallocated memory resources remain in the typed memory object designated by fildes to allocate len bytes. [ENOMEM] [TYM] [Option Start]在fildes指定的类型化内存对象中没有足够的未分配内存资源来分配len个字节。 [Option End] [选项结束]


i am not using MAP_FIXED or locking, and the size of the image in /dev/shm suggests that the third reason is not the problem. 我没有使用MAP_FIXED或锁定,并且/ dev / shm中的图像大小表明第三个原因不是问题。 my mmap call looks like this: 我的mmap呼叫看起来像这样:

mmap(mem, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) mmap(内存,长度,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0)

where mem initially is 0 and thereafter refers to the last address mmap successfully mapped. 其中mem最初为0,此后指成功映射的最后一个地址mmap。

i found information suggesting that ulimit settings could be limiting the memory mappable into a single process, but i don't think the problem was here. 我发现信息表明ulimit设置可能会将可映射的内存限制为一个进程,但是我不认为问题出在这里。 just in case, ulimit -a looks like this on my machine: 以防万一,ulimit -a在我的机器上看起来像这样:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
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) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

i hope this is an easy one :) 我希望这是一个简单的:)

well, i found out what my problem was the other day. 好吧,我发现前几天是我的问题。 i misread the documentation for mmap, which says that mmap returns a mapping based on the first parameter (the previously mapped address in my case), and the result is defined by implementation. 我误读了mmap的文档,其中说mmap返回基于第一个参数(在我的情况下为先前映射的地址)的映射,并且结果由实现定义。 i took this as a suggestion that mmap might remap my previous mapping for me, but this was certainly not the case. 我认为这是mmap可能会为我重新映射以前的映射的建议,但是事实并非如此。 this might only have been the case if i had used the MAP_FIXED flag, but i avoided this because the documentation recommended against it. 如果我使用了MAP_FIXED标志,则可能只有这种情况,但是我避免了这种情况,因为文档建议使用它。 in any case, it was necessary to use munmap to remove the previous mapping before creating a new one. 无论如何,在创建新映射之前,必须使用munmap删除先前的映射。 i hope this post will help anyone making the same foolish misreading that i did 我希望这篇文章能帮助任何与我一样愚蠢的误读的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM