We know that page tables are used to get the physical memory page address from the virtual memory page address. However, how the kernel knows about whether a physical memory page is shared by different processes (such as in case of a forked process and its parent process). Where is such list kept?
Share
The kernel is the entity that creates all the virtual mappings. So it knows exactly what is shared and what is not. Userspace processes can’t create shared mappings without help from the kernel.
The kernel is also responsible for duplicating the mappings when a process requests a fork. It has all the information required.
The number of mappings a page has is kept in a field in the
struct pagestructure that represents that page. It is incremented each time a new mapping is created, and decremented when a mapping disappears.If you want to dig deeper into this, you can visit Linux-MM. Especially, read Mel Gorman’s Understanding the Linux Virtual Memory Manager book (very technical) linked (freely available PDF) in the Documentation section.