I was reading a textbook about how Linux manages private object as picture below shows: enter image description here

the picture shows a case where two processes have mapped a private object into different areas of their virtual memories but share the same physical copy of the object. For each process that maps the private object, the page table entries for the corresponding private area are flagged as read-only, and the area struct is flagged as private copy-on-write. as soon as a process attempts to write to some page in the private area, the write triggers a protection fault.When the fault handler notices that the protection exception was caused by the process trying to write to a page in a private copy-on-write area, it creates a new copy of the page in physical memory, updates the page table entry to point to the new copy, and then restores write permissions to the page

And we know that Linux uses vm_area_struct (area structs) to characterizes an area of the current virtual address space as: enter image description here and I think vm_flags will be set to "private" for private area so that copy-on-write will work

my question is, let's say the private area for the private object in process 2's virtual memory is called pa2. so in the beginning, process 2 tries to write a page(let's say the starting virual address is 0x404000) in pa2, which triggers a protection fault, then it is handled and new copy of the page in physical memory is created. Let's say after a while, the program needs to modify the page starting at 0x404000 again, since pa2 is still private area, another protection fault will be triggered, then another new copy of the page in physical memory is created, but what about the first new page created during first fault handler?? it is not referenced by the virtual address anymore and it will still reside in physical memory, which is a waste of resource?


When a page fault occurs, if the kernel determines that the write failed in a copy-on-write scenario, and there are at least two users of the page, it copies the original page to a new one (as you describe), and it makes the new page writable. Subsequent writes to the same page by the owning process don’t cause any page faults.

  • Thanks for your answer. But I think any write to private area will trigger a page fault, 0x404000 virtual address is still in private area, so any subsequent writes will trigger a page fault even though this fault can be handled. My intuition sugguests that any subsequent write should not trigger a page fault? – slowjams Sep 22 '20 at 0:12
  • I will be really appreciated if you could also have a look at this question unix.stackexchange.com/questions/610448/… – slowjams Sep 22 '20 at 0:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.