What Is Copy-on-Write?
Copy on write, also written as copy-on-write (COW), sometimes, refers to as implicit sharing or shadowing. It is a resource-management technology applied in computer programming to implement a “copy” or “duplicate” on modifiable resources efficiently.
If a resource is duplicated but not modified, it’s not necessary to create a new resource for that the resource can be shared between the copy and the original one. Still, modifications must create a copy. So, the technique – the copy operation is deferred until the first write.
By sharing resources in that way, significantly reducing the resource consumption of unmodified copies while adding a small overhead to resource-modifying operations is possible.
Applications of Copy on Write Tech
In general, copy-on-write technology can be used in three aspects.
1. Copy on Write in Virtual Memory Management
Copy-on-write is mainly used in sharing the virtual memory of operating system processes, in the implementation of the fork system call. The process usually doesn’t modify any memory and immediately executes a new process, replacing the address space entirely.
Copy on write can also be implemented using the page table by marking certain pages of memory as read-only and keeping a count of the number of references to the page. It can be extended to support efficient memory allocation by having a page of physical memory filled with zeros.
Copy-on-write pages are also applied in the Linux Kernel’s Kernel same-page merging utility. Loading the libraries for an app also takes advantage of the COW technique.
2. Copy-on-Write in Software
Besides, copy on write can be applied in the library, application, as well as system code. In multithreaded systems, COW can be used without traditional locking and instead use compare-and-swap to increment or decrement the internal reference counter.
In the PHP programming language, all types except references are implemented as copy-on-write. In the Qt framework, lots of types are COW. Since the copies are cheap, Qt types can frequently be safely used by multiple threads without the need for locking mechanisms like mutexes. Thus, the advantages of copy on write are valid in both single- and multithreaded systems.
3. Copy on Write in Computer Storage
Copy-on-write can be used as the underlying mechanism for snapshots like those offered by logical volume management, file systems including ZFS and Btrfs, as well as database servers such as Microsoft SQL Server.
Usually, the snapshots save only the changed data. They are stored near the main array. Thus, they are only a weak form of incremental backup and can’t substitute for a full backup.
Some systems also make use of copy on write to avoid the fuzzy backups, otherwise incurred when any file in the set of files being backed up changes during that backup.
Two techniques are adopted when implementing snapshots:
- Copy-on-write (COW): When a written request is made, the data are copied into a new storage area. Then, the original data are modified.
- Redirect-on-write (ROW): The original storage is never modified. When a written request is made, it is redirected away from the original data into a new storage area.
Despite their names, typically, copy on write refers to the second technology ROW. While COW does two data writes compared to ROW’s one; it’s difficult to implement efficiently and used infrequently.
Dirty COW Vulnerability
In October 2016, a vulnerability in the Linux kernel was discovered that exploited a race condition in its copy on write mechanism. It was nicknamed Dirty COW. This timing attack (CVE-2016-5195) would permit a local user to modify the read-only portion of COW data. It was detected to affect nearly every system relying on the Linux Kernel including devices running Android 7 or older. Luckily, this vulnerability was quickly patched.
Windows 11 Assistant Software Recommended
The new and powerful Windows 11 will bring you many benefits. At the same time, it will also bring you some unexpected damages such as data loss. Thus, it is strongly recommended that you back up your crucial files before or after upgrading to Win11 with a robust and reliable program like MiniTool ShadowMaker, which will assist you to protect your increasing data automatically on schedules!