Copy-on-write: Difference between revisions

Content deleted Content added
In high-reliability software: This reads like an advertisement.
Line 7:
Copy-on-write finds its main use in sharing the [[virtual memory]] of [[operating system]] [[computer process|process]]es, in the implementation of the [[Fork (system call)|fork system call]]. Typically, the process does not modify any memory and immediately executes a new process, replacing the address space entirely. Thus, it would be wasteful to copy all of the process's memory during a fork, and instead the copy-on-write technique is used.
 
Copy-on-write can be implemented efficiently using the [[page table]] by marking certain pages of [[computer storage|memory]] as read-only and keeping a count of the number of references to the page. When data is written to these pages, the [[kernel (computingoperating system)|kernel]] intercepts the write attempt and allocates a new physical page, initialized with the copy-on-write data, although the allocation can be skipped if there is only one reference. The kernel then updates the page table with the new (writable) page, decrements the number of references, and performs the write. The new allocation ensures that a change in the memory of one process is not visible in another's.
 
The copy-on-write technique can be extended to support efficient [[memory allocation]] by having a page of [[physical memory]] filled with zeros. When the memory is allocated, all the pages returned refer to the page of zeros and are all marked copy-on-write. This way, physical memory is not allocated for the process until data is written, allowing processes to reserve more virtual memory than physical memory and use memory sparsely, at the risk of running out of virtual address space. The combined algorithm is similar to [[demand paging]].<ref name="Linux" />