vfork:理解问题:

发布于 2024-11-17 23:46:08 字数 393 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-11-24 23:46:09

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 of fork() which creates a new address space (page tables and allocations) for the child, and instead sets a flag which execve() 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).

触ぅ动初心 2024-11-24 23:46:09

execve() 释放当前进程的内存映射并分配新的内存映射。退出进程最终也会释放该进程的内存映射。

传统上,vfork() 会挂起父进程,直到子进程停止使用父进程的内存映射。安全地执行此操作的唯一方法是通过 execve()_exit()

假设子进程执行 exec 系统调用
LS。现在,根据 exec 调用,ls 程序将被加载到
到子进程的地址空间。

实际上,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 via execve() or _exit().

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.

Actually, ls will be loaded in a new address space, the parent's address space is released on the child.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文