Copy-on-write: Difference between revisions

Content deleted Content added
m script-assisted date audit and style fixes per MOS:NUM
Rm {{More citations needed}} tag and replace with two {{Citations needed}} tags. Rm link that is to the wrong book (a bound collection) and thus the page numbers are wrong.
Line 1:
{{Short description|Programming technique for efficiently duplicating data}}
{{Use dmy dates|date=November 2023}}
{{More citations needed|date=August 2020}}
 
'''Copy-on-write''' ('''COW'''), sometimes referred to as '''implicit sharing'''<ref>{{cite web |title=Implicit Sharing |url=https://doc.qt.io/qt-5/implicit-sharing.html |website=Qt Project |access-date=10 November 2023 }}</ref> or '''shadowing''',<ref>{{cite journal |last=Rodeh |first=Ohad |title=B-Trees, Shadowing, and Clones |journal=ACM Transactions on Storage |volume=3 |issue=4 |date=1 February 2008 |page=1 |citeseerx=10.1.1.161.6863 |s2cid=207166167 |doi=10.1145/1326542.1326544 |url=http://liw.fi/larch/ohad-btrees-shadowing-clones.pdf |archive-url=https://web.archive.org/web/20170102212904/http://liw.fi/larch/ohad-btrees-shadowing-clones.pdf |archive-date=2 January 2017 |access-date=10 November 2023 }}</ref> is a [[Resource management (computing)|resource-management]] technique used in [[computer programming]] to efficiently implement a "duplicate" or "copy" operation on modifiable [[System resource#General resources|resources]]<ref name="Linux">{{cite book |last1=Bovet |first1=Daniel Pierre |last2=Cesati |first2=Marco |date=1 January 2002 |title=Understanding the Linux Kernel |url=https://books.google.com/books?id=9yIEji1UheIC&q=%22copy%20on%20write%22&pg=PA295 |publisher=O'Reilly Media |isbn=9780596002138 |page=295 |access-date=10 November 2023 }}</ref> (most commonly memory pages, storage sectors, files, and data structures).
 
==In virtual memory management==
Copy-on-write finds its main use in [[operating system]]s, sharing the [[physical memory]] of computers running multiple [[Process (computing)|processes]], in the implementation of the [[Fork (system call)|fork() system call]]. Typically, the new process does not modify any memory and immediately executes a new process, replacing the address space entirely. It would waste processor time and memory to copy all of the old process's memory during the fork only to immediately discard the copy.{{Citation needed|date=November 2023}}
 
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 operating-system [[Kernel (operating 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.{{Citation needed|date=November 2023}}
 
The copy-on-write technique can be extended to support efficient [[memory allocation]] by keeping one 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" />
Line 20 ⟶ 19:
 
===Examples===
The [[String (C++)|string]] class provided by the [[C++ standard library]] was specifically designed to allow copy-on-write implementations in the initial C++98 standard,<ref name="meyers">{{cite book |first=Scott |last=Meyers |author-link=Scott Meyers |date=2012 |title=Effective STL |publisher=Addison-Wesley |pages=64–65 |isbn=9780132979184 |url=https://www.google.com/books/edition/Effective_C++_Digital_Collection/U7lTySXdFk0C?hl=en&gbpv=1&pg=PT734&printsec=frontcover |access-date=10 November 2023 }}</ref> but not in the newer C++11 standard:<ref>{{cite web |title=Concurrency Modifications to Basic String |url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html |website=Open Standards |access-date=10 November 2023 }}</ref>
<syntaxhighlight lang="cpp">
std::string x("Hello");