简体   繁体   English

叉后COW如何工作?

[英]How does COW after fork work?

I was reading about using COW -approach after doing fork in modern UNIX-like systems. 在类似现代UNIX的系统中进行fork之后,我正在阅读有关使用COW方法的信息。

Suppose we have process — P1. 假设我们有流程-P1。 It forks; 它分叉; we get another process — P2. 我们得到另一个过程-P2。 Their virtual memory is backed by the same physical pages because of COW . 由于COW,它们的虚拟内存由相同的物理页面支持。 There is a page where one static global variable(for example, static long variable; outside of main ) is located (in the .data segment) that is backed by physical page A. 在一个页面中,一个静态全局变量(例如, static long variable;位于main之外)位于(位于.data段中)一个页面,该页面由物理页面A支持。

Now P1 changes its static global variable; 现在,P1更改其静态全局变量; the kernel, after processing the protection fault, maps a new page (page B) to the virtual memory of P1 to store that changed variable. 内核在处理完保护故障后,将一个新页面(页面B)映射到P1的虚拟内存中,以存储更改后的变量。

The same way P2 changes its static global variable, the kernel, after processing the protection fault, maps a new page (page C) to the virtual memory of P1 to store that changed variable. 与P2更改其静态全局变量的方式相同,内核在处理保护故障后,将一个新页面(页面C)映射到P1的虚拟内存以存储该更改的变量。

Now nothing is referencing page A. Where is it located? 现在没有任何内容引用页面A。它在哪里? I suppose it is not "hanging in the air" keeping one physical page out of use, thus wasting memory? 我想这不是“悬而未决”,使一个物理页面无法使用,从而浪费了内存吗?

When Page B is created, the COW flag on Page A is removed because the page is no longer shared; 创建页面B后,将删除页面A上的COW标志,因为该页面不再共享。 there is no longer a need to copy it before modifying it. 不再需要在修改之前复制它。 Therefore, P2 simply uses Page A, possibly without incurring a page fault at all, and certainly without a need to copy the page. 因此,P2仅使用Page A,可能根本不会引起页面错误,当然也不需要复制页面。 Consequently, there is no Page C and Page A is not left unreferenced. 因此,没有页面C,并且页面A也没有未被引用。

Note that if P1 forks again, or if P2 forks, or both, before modifying the variable on Page A, then there might be 3 or more processes referencing the page. 请注意,如果在修改页面A上的变量之前再次P1派生,P2派生或两者兼有,则可能有3个或更多进程引用该页面。 The system usually maintains a reference counter for each page in the memory mapping control information, recording how many processes have the page mapped into their process memory, and that count controls whether the COW flag can be cleared. 系统通常在内存映射控制信息中为每个页面维护一个参考计数器,记录将多少个页面映射到其进程内存中的进程,该计数控制是否可以清除COW标志。 Until there is just one process referencing the page, the COW flag stays in effect. 在只有一个进程引用该页面之前,COW标志保持有效。

An exec operation will decrease the reference count for all the pages in the old process, and that will free the pages for reuse if the reference count goes to zero. exec操作将减少旧进程中所有页面的引用计数,并且如果引用计数变为零,则将释放这些页面以供重用。 If P1 set up some explicitly shared memory, the shared memory pages will not have the COW flag set even though the reference counter can be bigger than 1. 如果P1设置了一些显式共享内存,则即使参考计数器可以大于1,共享内存页面也不会设置COW标志。

Page A doesn't become dangling, because only one copy takes place. A页不会悬空,因为只有一个副本发生。

One of the two processes will trigger the COW first. 这两个过程之一将首先触发COW。 It will get the new frame B, and the other process sticks with A. 它将得到新的框架B,其他过程将保持不变。

We could arrange for the other process not to get a page fault. 我们可以安排其他过程以免出现页面错误。 That's probably fraught with races, particularly under SMP whereby each core has its own TLB. 这可能充满了比赛,特别是在SMP之下,每个核心都有自己的TLB。

Or we can let the other process get a page fault too. 或者我们也可以让其他进程出现页面错误。 It will know that frame A no longer requires copying because, say, there is a ref count in the management object which tracks A, and that ref count has value 1 indicating that A is uniquely mapped. 它将知道帧A不再需要复制,因为,例如,管理对象中有一个跟踪A的引用计数,并且该引用计数的值为1,表示A被唯一映射。 So the page fault handler will just mark the page as present, keeping it mapped to A. 因此,页面错误处理程序只会将页面标记为存在,并将其映射到A。

The same thing should happen if a parent spawns a child, that child exits, and then the parent touches a previously shared page. 如果父母产卵一个孩子,该孩子退出,然后父母触摸以前共享的页面,则应该发生相同的事情。 Since it is no longer shared, there is no reason to copy-on-write it. 由于不再共享,因此没有理由在写入时进行复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM