If you throw fork () and the thrown (child) process, are all VM pages still marked COW in the parent?

On Linux, if you throw fork () and forked (child), are all the virtual memory pages still marked copy-on-write in the parent?

I think that the pages will be marked as COW, because something else may be prohibitively expensive to implement, possibly requiring recalculation of the links to each page and other expensive accounting. But the other day I was wondering if I would unlock the process of executing some code in a “stable snapshot” of the current process. What happens when a child process terminates? Are all memory pages in the parent marked as copy to write? This means that branching in a process with a large amount of virtual memory (for example, 128 GB +) only to execute some code for several minutes will slow down the performance degradation in the parent process for several hours or even days (not to mention the call itself fork which would not be cheap.)

I'm just wondering what the actual behavior is in Linux (and I don't know how I can test it.)

+5
source share
2 answers

In addition to the copy-on-write bit, the page table also has a link counter. Therefore, when the children are forks, all non-personal pages in the parent are marked as COW, and the link count is incremented.

Then, while the child process is running, and the parent writes the page, it will receive a page error, and the page will be copied as you would expect, and the number of links will decrease. When a child leaves, he reduces all links to pages with one, and pages with a zero link number are thrown away.

Now, when the parent writes the page with the COW bit set and the link count to one, the COW bit is simply ignored.

+8
source

, , . ( , ), .

, , ​​, , . , , ​​ COW .

0

All Articles