Show understanding of program libraries

Published by Patrick Mutisya · 14 days ago

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

AspectStatic LinkingDynamic Linking
When linking occursDuring compilation (build time)At program start‑up or on‑demand (run‑time)
Executable sizeUsually larger – library code is copied into each executableUsually smaller – only references to the shared object are stored
Memory usageEach process has its own copy of the library codeAll processes can share a single copy in RAM
Update/patchingRequires recompilation of each program that uses the libraryUpdating the shared library automatically benefits all programs
PortabilityExecutable is self‑contained – easier to move between systemsDepends 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:

  1. Read the executable’s import table to determine which shared objects are required.
  2. Locate the required .so or .dll files using the system’s library search path.
  3. Map the shared object into the process’s address space (usually using mmap on Unix‑like systems).
  4. Resolve symbol addresses – the loader patches the program’s code so that calls to library functions jump to the correct locations.
  5. 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:

\$\text{Static total memory} = 10 \times 2\text{ MB} = 20\text{ MB}\$

With a shared library, the code is loaded once:

\$\text{Dynamic total memory} = 2\text{ MB (shared)} + 10 \times \text{(process‑specific data)}\$

Thus, the shared approach saves up to 18 MB of RAM in this scenario.

Suggested diagram: Flowchart of the dynamic linking process from program start‑up to symbol resolution.

Key Points to Remember

  • Libraries are collections of reusable compiled code.
  • Static linking incorporates library code into the executable; dynamic linking loads it at run‑time.
  • Dynamic libraries reduce memory usage and simplify updates, but introduce version‑dependency issues.
  • Understanding the loader’s role is essential for debugging linking problems.