Content deleted Content added
m Task 18 (cosmetic): eval 10 templates: del empty params (1×); hyphenate params (1×); del |url-status= (1×); |
m Task 18 (cosmetic): eval 10 templates: hyphenate params (8×); |
||
Line 2:
{{More citations needed|date=August 2020}}
'''Copy-on-write''' ('''COW'''), sometimes referred to as '''implicit sharing'''<ref>{{cite web|title= Implicit Sharing|url= http://doc.qt.io/qt-5/implicit-sharing.html|website= Qt Project|
==In virtual memory management==
Line 11:
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" />
Copy-on-write pages are also used in the [[Linux kernel]]'s [[kernel same-page merging]] feature.<ref>{{cite web|last1=Abbas|first1=Ali|title=The Kernel Samepage Merging Process|url=http://alouche.net/blog/2011/07/18/the-kernel-samepage-merging-process/|website=alouche.net|
Loading the libraries for an application is also a use of copy-on-write technique. The dynamic linker maps libraries as private like follows. Any writing action on the libraries will trigger a COW in virtual memory management.
Line 27:
===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">{{citation |first=Scott |last=Meyers |author-link=Scott Meyers |year=2012 |title=Effective STL |publisher=Addison-Wesley |pages=64–65 |url=https://books.google.com/books?id=U7lTySXdFk0C&pg=PT734|isbn=9780132979184 }}</ref> but not in the newer C++11 standard:<ref>{{cite web|title=Concurrency Modifications to Basic String|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html|website=Open Standards|
<syntaxhighlight lang="cpp">
std::string x("Hello");
Line 36:
// x still uses the same old buffer
</syntaxhighlight>
In the [[PHP]] programming language, all types except references are implemented as copy-on-write. For example, strings and arrays are passed by reference, but when modified, they are duplicated if they have non-zero reference counts. This allows them to act as value types without the performance problems of copying on assignment or making them immutable.<ref>{{cite web|last1=Pauli|first1=Julien|last2=Ferrara|first2=Anthony|last3=Popov|first3=Nikita|title=Memory management|url=http://www.phpinternalsbook.com/zvals/memory_management.html#reference-counting-and-copy-on-write|website=www.phpinternalsbook.com|publisher=PHP Internals Book|
In the [[Qt (software)|Qt]] framework, many types are copy-on-write ("implicitly shared" in Qt's terms). Qt uses atomic [[compare-and-swap]] operations to increment or decrement the internal reference counter. Since the copies are cheap, Qt types can often be safely used by multiple threads without the need of locking mechanisms such as [[Mutual exclusion|mutexes]]. The benefits of COW are thus valid in both single- and multithreaded systems.<ref>{{cite web|title=Threads and Implicitly Shared Classes|url=http://doc.qt.io/qt-5/threads-modules.html#threads-and-implicitly-shared-classes|website=Qt Project|
== In computer storage ==
COW may also be used as the underlying mechanism for [[Snapshot (computer storage)|snapshots]], such as those provided by [[logical volume management]], file systems such as [[Btrfs]] and [[ZFS]],<ref>{{cite web|url=http://sakisk.me/files/copy-on-write-based-file-systems.pdf|title=Copy On Write Based File Systems Performance Analysis And Implementation|last=Kasampalis|first=Sakis|year=2010|page=19|
When implementing snapshots, there are two techniques:
|