Say we have a certain parent process with some arbitrary amount of data stored in memory and we use fork to spawn a child process. I understand that in order for the OS to perform copy on write, the certain page in memory that contains the data that we are modifying will have its Read-only bit set, and the OS will use the exception that will result when the child tries to modify the data to copy the entire page into another area in memory so that the child gets it's own copy. What I don't understand is, if that specific section in memory is marked as Read-only, then the parent, to whom the data originally belonged, would not be able to modify the data neither. So how can this whole scheme work? Does the parent lose ownership of its data and copy on write will have to be performed even when the parent itself tries to modify the data?
相关问题
- Why should we check WIFEXITED after wait in order
- Linux kernel behaviour on heap overrun or stack ov
- Is there a difference between an ISR and an interr
- Long latency instruction
- Access paging in iOS - previous and next : retriev
相关文章
- Does the Linux scheduler prefer to run child proce
- What happens to dynamic allocated memory when call
- socket() returns 0 in C client server application
- Why does the latency of the sqrtsd instruction cha
- x86 Program Counter abstracted from microarchitect
- How can I tell Linux to keep a page and not evict
- How can the linux bottom halfs execute in interrup
- How is copy paste possible?
Right, if either process writes a COW page, it triggers a page fault.
In the page fault handler, if the page is supposed to be writeable, it allocates a new physical page and does a
memcpy(newpage, shared_page, pagesize)
, then updates the page table of whichever process faulted to map the newpage to that virtual address. Then returns to user-space for the store instruction to re-run.This is a win for something like
fork
, because one process typically makes anexecve
system call right away, after touching typically one page (of stack memory).execve
destroys all memory mappings for that process, effectively replacing it with a new process. The parent once again has the only copy of every page. (Except pages that were already copy-on-write, e.g. memory allocated withmmap
is typically COW-mapped to a single physical page of zeros, so reads can hit in L1d cache).A smart optimization would be for
fork
to actually copy the page containing the top of the stack, but still do lazy COW for all the other pages, on the assumption that the child process will normallyexecve
right away and thus drop its references to all the other pages. It still costs a TLB invalidation in the parent to temporarily flip all the pages to read-only and back, though.