C – How does the kernel know that a physical page is shared by two different processes?

How does the kernel know that a physical page is shared by two different processes?… here is a solution to the problem.

How does the kernel know that a physical page is shared by two different processes?

We know that page tables are used to derive physical memory page addresses from virtual memory page addresses. However, how does the kernel know if a physical memory page is shared by different processes (for example, in the case of forking the process and its parent process). Where is such a list kept?

Solution

The kernel is the entity that creates all virtual mappings. So it knows exactly what is shared and what is not. Without the help of the kernel, user-space processes cannot create shared mappings.

The kernel is also responsible for copying the mapping when the process requests derivation. It has all the information you need.

The number of mappings for a page is saved in a field in the struct page structure that represents the page. It increments each time a new mapping is created and decrements when the mapping disappears.

If you want to dig deeper into this, you can visit Linux-MM Understanding the Linux Virtual Memory Manager Links to books (very technical) in the documentation section (PDF available free of charge).

Related Problems and Solutions