mmap
reserves the virtual address space of the process, but does not immediately allocate physical memory for it. Therefore, on a 64-bit platform, you can reserve a huge amount without failures (although you still need to check for a failure, there is no code in your example). Physical pages of RAM are allocated later when accessing memory.
mprotect
simply changes the read / write access of the reserved memory; he will not force him to remain in RAM either. You will get the same effect by passing PROT_READ | PROT_WRITE
PROT_READ | PROT_WRITE
instead of PROT_NONE
in mmap
and deleting the mprotect
call.
If you need RAM located in RAM immediately, use mlock
for this. It will not work if access to RAM is insufficient. On many Linux platforms (including Ubuntu) there is a resource limit ( RLIMIT_MEMLOCK
) that limits the amount of memory that any process can block; you can configure it with ulimit -l
.
source share