fork() 和 vfork() 有什么区别? [英] What is the difference between fork() and vfork()?
问题描述
fork()
<有什么区别/a> 和 vfork()
?vfork()
是否像 fork()
一样返回.
What is the difference between fork()
and vfork()
? Does vfork()
return like fork()
.
推荐答案
vfork
的目的是消除复制整个过程映像的开销,如果你只想做一个 exec*
在孩子中.因为exec*
替换了子进程的整个镜像,所以复制父进程的镜像没有意义.
The intent of vfork
was to eliminate the overhead of copying the whole process image if you only want to do an exec*
in the child. Because exec*
replaces the whole image of the child process, there is no point in copying the image of the parent.
if ((pid = vfork()) == 0) {
execl(..., NULL); /* after a successful execl the parent should be resumed */
_exit(127); /* terminate the child in case execl fails */
}
对于其他类型的用途,vfork
是危险且不可预测的.
For other kinds of uses, vfork
is dangerous and unpredictable.
然而,对于包括 Linux 在内的大多数当前内核,vfork
的主要优势已经消失,因为 fork
的实现方式.不是在执行 fork
时复制整个图像,而是使用写时复制技术.
With most current kernels, however, including Linux, the primary benefit of vfork
has disappeared because of the way fork
is implemented. Rather than copying the whole image when fork
is executed, copy-on-write techniques are used.
这篇关于fork() 和 vfork() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!