Copy-on-write混淆:难道所有进程都会写入内存吗?

huangapple go评论55阅读模式
英文:

Copy-on-write Confusion: Don't all Processes write to memory anyways?

问题

在Linux内核中的写时复制(Copy on write)是在进程尝试写入由不同进程引用的页面时为其分配内存。这是一种优化,因为每次复制进程时都分配页面是昂贵的。然而,让我困惑的是,难道不是大多数进程最终都会写入页面吗?

我尝试了解从不写入页面的进程,但我找不到任何示例。

英文:

Copy on write in the Linux kernel allocates memory for a process when it attempts to write to a page that is referenced by a different process. This is an optimization because allocating pages every time a process is forked is expensive. However, the thing that I am confused about is that don't most processes eventually write to a page anyways?

I tried to learn about processes that never write to a page but I could not find any examples.

答案1

得分: 1

记住fork()可能是最常见的用法之一:与exec()函数之一结合使用。由于子进程在forkexec之间通常只有几行代码,因此它可能只会写入很少的变量,从而只触及很少的页面。这些页面必须被复制,无论如何。但绝大多数页面将不会被子进程写入,也不会在forkexec之间的短时间窗口内由父进程写入。(请记住,父进程通常正在执行wait(),在这种情况下,它也只触及很少的变量。)有了写时复制(COW),这些页面根本不需要被复制。

当子进程执行exec时,它的所有内存映射都被销毁,并且新程序被映射到它们的位置。此时,父进程的内存不再与任何其他进程共享,因此内核可以将其设置为可写,从而允许父进程在不触发任何复制的情况下使用它。

因此,COW的效果是使常见的fork/exec用例更加高效,而不会破坏子进程执行某些复杂操作的罕见情况。即使在后一种情况下,仍然可以节省一些开销,因为父进程和子进程可能不会触及它们的所有页面。

英文:

Remember what is probably the very most common use case of fork(): in combination with one of the exec() functions. Since there is usually only a few lines of code in the child between the fork and the exec, it will likely only write to a very small number of variables, thus touching only a small number of pages. Those pages must get copied, regardless. But the vast majority of pages will not be written by the child at all, nor will they be written by the parent in the short time window between fork and exec. (Keep in mind that the parent is often doing wait() in which case it is also touching very few variables.) With COW, those pages do not ever need to be copied.

When the child does exec, all its memory mappings are destroyed, and the new program is mapped in their place. At this point, the memory of the parent process is no longer shared with anybody, so the kernel can set it writable again, thus allowing the parent to use it without triggering any copying.

So COW has the effect of making the common case of fork/exec much more efficient, without breaking the uncommon case where the child does do something nontrivial. And even in the latter case, there is still some savings, because it's likely that the parent and child aren't going to touch all of their pages.

huangapple
  • 本文由 发表于 2023年7月11日 09:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定