开发者

vfork:Understanding Issue:

开发者 https://www.devze.com 2023-03-16 14:43 出处:网络
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 wr

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?


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号