简体   繁体   中英

Copy on Write - COW - what happened after the page is modified

I'm learning the copy-on-write technique. I can understand that parent and child process share the same address space. When the parent or child want to modify the page, so that page will be copied to private memory of process then modified it.

So my question is, assume that child process is modified the page, then complete and terminate. How the modified data? is it still there and visible to parent process and other child processes?

In short, if child process modified the page, and what happen next to parent and other child processes for that modified page/data?

I read the COW concepts and understand it basic principles but not sure how deep I understand.

In short - the parent does not have access to the child process data. Neither do any other siblings. The moment the child process terminates, all its modifications is lost.

Remember, COW is just an optimization. From the processes point of view, they don't even realize it is copy on write. From their perspective, each process has its own copy of the memory space.

Long answer, what happens behind the scenes:

*Note, I am simplifying some corner case, not everything is 100% accurate but this is the idea.

Each process has its own page table, which maps process virtual addresses to physical pages.

At some point, the parent process calls fork. At this step, a child process is created, its VMA descriptors are duplicated (there are certain rules on how that is done, with intermediate chain etc, not going to deep dive into this). What is important is that at this stage, child and parent virtual addresses are pointing to the same physical addresses.

Next, all pages are made read only.

At this point, if either child or parent try to write to a certain page, it will cause a page fault. They can read freely, however.

Now assume child writes to a page. This causes a page fault. This page fault is caught by the kernel. So the kernel understands it is COW now. What it does is creating a separate copy of a physical page for child.

So at this point, child and parent have same virtual address pointing to two different physical addresses.

That should answer your question. Parent cannot access other process physical pages. The virtual address is the same, but that does not matter. When child dies, its pages are recycled, and all changes are lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM