我可以在 Linux 中进行写时复制 memcpy 吗? [英] 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.
我已经实现了一个跟踪更改的系统,但我认为这可能很好,如果可能的话,告诉操作系统对内存进行写时复制",并让它只处理复制那些改变的部分.然而,虽然 Linux 执行写时复制,例如在 fork() 时,我找不到控制它并自己做的方法.
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.
推荐答案
你最好的机会可能是 mmap()
将原始数据归档,然后 mmap()
再次使用 MAP_PRIVATE
相同的文件.
Your best chance is probably to mmap()
the original data to file, and then mmap()
the same file again using MAP_PRIVATE
.
这篇关于我可以在 Linux 中进行写时复制 memcpy 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!