Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do a copy-on-write memcpy in Linux?

I have some code where I frequently copy a large block of memory, often after making only very small changes to it.

I have implemented a system which tracks the changes, but I thought it might be nice, if possible to tell the OS to do a 'copy-on-write' of the memory, and let it deal with only making a copy of those parts which change. However while Linux does copy-on-write, for example when fork()ing, I can't find a way of controlling it and doing it myself.

like image 853
Chris Jefferson Avatar asked Oct 14 '09 09:10

Chris Jefferson


People also ask

Does Linux use copy on write?

Copy-on-write pages are also used in the Linux kernel's same-page merging feature. 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.

What is copy on write mechanism in Linux?

Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data. Rather than duplicate the process address space, the parent and the child can share a single copy. The data, however, is marked in such a way that if it is written to, a duplicate is made and each process receives a unique copy.

Does copy on write save memory?

Copy On Write (abbreviated as 'COW') is a trick designed to save memory. It is used more generally in software engineering. It means that PHP will copy the memory (or allocate new memory region) when you write to a symbol, if this one was already pointing to a zval.

What is copy on write filesystem?

Copy On Write (COW) is an optimization technique for maintaining a copy of a collection of data, handling resources when multiple tasks are using the same data. The aim of this journaling file system is to bring more efficient storage management and better data integrity features to Linux.


1 Answers

Your best chance is probably to mmap() the original data to file, and then mmap() the same file again using MAP_PRIVATE.

like image 50
MSalters Avatar answered Sep 30 '22 21:09

MSalters