How to know whether a copy-on-write page is an actual copy?

前端 未结 6 769
感情败类
感情败类 2020-12-29 13:49

When I create a copy-on-write mapping (a MAP_PRIVATE) using mmap, then some pages of this mapping will be copied as soon as I write to specific addresses. At a certain point

6条回答
  •  [愿得一人]
    2020-12-29 14:29

    Copy-on-write is implemented using the memory protection scheme of the virtual memory hardware.

    When a read-only page is written to, a page fault occurs. The page fault handler checks if the page carries the copy-on-write flag: if so, a new page is allocated, the contents of the old page and copied, and the write is retried.

    The new page is neither read-only nor copy-on-write, the link to the original page is completely broken.

    So all you need to do is test the memory protection flags for the page.

    On Windows, the API is GetWorkingSet, see the explanation at VirtualQueryEx. I don't know what the corresponding linux API is.

提交回复
热议问题