fork()中的写时复制如何处理多个fork? [英] How does copy-on-write in fork() handle multiple fork?

查看:221
本文介绍了fork()中的写时复制如何处理多个fork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据维基百科(可能是错误的)

发出fork()系统调用时,将创建与父进程相对应的所有页面的副本,并由OS加载到子进程的单独内存位置.但这在某些情况下是不需要的.考虑以下情况:子级执行"exec"系统调用(用于从C程序中执行任何可执行文件)或在fork()之后不久退出.当仅需要子进程来执行父进程的命令时,就不需要复制父进程的页面,因为exec用要执行的命令替换了调用该子进程的进程的地址空间.

在这种情况下,使用了一种称为写时复制(COW)的技术.使用此技术时,发生派生时,不会为子进程复制父进程的页面.而是在子进程和父进程之间共享页面.每当进程(父级或子级)修改页面时,都会为执行修改的那个进程(父级或子级)单独创建该页面的单独副本.然后,此过程将使用新复制的页面,而不是将来所有引用中的共享页面.另一个过程(未修改共享页面的过程)继续使用页面的原始副本(现在不再共享).这种技术称为写时复制,因为当某些进程向页面写入内容时会对其进行复制.

似乎任何一个过程尝试写入页面时.页面的新副本已分配并分配给生成页面错误的过程.原始页面之后被标记为可写.

我的问题是:如果在任何进程尝试写入共享页面之前多次调用fork,会发生什么?

解决方案

如果在原始父进程中多次调用fork,则每个子级和父级的页面都将被标记为只读.当子进程尝试写入数据时,会将父进程的页面复制到其地址空间,并且复制的页面被标记为在子进程中可写,但在父进程中不可写.

如果从子进程中调用fork并且孙子试图写入,则将原始父级的页面复制到第一个孩子,然后再复制到孙子,并将所有标记为可写. /p>

According to wikipedia (which could be wrong)

When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. Consider the case when a child executes an "exec" system call (which is used to execute any executable file from within a C program) or exits very soon after the fork(). When the child is needed just to execute a command for the parent process, there is no need for copying the parent process' pages, since exec replaces the address space of the process which invoked it with the command to be executed.

In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork occurs, the parent process's pages are not copied for the child process. Instead, the pages are shared between the child and the parent process. Whenever a process (parent or child) modifies a page, a separate copy of that particular page alone is made for that process (parent or child) which performed the modification. This process will then use the newly copied page rather than the shared one in all future references. The other process (the one which did not modify the shared page) continues to use the original copy of the page (which is now no longer shared). This technique is called copy-on-write since the page is copied when some process writes to it.

It seems that when either of the process tries to write to the page. A new copy of the page is allocated and assigned to the process that generated the page fault. The original page is marked writable afterwards.

My question is: what happens if the fork is called multiple times before any of the process made an attempt to write to a shared page?

解决方案

If fork is called multiple times from the original parent process, then each of the children and parent will have their pages marked as read-only. When a child process attempts to write data then the page from the parent process is copied to its address space and the copied page is marked as writeable in the child but not in the parent.

If fork is called from the child process and the grand-child attempts to write, the page from the original parent is copied to the first child, and then to the grand child, and all is marked as writeable.

这篇关于fork()中的写时复制如何处理多个fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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