What is the difference between fork() and vfork()?

user507401 picture user507401 · Nov 23, 2010 · Viewed 35.3k times · Source

What is the difference between fork() and vfork()? Does vfork() return like fork().

Answer

Blagovest Buyukliev picture Blagovest Buyukliev · Nov 23, 2010

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 */
}

For other kinds of uses, vfork is dangerous and unpredictable.

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.