From owner-freebsd-current@FreeBSD.ORG Thu Nov 27 11:04:14 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2E61516A4CE for ; Thu, 27 Nov 2003 11:04:14 -0800 (PST) Received: from canning.wemm.org (canning.wemm.org [192.203.228.65]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8327643FAF for ; Thu, 27 Nov 2003 11:04:13 -0800 (PST) (envelope-from peter@wemm.org) Received: from wemm.org (localhost [127.0.0.1]) by canning.wemm.org (Postfix) with ESMTP id 6E8152A8FC for ; Thu, 27 Nov 2003 11:04:13 -0800 (PST) (envelope-from peter@wemm.org) X-Mailer: exmh version 2.6.3 04/04/2003 with nmh-1.0.4 To: current@freebsd.org Date: Thu, 27 Nov 2003 11:04:13 -0800 From: Peter Wemm Message-Id: <20031127190413.6E8152A8FC@canning.wemm.org> Subject: fork speed vs /bin/sh X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Nov 2003 19:04:14 -0000 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@overcee[10:46am]/tmp-149> cc -O -o vforkathon.dynamic vforkathon.c peter@overcee[10:46am]/tmp-150> cc -O -static -o vforkathon.static vforkathon.c peter@overcee[10:47am]/tmp-151> cc -O -static -o forkathon.static forkathon.c peter@overcee[10:47am]/tmp-152> cc -O -o forkathon.dynamic forkathon.c peter@overcee[10:47am]/tmp-153> time ./forkathon.dynamic 0.120u 17.192s 0:17.81 97.1% 5+169k 0+0io 0pf+0w peter@overcee[10:47am]/tmp-154> time ./forkathon.static 0.051u 5.939s 0:06.38 93.7% 15+177k 0+0io 0pf+0w peter@overcee[10:47am]/tmp-155> time ./vforkathon.dynamic 0.015u 2.006s 0:02.30 87.3% 5+176k 0+0io 0pf+0w peter@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@overcee[10:48am]/tmp-157> cat forkathon.c #include #include #include 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@overcee[10:53am]/tmp-158> diff forkathon.c vforkathon.c 12c12 < pid = fork(); --- > pid = vfork(); Cheers, -Peter -- Peter Wemm - peter@wemm.org; peter@FreeBSD.org; peter@yahoo-inc.com "All of this is for nothing if we don't go to the stars" - JMS/B5