How statically linked programs run on Linux
eli.thegreenplace.net
eli.thegreenplace.net
Or to be specific, they start up much faster, fork()/exec() are much slower for dynamically linked programs, while for statically linked programs it is much faster than most people think.
There is a myth that forking is slow, and that caused people to abandon very simple, elegant and unixy solutions like CGI.
I wrote a whole web "framework" in rc shell ( http://werc.cat-v.org) and the reality is that if you statically link your programs you can use shell scripts that do dozens of forks per request and still provide better performance than something like php with fcgi.
(Another great thing is that shell scripts and pipes naturally and automagically take advantage of multi-core systems, Unix once again beautifully shows how simple and beautiful concepts like fork and pipes have unforeseen benefits many decades after they were invented.)
What about the sharing of state?
There isn't any shared state between components of a pipeline.
Multiprocessing and using multiple processes (instead of threads) to take advantage of them predates UNIX, by a lot.
The issue described above can be avoided by using posix_spawn(3), which on linux uses vfork(2).
The cost of fork() is linear with respect to memory size.
Using shared objects has a number of pitfalls: wasteful duplication on your hard disk, wasteful copying of the ELF binary into memory when you could tap the OS library cache for your dependencies (including extra page allocations), and the inability to upgrade a dependency of a binary without recompiling the binary.
For overly large programs, statically linking in e.g. an X11 enviroment, it might matter.
The Inside story on shared libraries and dynamic loading :http://www.dabeaz.com/papers/CiSE/c5090.pdf
The beta site includes the book in draft form, for those with the patience to deal with the occasional typo...
One thing would be worth mentionning: do_execve has it's a most commonly know wrapper for sys_execve.
I'm happy to see this type of article very useful indeed I can't wait for the follow-up
I'd also recommend reading the rest of his blog, at least the programming section. It's very insightful.
Moreover, he's written "gold", which does what "ld" does but has an actually comprehensible source code :), and speed, or so I heard.
But I gave up on static linking in Linux after trying to static link a distributed network client we build in a University project that makes heavy use of C++11 std::thread and Boost. It compiles fine, but segfaults on startup. This is a known issue[1] but I did not investigated further.
It appears for me as an complete outsider to glibc/gcc Linux development that static linking is discouraged in Linux[2].