0% found this document useful (0 votes)
217 views

Copy On Write

Modern operating systems like Linux employ a technique called copy-on-write to optimize the process of forking by avoiding unnecessary copying of memory pages. With copy-on-write, the parent and child process initially share the same read-only memory pages, until either process attempts to modify a page, at which point a copy is made for the writing process. This lazy approach means copying only occurs when needed, improving performance over the traditional method of immediately copying all memory pages during forking.

Uploaded by

chamed1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views

Copy On Write

Modern operating systems like Linux employ a technique called copy-on-write to optimize the process of forking by avoiding unnecessary copying of memory pages. With copy-on-write, the parent and child process initially share the same read-only memory pages, until either process attempts to modify a page, at which point a copy is made for the writing process. This lazy approach means copying only occurs when needed, improving performance over the traditional method of immediately copying all memory pages during forking.

Uploaded by

chamed1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Copy-on-Write

In early Unix systems, forking was simple, Upon invocation, the kernel created copies of all internal data structures, duplicated the process page table entries, and then performed a page-by-page copy of the parents address space into the childs new address space. But this page-by-page copy was, at least from the standpoint of the kernel, time-consuming.

Modern Unix systems behave more optimally. Instead of a wholesale copy of the parents address space, modern Unix systems such as Linux employ copy-on-write (COW) pages.

Lazy optimization design to mitigate(ease) the overhead of duplicating resources. if multiple consumers request read access to their own copies of a resource, duplicate copies of the resource need not be made. Instead, each consumer can be handed a pointer to the same resource

So long as no consumer attempts to modify its copy of the resource, the illusion of exclusive access to the resource remains, and the overhead of a copy is avoided. If a consumer does attempt to modify its copy of the resource, at that point, the resource is transparently duplicated, and the copy is given to the modifying consumer. The consumer, never the wiser, can then modify its copy of the resource while the other consumers continue to share the original, unchanged version. Hence the name: the copy occurs only on write.

The primary benefit is that if a consumer never modifies its copy of the resource, a copy is never needed. The general advantage of lazy algorithms that they defer expensive actions until the last possible momentalso applies.

At the completion of a fork, the parent and child believe that they each have a unique address space, while in fact they are sharing the parents original pageswhich in turn may be shared with other parent or child processes, and so on!

The kernel implementation is simple. The pages are marked as read-only and as copy-on-write in the kernels page-related data structures. If either process attempts to modify a page, a page fault occurs. The kernel then handles the page fault by transparently making a copy of the page; at this point, the pages copy-on-write attribute is cleared, and it is no longer shared.

Because modern machine architectures provide hardware-level support for copy on-write in their memory management units (MMUs), the charade(simulation) is simple and easy to implement.

Copy-on-write has yet a bigger benefit in the case of forking. Because a large percentage of forks are followed by an exec, copying the parents address space into the childs address space is often a complete waste of time: if the child summarily executes a new binary image, its previous address space is wiped out. Copy-on-write optimizes for this case.

You might also like