Mmap() is one of the ways to manage the association between {address, Physical memory, disk-blocks} All three members of this association are resources. The association is kept inside Page Table Entries (PTE's)
What mmap() actually does, is:
- [maybe] allocate an address-range inside the user process. This range must consist of consecutive addresses (should not overlap with existing ranges)
- create PTEs for the requested range and make them point to the pages within the address-range
- make the PTE's point to the file being mmap()ed
- [maybe] allocate and prefetch (some) pages
- [maybe] reserve some backing storage.
Many (3 out of 5) of the above steps are optional and depend on the actual arguments and flags supplied in the mmap() call. (the fd may be -1: creating an anonymous mapping, the start-adress may be NULL: mmap should allocate a (previously) unused range of memory)
After a call to mmap(), the pagefault-handler inside the kernel should be able to find out what to do. (attach physical ram to a page; flush and detach; allocate and COW, ...)
not reserving swapspace means that the caller trusts that there will be enough swap space at any time in the future. Swap space is shared by all the processes, so there can never be a guarantee that there is enough of it. Preallocating it (more or less) gives a guaranty that the calling process will always have enough of it. (when not: the mmap() should have failed)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…