vfork:理解问题:
我对 vfork() 的功能感到困惑。我读到,在 vfork() 的情况下,父进程和子进程用于在它们之间共享页面。它不支持任何写入时复制功能。这意味着,如果子进程在其时间片期间进行了一些更改,则当父进程返回时,所有这些更改都将对父进程可见。还提到,vfork() 系统调用仅在子进程创建后仅执行 exec 系统调用时才有用。
假设子进程使用 ls
执行 exec
系统调用。现在,根据exec
调用,ls
程序将被加载到子进程的地址空间。现在,当父进程的时间片启动时,它可能会在其 PC 上执行不同的指令,这可能会导致该进程的行为不同。
有人可以向我解释一下这种情况,vfork()
调用在这种情况下有什么帮助吗?
I have a confusion around the functionality of vfork()
. I read that in case of vfork()
, parent and the child process used to share pages between them. It doesn't support any copy on write functionality. This means, if during its timeslice child process makes some changes, all these changes would be visible to the parent process when it will return. It was also mentioned, that the vfork()
syscall is only been useful when the child process just executes the exec
system call after its creation.
Let us say, that the child process executes the exec
system call with ls
. Now, according to the exec
calls, the ls
program will be loaded on to the address space of the child process. Now, when the parent process' timeslice will start, it might have a different intruction to execute on its PC, which might cause this process to behave differently.
Can somebody please make this scenario clear to me, how the vfork()
call is helpful in such situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vfork()
的要点不是为子进程分配新的地址空间,而子进程会立即再次丢弃它。因此,vfork()
省略了fork()
为子进程创建新地址空间(页表和分配)的部分,而是设置了一个标志,该标志< code>execve() 解释为它应该在用新的可执行文件及其请求的初始堆(BSS)填充进程之前为进程分配一个新的页表和堆栈。The point of
vfork()
is not to allocate a new address space for a child which is going to immediately throw it away again. As such,vfork()
omits the part offork()
which creates a new address space (page tables and allocations) for the child, and instead sets a flag whichexecve()
interprets as meaning that it should allocate a new page table and stack for the process before populating it with the new executable and its requested initial heap (BSS).execve()
释放当前进程的内存映射并分配新的内存映射。退出进程最终也会释放该进程的内存映射。传统上,vfork() 会挂起父进程,直到子进程停止使用父进程的内存映射。安全地执行此操作的唯一方法是通过
execve()
或_exit()
。实际上,
ls
将被加载到一个新的地址空间中,父级的地址空间被释放到子级上。execve()
releases the current process' memory mappings and allocates new ones. Exiting a process also ends up releasing that process' memory mappings.Traditionally,
vfork()
suspends the parent process until the child stops using the parent's memory mappings. The only way to do that safely is viaexecve()
or_exit()
.Actually,
ls
will be loaded in a new address space, the parent's address space is released on the child.