Disk-to-memory mapping is a file mapping, before we say the swap, because
This problem makes me very easy to think of Swap,linux swap is the meaning of the swap partition, in the case of memory not enough, the operating system first put the memory and the swap area of the disk to "map", and then the these memory freed up into memory, For the subsequent process of freeing up a piece of memory space, wait until the own process is awakened again, then the memory inside the disk in exchange. There is a mapping between file and memory , but Mmap is completely different from swap design, one for physical memory one for virtual memory. before we say Mmap, let's talk about the general principle of read and write files, the process call read or write will fall into the kernel, because these two functions are system calls, enter the system call, the kernel began to read and write files, Assuming the kernel reads the file, the kernel first reads the file into its own kernel space, and after reading the process returns to the user state in the kernel, the kernel reads the data into the kernel memory and then copy it into the user-state storage space of the process. We actually read the same file two times, read into kernel space, and read the user space from kernel space. mmap is a system call, the role of Mmap is to make the virtual address space of the process and the location of the file in the disk one by one map, after mapping, read and write files although the same call read and write but the mechanism of the trigger has been divided (Mmap is a member of the file_operations that is not clear), in fact, we are so said is not reasonable because the one by one mapping is not the beginning of the mmap to complete the mapping. mmap only returns a pointer to a location in the process's logical address space. This time the process is this kind, first read will be rewritten as read memory operation, reading memory, the system found that the address corresponding to the physical memory is empty, triggering a page fault mechanism, the paging mechanism first in the swap to find the corresponding pages, found no and then go through mmap Establishes a mapping relationship that reads a file from the hard disk into physical memory. That is to say, mmap maps files directly to user space without experiencing kernel space. mmap can map the file into the user's virtual memory, in fact, he can also map the memory of the device into the user's virtual memory, because we generally need the kernel to read and write devices, if the physical memory of the device directly mapped into space as described above, Save the kernel copy once.
File mapping mmap