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
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.