Content deleted Content added
m Rm duplicate links |
Update storage description to make clear entire file is not copied when modified. Tags: Visual edit Mobile edit Mobile web edit |
||
(19 intermediate revisions by 13 users not shown) | |||
Line 1:
{{Short description|Programming technique for efficiently duplicating data}}
{{
'''Copy-on-write''' ('''COW'''),
==In virtual memory management==
Copy-on-write finds its main use in [[operating system]]s, sharing the [[
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.
The copy-on-write technique can be extended to support efficient [[memory allocation]] by
Copy-on-write pages are also used in the [[Linux kernel]]'s [[Kernel same-page merging|same-page merging]] feature.<ref>{{
==In software==
{{
COW is also used in [[Library (computer science)|library]], [[Application software|application]], and [[System software|system]] code.
===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
<syntaxhighlight lang="cpp">
std::string x("Hello");
Line 28:
</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 |date=2013 |url=
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 [[Multithreading (computer architecture)|multiple threads]] without the need of [[Lock (computer science)|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 |website=Qt Project |url=
==In computer storage==
COW is used as the underlying mechanism in file systems like [[ZFS]], [[Btrfs]],<ref>{{cite web |last=Kasampalis |first=Sakis |date=2010 |title=Copy-on-Write Based File Systems Performance Analysis and Implementation |page=19 |url=https://sakisk.me/files/copy-on-write-based-file-systems.pdf |access-date=10 November 2023 |archive-date=5 May 2024 |archive-url=https://web.archive.org/web/20240505220510/https://sakisk.me/files/copy-on-write-based-file-systems.pdf |url-status=live }}</ref> [[ReFS]], and [[Bcachefs]], as well as in [[logical volume management]] and database servers such as [[Microsoft SQL Server#Replication Services|Microsoft SQL Server]].
In traditional file systems, modifying a file overwrites the original data blocks in place. In a copy-on-write (COW) file system, the original blocks remain unchanged. When part of a file is modified, only the affected blocks are written to new locations, and metadata is updated to point to them, preserving the original version until it’s no longer needed. This approach enables features like [[Snapshot (computer storage)|snapshots]], which capture the state of a file at a specific time without consuming much additional space. Snapshots typically store only the modified data and are kept close to the original. However, they are considered a weak form of [[incremental backup]] and cannot replace a full backup.<ref>{{cite web |last=Chien |first=Tim |title=Snapshots Are NOT Backups |url=https://www.oracle.com/database/technologies/rman-fra-snapshot.html |website=Oracle.com |publisher=Oracle |access-date=10 November 2023 |archive-date=10 November 2023 |archive-url=https://web.archive.org/web/20231110024434/https://www.oracle.com/database/technologies/rman-fra-snapshot.html |url-status=live }}</ref>
==See also==
Line 41 ⟶ 43:
* [[Memory management]]
* [[Persistent data structure]]
* [[Wear leveling]]
==References==
{{Reflist}}
==External links==
* {{cite web |title=A history of copy-on-write memory management |url=https://obvious.services.net/2011/01/history-of-copy-on-write-memory.html |website=A keen grasp of the obvious |access-date=18 November 2024 |date=21 January 2011}}
{{File systems}}
|