0

On Solaris 10 as well as Linux, I am using mmap call to create a memory mapped file and subsequently read the file from a separate process. For large memory mapped file, during reading (no writing), I am getting ENOMEM. What could be the reason and what could be remedy or way forward? I thought memory mapped file is not occupying memory for the entirety.

I am using the following call:

char * segptr = (char *) mmap(0,sz,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);

where,sz is the file size and fd is file descriptor of file opened through open.

I am getting ENOMEM failure while trying to reserve space for the entirety.

ulimit -a shows:

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 29995
virtual memory          (kbytes, -v) unlimited

Can I map partial file? If I open partial file, then will I able to access the whole contents on-demand? I have not used setrlimit to set any limit, so I guess, using the default (don't know what is the default), should I increase that? Please guide. How do I map the file in smaller chunks to save over usage of memory and thus avoiding ENOMEM?

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • 5
    With what `mmap()` flags? What is the value of [`RLIMITVMEM`](https://docs.oracle.com/cd/E23824_01/html/821-1463/getrlimit-2.html#scrolltoc)? `ENOMEM` is also returned when the current process' does not have sufficient continuous free address space for the mapping (e.g. you cannot map more than 2GB in a 32bit process and similar). The solution usually is: Do not map the whole file at once. – dhke Feb 16 '17 at 17:29
  • @dhke edited my Qs – Dr. Debasish Jana Feb 16 '17 at 18:03
  • `how do I map partial file?` -- you can map area of file by adjusting size (`sz`) and offset (last argument to `mmap`) in `mmap` call. Also, please distinguish _memory_ and _address space_, they impose different limitations. – myaut Feb 16 '17 at 18:10
  • @myant please provide with an example to illustrate – Dr. Debasish Jana Feb 16 '17 at 18:13
  • 1
    Check the output of `ulimit -a`, max memory size and virtual memory. – Maxim Egorushkin Feb 16 '17 at 18:14
  • 1
    @dhke: My solution is usually "use a 64 bit OS, it's 2017 for crying out loud, virtual address space is effectively infinite!" – ShadowRanger Feb 17 '17 at 03:16
  • My remnants of Solaris knowledge stop here, I am afraid... – Maxim Egorushkin Feb 17 '17 at 10:38

0 Answers0