LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
123
返回列表 发新帖
楼主: marvel

fork和资源的关系

[复制链接]
发表于 2005-10-9 15:43:12 | 显示全部楼层
总结一下:
这里应该解释的是interface,而不是implementation
无论implementation如何,它都是为interface服务的,它必定会保证interface的效果。implemention当然重要,只是提它实在是多余的。LKD里robert love提到现在还有提供copy-on-write页表的补丁,成熟后会进入vanilla kernel。那个时候你是不是会给别人解释:cow页表是虚拟地址一样的原因呢?
再举个例子,老师让你做作业,那你无论如何也要做完,不管是自己写还是抄别人的。并不是因为你抄袭,所以才写完作业,而是因为老师有要求,你没有选择,你必须要写完你的作业。
回复 支持 反对

使用道具 举报

发表于 2005-10-11 13:37:46 | 显示全部楼层
都是牛人啊
回复 支持 反对

使用道具 举报

发表于 2005-10-11 14:40:47 | 显示全部楼层
贴一下APUE的一段描述:

Both the child and the parent continue executing with the instruction that follows the call to fork. The child is a copy of the parent. For example, the child gets a copy of the parent's data space, heap, and stack. Note that this is a copy for the child; the parent and the child do not share these portions of memory. The parent and the child share the text segment (Section 7.6).

Current implementations don't perform a complete copy of the parent's data, stack, and heap, since a fork is often followed by an exec. Instead, a technique called copy-on-write (COW) is used. These regions are shared by the parent and the child and have their protection changed by the kernel to read-only. If either process tries to modify these regions, the kernel then makes a copy of that piece of memory only, typically a "page" in a virtual memory system. Section 9.2 of Bach [1986] and Sections 5.6 and 5.7 of McKusick et al. [1996] provide more detail on this feature.

希望可以解答lz的问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-28 14:24:18 | 显示全部楼层
谢谢楼上的“争吵”,毕竟是这个问题深入了不少!
当时的确我对虚拟地址这个东西不熟,现在看了《深入理解Linux内核》这本书的内存部分,对这个问题理解深了一步。
在此我也说一点,我看得最早稳定的Linux0.11内核中,Linux就采用了写时复制的技术,所以我认为Linux应该就是一直使用这种方法复制进程的。zhllg说的开始就复制物理内存可能是出现在早期的Unix版本中吧?
回复 支持 反对

使用道具 举报

发表于 2005-12-28 14:55:46 | 显示全部楼层
很有益的讨论,受教了
回复 支持 反对

使用道具 举报

发表于 2005-12-31 00:15:42 | 显示全部楼层
Post by realtang
在内核中,每个进程都有自己的线性区,虽然都有相同的虚地址,但所对应的物理页其实是不同的。Linux在执行fork系统调用时,并没有立即分配新的页给子进程。而是复制了父进程的进程描述符等相关的资源,重新分配一个PID给子进程,并将字进程设为可运行。并且将父进程的页标为只读。这以后,如果父进程或者子进程有写的操作。将会产生一个写保护的异常,这个异常会导致缺页异常中断处理程序的运行。这时Linux会分配一个新的页,当然这个页也可能是从Linux的页面高速缓存中直接拿来用的。这个页就给了那个产生异常的进程用了,那么被复制的页就归另外一个进程使用。这时这两个页的属性就都是可写的了。


realtang兄说的好!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表