I am getting this error in my program…
mprotect: Cannot allocate memory
ulimit -a gives the output:
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) 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) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
The amount of memory I’m trying to protect is 60 MB. Can someone tell me what is the problem and how it can be solved?
Given the error message, you probably got an
ENOMEMerror, and looking at the error code, this does not necessarily mean that memory could not be allocated. You might have an invalid address range, or (most likely) you have pages that aren’t mapped.Don’t try to protect such a big hunk of memory in one swell foop. Given how virtual memory works, the odds are just too high that some page in that huge chunk will not be mapped. You need to ensure that the page (pages) in question are mapped before calling mprotect.
When you are using system functions it is always a good idea to read the man page on that function. Then re-read it. The man pages can be a bit terse at times.