fork产生的唯一开销是页表复制和进程ID创建 [英] the only overhead incurred by fork is page table duplication and process id creation

查看:226
本文介绍了fork产生的唯一开销是页表复制和进程ID创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fork()唯一的开销就是父页面页表的重复 并为孩子创建唯一的过程描述符. 在Linux中,实现了fork() 通过使用写时复制页面.写时复制(或COW)是一种技术 延迟或完全阻止数据的复制.

The only overhead incurred by fork() is the duplication of the parent’s page tables and the creation of a unique process descriptor for the child. In Linux, fork() is implemented through the use of copy-on-write pages. Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data.

那么为什么需要复制页表.只要这些进程以只读模式共享页面,或者直到它们写入内容,就不需要复制页面表,因为父级和子级的转换是相同的?

so why is there a need to copy page tables . as long as the processes share the pages in read only mode or until they write something there is no need that the page tables need to be copied because the translation is the same for both the parent and child??

有人可以解释一下吗..

can someone please explain..

预先感谢

推荐答案

因为COW在页面是只读的基础上工作,所以我们需要页面表的副本都是只读的.当新进程写入某处时,将页面错误视为写入只读页面的后果.页面错误处理程序查看页面的状态,确定是否应该将其写入(如果不是,则将segfault写入,就像您在原始过程中以只读方式写入一样),然后将相关的原始页面复制到新页面过程.

Because COW works on the basis that the page is read-only, so we need a copy of the page-table that is all read-only. When the new process writes to somewhere, a page-fault is taken as a consequences of writing to a page that is read-only. The page-fault handler looks at the status of the page, determines whether it's supposed to be written to (if not, segfault, just like if you write to read-only in the original process) and copies the relevant original page to the new process.

某些条目的原始页表是可读写的,因此至少必须复制这些条目.我确实相信整个页表都被复制了(因为它使其他代码更简单,并且页表项不是很大-每页4或8个字节[每4096KB加上一个条目,每4009 * 4096KB加上一个条目,等等).

The original page-table is read-write for some of the entries, so at least those will have to be copied. I do believe the entire page-table is copied (because it makes some other code simpler, and a page-table entry is not very large - four or eight bytes per page [plus one entry per 4096KB, plus one for every 4009*4096KB, etc up the hierarchy].

例如,如果我们有一些执行此操作的代码,则还有一些有趣的方面:​​

There are also some interesting aspects if, for example, we have some code that does:

 char *ptr = malloc(big_number);
 // Fill ptr[...] with some data. 
 if(!fork())
 {
      // child process works on ptr data. 
      ...
 }
 else
 {
    free(ptr);
 }

现在,父进程中的页表条目将被删除.如果要与子进程共享这些内容,则需要知道这些页表条目是共享的.

Now, the page-table entries in the parent process will be removed. If we are sharing these with the child process, we need to know that those page-table entries are shared.

通过网络接收/发送数据,写入磁盘,换入和换出页面等时,还会发生很多其他类似的问题.

Lots of other similar problems occur when receiving/sending data via network, writing to disk, swapping pages in and out, etc, etc.

这篇关于fork产生的唯一开销是页表复制和进程ID创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆