在Linux中使用Write-On-Write在fork()之后如何父级和子级共享页面? [英] How do parent and child share pages after fork() with Copy-On-Write in Linux?

查看:64
本文介绍了在Linux中使用Write-On-Write在fork()之后如何父级和子级共享页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对fork()用例感兴趣.

I am interested in the fork() use case.

1)分叉后,内核为子进程创建一个新的PCB.它将父级中所有页表项的权限更改为只读.然后,它在新的子PCB中复制父目录的页面目录和表. 这样对吗?还是父子进程共享它们?

1) After fork, the kernel creates a new PCB for the child process. It changes the permission of all page table entries in the parent to be read-only. It then copies the page directories and table of the parent in the new child PCB. Is this correct? Or do the parent and child process share them?

2)如果内核将所有页面更改为仅在复制后才能读取,则内核如何知道页面的实际许可?

2) How does the kernel know the actual permission of the pages if it changes all them to be read only after copying them?

推荐答案

我认为1)中的观点是正确的,在fork()期间,内核将为子进程创建一个新的task_struct,并复制mm_struct和page表,它与依赖于clone_flags的父进程共享资源.它将使is_cow_mapping()的MMU条目无效,并将相关的页表条目标记为只读.

I think the opinions in the 1) is correct, during fork(), kernel will create a new task_struct for the child process, and copy the mm_struct and page table, it shares resources with parent process which depends on the clone_flags. And it will invalidate the MMU entries which is_cow_mapping() and marks the related page table entries to read only.

对2)的答案:内核仅将is_cow_mapping()的页表条目更改为只读,当子级或父级要访问这些地址范围时,内核将复制并标记为读/写.

The answer to 2): The kernel only change page table entries which is_cow_mapping() to read only, when child or parent want to access these address ranges, kernel will duplicate and marked to read/write.

如果在fork()之后调用exec(),则会设置一个全新的虚拟空间,而从父级复制的较旧的虚拟空间将被丢弃.

If exec() is called after fork(), a totally new virtual space will be setup, the older one which is copied from parent is discarded.

这篇关于在Linux中使用Write-On-Write在fork()之后如何父级和子级共享页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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