Static Versus Dynamic Application

The smallest possible embedded Linux application that uses all three software layers (kernel, C library, and application) consists of a kernel loader, the kernel, and a very few statically linked application programs.We’ll call this a static application because to build it you must link your executables statically against the C library. The great thing about this kind of embedded application is that it’s extremely small and very easy to put together if you have only a few executables. In fact, Chapter 8,“Static Application Example: Minicom,” shows you how to build a single floppy-disk terminal emulator out of Minicom in about a half hour, by downloading the pieces from the Internet and assembling them. The resulting application is less than 500KB in size and will work on any PC with a serial port.

When the kernel starts up, it runs a program called /linuxrc or /bin/init, depending on whether you use the initrd feature of Linux. If you name your starting executable properly on the root filesystem, you can force the kernel to run it when the kernel’s done booting. If you link the executable statically, you don’t need to have the shared C libraries in your root filesystem.

Usually, however, you need many executables for your application. Perhaps you need to start the network with ifconfig and route. Perhaps you need certain daemons to run to respond to events. Perhaps you need cron running to provide timing. Each of these programs takes much more space when statically linked than when dynamically linked. So it’s best to link executables dynamically if your application has more than a few.

Dynamically linked executables share a single copy of functions that are commonly used in all executables. Examples of functions in the standard C library include printf(), open(), read(), write(), and close(). Sharing the code that performs these common functions helps the embedded programmer in two ways:

  • With numerous executables, the total amount of storage used by all the executables plus the shared libraries will be less than the total amount of storage used by the executables if they were statically linked.

  • In general, dynamically linked executables take up less real memory for the same reason. There will be only one copy of shared functions, such as printf(), for the whole machine. If the executables are statically linked, each will have its own copy of printf(), and they may both be in memory at the same time even though they’re identical.

Dynamically linked executables are marginally slower, however. Because each shared function is not part of the executable code, whenever it’s called some fix-up code must discover what actual code needs to be called. This may be only a few dozen instructions, but in a tight loop these instructions can add overhead. Normally, however, the difference is negligible and the storage size win is tremendous.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.94