fork speed vs /bin/sh
tlambert2 at mindspring.com
Fri Nov 28 04:29:38 PST 2003
Peter Wemm wrote:
> What this shows is that vfork() is 3 times faster than fork() on static
> binaries, and 9 times faster on dynamic binaries. If people are
> worried about a 40% slowdown, then perhaps they'd like to investigate
> a speedup that works no matter whether its static or dynamic? There is
> a reason that popen(3) uses vfork(). /bin/sh should too, regardless of
> whether its dynamic or static. csh/tcsh already uses vfork() for the
> same reason.
I'm a big fan of vfork(); the on problem I have with the use of
it is that people tend to treat it as "a faster fork()", when it
definitely is not. The utility of vfork() is limited to the list
of allowed system calls, which are _exit() and execve(); all other
usage is undefined -- specifically, you cannot control things like
whether it's the parent or the child that gets effected by calls
like setsid(), setpgrp(), etc..
The other place that vfork() really sucks is in applications like
"screen" or other applications that have multiple children and act
as mux'es for them: during the vfork() to spawn off a new child
from the parent, the parent is stalled, and this in turn stalls
all the children, as well.
The vfork() system call is a good thing, particularly compared to
the fork() system call, IFF it's used appropriately.
For the most part, FreeBSD should consider creating a posix_spawn()
system call, instead, for most uses to which people put either the
fork() or vfork() system calls today.
More information about the freebsd-current