fork speed vs /bin/sh

Peter Wemm peter at
Thu Nov 27 11:04:14 PST 2003

I *know* I'm going to regret posting this, but if people care about
the speed of their shell, then perhaps you want to look at this:

peter at overcee[10:46am]/tmp-149> cc -O -o vforkathon.dynamic vforkathon.c
peter at overcee[10:46am]/tmp-150> cc -O -static -o vforkathon.static vforkathon.c
peter at overcee[10:47am]/tmp-151> cc -O -static -o forkathon.static forkathon.c
peter at overcee[10:47am]/tmp-152> cc -O -o forkathon.dynamic forkathon.c
peter at overcee[10:47am]/tmp-153> time ./forkathon.dynamic
0.120u 17.192s 0:17.81 97.1%    5+169k 0+0io 0pf+0w
peter at overcee[10:47am]/tmp-154> time ./forkathon.static
0.051u 5.939s 0:06.38 93.7%     15+177k 0+0io 0pf+0w
peter at overcee[10:47am]/tmp-155> time ./vforkathon.dynamic
0.015u 2.006s 0:02.30 87.3%     5+176k 0+0io 0pf+0w
peter at overcee[10:48am]/tmp-156> time ./vforkathon.static
0.022u 2.020s 0:02.34 87.1%     16+182k 0+0io 0pf+0w

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.

NetBSD have already taken advantage of this speedup and their /bin/sh uses
vfork().  Some enterprising individual who cares about /bin/sh speed should
check out that.  Start looking near #ifdef DO_SHAREDVFORK.

In case anybody was wondering:

peter at overcee[10:48am]/tmp-157> cat forkathon.c
#include <sys/types.h>
#include <sys/signal.h>
#include <stdio.h>

main(int ac, char *av[])
        int i;
        pid_t pid;

        for (i = 0; i < 100000; i++) {
                pid = fork();
                switch (pid) {
                case 0:
                        waitpid(pid, NULL, 0);
peter at overcee[10:53am]/tmp-158> diff forkathon.c vforkathon.c
<               pid = fork();
>               pid = vfork();

Peter Wemm - peter at; peter at; peter at
"All of this is for nothing if we don't go to the stars" - JMS/B5

More information about the freebsd-current mailing list