What is the difference between fork()
and vfork()
? Does vfork()
return like fork()
.
In fork() system call, child and parent process have separate memory space. While in vfork() system call, child and parent process share same address space. 2. The child process and parent process gets executed simultaneously.
System calls provide an interface to the services made available by an operating system. The system calls fork(), vfork(), exec(), and clone() are all used to create and manipulate processes.
LINUX DESCRIPTION vfork() is a special case of clone(2). It is used to create new processes without copying the page tables of the parent process. It may be useful in performance sensitive applications where a child will be created which then immediately issues an execve().
When a vfork system call is issued, the parent process will be suspended until the child process has either completed execution or been replaced with a new executable image via one of the "exec" family of system calls.
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.
As already stated, the vfork
man page is clear about the differences. This topic gives a good description of fork
, vfork
, clone
and exec
.
Below are some often overlooked differences between fork
and vfork
I experienced on some Linux 2.6.3x embedded systems I worked with.
Even with copy-on-write techniques, fork
fails if you don't have enough memory to duplicate the memory used by the parent process. For example, if the parent process uses 2 GB of resident memory (ie, memory that is used and not just allocated), fork
fails if you have less than 2 GB of free memory left. That's frustrating when you just want to exec
a simple program and therefore will never need that huge parent address space!
vfork
doesn't have this memory issue, as it doesn't duplicate the parent address space. The child process acts more like a thread in which you are able to call exec*
or _exit
without hurting your parent process.
Because memory page tables are not duplicated, vfork
is much faster than fork
and vfork
's execution time is not affected by the amount of memory the parent process uses, as pointed out here: http://blog.famzah.net/2009/11/20/fork-gets-slower-as-parent-process-use-more-memory/
In situations where performance is critical and/or memory limited, vfork
+ exec*
can therefore be a good alternative to fork
+ exec*
. The problem is that it is less safe and the man page says vfork
is likely to become deprecated in the future.
A safer and more portable solution may be to look at the posix_spawn
function, which is higher level and offers more options. It safely uses vfork
when possible, depending on the options you pass it. I have been able to use posix_spawn
successfully and overcome that annoying "double memory checking issue" that fork
+ exec
was giving me.
A really good page on this topic, with links to some posix_spawn
examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With