The handler doesn't know the file name or the path, because it doesn't use those (you can tell, because even if the file is deleted from the file system after the mapping is created, the mapping continues to work just fine; the file contents remain valid until all open file descriptors and memory mapping are closed).
It doesn't use the fd
either; you're allowed to close
the fd
passed to mmap
immediately after the mmap
call, and the mapping remains valid (this is in fact necessary on some systems with low ulimit
s for open file handles; you can map 10,000 files at once, but you couldn't hold open fd
s for all of them if the ulimit
for fds was 1000).
What happens is that, at mmap
time the virtual memory manager for the OS sets up a bunch of virtual memory tables that basically say "this memory is backed by the following disk sectors". It uses a very similar process when retrieving data that has been written to the swap file and must be read back in. The only differences are in how aggressively memory and disk are synced, whether the mapping to a particular disk sector is static or dynamic (though even for "real" files, the disk sector could change as you run, e.g., when writing to a copy-on-write file system), whether the memory must be written (swap) or can simply be dropped (mmap
-ed file w/o dirty pages) under memory pressure, etc.
There are several layers of virtual memory address translation involved that differ by CPU and OS, so the exact mechanics differ, but the basic idea is that after mmap
ing, you're bypassing the directory structure and interacting with underlying disk sectors in a way that ignores stuff like names and paths.