The mmap() function shall establish a mapping between a process virtual address space and a device file or physical memory region.
A process virtual memory layout has the following sections:
Which region of Process Virtual Address Space does mmap() use for mapping?
The mmap() function establishes a mapping between a process' address space and a stream file. The address space of the process from the address returned to the caller, for a length of len, is mapped onto a stream file starting at offset off.
The mmap() system call can also be used to allocate memory (an anonymous mapping). A key point here is that the mapped pages are not actually brought into physical memory until they are referenced; thus mmap() can be used to implement lazy loading of pages into memory (demand paging).
mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.
What happens with mmap is that the data remains on disk, and it is copied from disk to memory as your process reads it. It can also be copied to physical memory speculatively.
Mmap uses "unallocated memory".
Please note that the picture you drew is unlikely to be used on any UNIX system that is younger than about 30 years. UNIX used do have that memory layout in the early 70s, but the picture is much more complicated nowdays, especially when using shared libraries.
To get an insight of what is happenning today, try (on Linux) the following command
cat /proc/self/maps
on my machine, it gives now
00400000-0040c000 r-xp 00000000 08:01 1850896 /bin/cat
0060c000-0060d000 rw-p 0000c000 08:01 1850896 /bin/cat
00adc000-00afd000 rw-p 00000000 00:00 0 [heap]
7ffe843ef000-7ffe84569000 r-xp 00000000 08:01 787567 /lib/x86_64-linux-gnu/libc-2.13.so
7ffe84569000-7ffe84769000 ---p 0017a000 08:01 787567 /lib/x86_64-linux-gnu/libc-2.13.so
7ffe84769000-7ffe8476d000 r--p 0017a000 08:01 787567 /lib/x86_64-linux-gnu/libc-2.13.so
7ffe8476d000-7ffe8476e000 rw-p 0017e000 08:01 787567 /lib/x86_64-linux-gnu/libc-2.13.so
7ffe8476e000-7ffe84773000 rw-p 00000000 00:00 0
7ffe84773000-7ffe84792000 r-xp 00000000 08:01 790578 /lib/x86_64-linux-gnu/ld-2.13.so
7ffe8495e000-7ffe84961000 rw-p 00000000 00:00 0
7ffe84990000-7ffe84992000 rw-p 00000000 00:00 0
7ffe84992000-7ffe84993000 r--p 0001f000 08:01 790578 /lib/x86_64-linux-gnu/ld-2.13.so
7ffe84993000-7ffe84994000 rw-p 00020000 08:01 790578 /lib/x86_64-linux-gnu/ld-2.13.so
7ffe84994000-7ffe84995000 rw-p 00000000 00:00 0
7fffdbaac000-7fffdbacd000 rw-p 00000000 00:00 0 [stack]
7fffdbb66000-7fffdbb67000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
It shows the memory map of the process executing the cat
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With