fork speed vs /bin/sh

From: Peter Wemm <peter_at_wemm.org>
Date: Thu, 27 Nov 2003 11:04:13 -0800
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>

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

        for (i = 0; i < 100000; i++) {
                pid = fork();
                switch (pid) {
                case 0:
                        _exit(0);
                default:
                        waitpid(pid, NULL, 0);
                }
        }
}
peter_at_overcee[10:53am]/tmp-158> diff forkathon.c vforkathon.c
12c12
<               pid = fork();
---
>               pid = vfork();


Cheers,
-Peter
--
Peter Wemm - peter_at_wemm.org; peter_at_FreeBSD.org; peter_at_yahoo-inc.com
"All of this is for nothing if we don't go to the stars" - JMS/B5
Received on Thu Nov 27 2003 - 10:04:14 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:31 UTC