Cambridge A-Level Computer Science 9618 – Operating Systems: Program Libraries
5.1 Operating Systems – Program Libraries
What is a Program Library?
A program library is a collection of pre‑compiled routines (functions, procedures, classes) that can be used by multiple programs. Libraries provide reusable code, hide implementation details and allow developers to build applications more quickly.
Types of Libraries
Static libraries – linked into the executable at compile time.
Dynamic (shared) libraries – linked at run‑time, allowing a single copy to be shared among many processes.
Import libraries – small stub files that describe the symbols in a dynamic library for the linker.
Static vs Dynamic Linking
Aspect
Static Linking
Dynamic Linking
When linking occurs
During compilation (build time)
At program start‑up or on‑demand (run‑time)
Executable size
Usually larger – library code is copied into each executable
Usually smaller – only references to the shared object are stored
Memory usage
Each process has its own copy of the library code
All processes can share a single copy in RAM
Update/patching
Requires recompilation of each program that uses the library
Updating the shared library automatically benefits all programs
Portability
Executable is self‑contained – easier to move between systems
Depends on the presence of the correct version of the shared library on the target system
Typical file extensions
.a (Unix), .lib (Windows)
.so (Unix/Linux), .dll (Windows)
How Dynamic Linking Works
When a program that uses a dynamic library starts, the operating system’s loader performs the following steps:
Read the executable’s import table to determine which shared objects are required.
Locate the required .so or .dll files using the system’s library search path.
Map the shared object into the process’s address space (usually using mmap on Unix‑like systems).
Resolve symbol addresses – the loader patches the program’s code so that calls to library functions jump to the correct locations.
If the library has dependencies of its own, the loader repeats the process recursively.
Versioning and Compatibility
Dynamic libraries often use semantic versioning to indicate compatibility:
Major version – incompatible API changes.
Minor version – backward‑compatible additions.
Patch version – bug fixes only.
Operating systems may keep multiple versions of a library simultaneously, allowing older programs to continue using the version they were built against.
Common Examples of Program Libraries
C Standard Library (libc) – provides functions such as printf, malloc, and string handling.
Java Standard API – a large set of packages (e.g., java.util, java.io) packaged in rt.jar.
.NET Framework Class Library (FCL) – collections of classes in assemblies like System.dll.
POSIX Threads Library (pthread) – functions for creating and managing threads.
Advantages of Using Libraries
Code reuse reduces development time and errors.
Encapsulation of complex functionality behind simple interfaces.
Centralised maintenance – fixing a bug in the library benefits all dependent programs.
Memory efficiency when using shared libraries.
Disadvantages and Risks
Dependency on external files can cause “DLL hell” or “dependency hell” if versions conflict.
Security risk: a compromised shared library can affect many applications.
Performance overhead at start‑up due to dynamic linking.
Static linking increases executable size, which may be undesirable for embedded systems.
Sample Calculation – Memory Savings with Shared Libraries
Assume each process needs 2 MB of library code. If 10 processes run simultaneously: