Open In App

Static and Dynamic Linking in Operating Systems

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Static Linking: When we click the .exe (executable) file of the program and it starts running, all the necessary contents of the binary file have been loaded into the process’s virtual address space. However, most programs also need to run functions from the system libraries, and these library functions also need to be loaded. In the simplest case, the necessary library functions are embedded directly in the program’s executable binary file. Such a program is statically linked to its libraries, and statically linked executable codes can commence running as soon as they are loaded.

Static linking is performed during the compilation of source program.

Linking is performed before execution in static linking . It takes collection of Realocatable object file and command line arguments and generates a fully linked. 

object file that can be loaded and run. 

 Disadvantage: Every program generated must contain copies of exactly the same common system library functions. In terms of both physical memory and disk-space usage, it is much more efficient to load the system libraries into memory only once. Dynamic linking allows this single loading to happen. 

Dynamic Linking: Every dynamically linked program contains a small, statically linked function that is called when the program starts. This static function only maps the link library into memory and runs the code that the function contains. The link library determines what are all the dynamic libraries which the program requires along with the names of the variables and functions needed from those libraries by reading the information contained in sections of the library. After which it maps the libraries into the middle of virtual memory and resolves the references to the symbols contained in those libraries. We don’t know where in the memory these shared libraries are actually mapped: 

 

They are compiled into position-independent code (PIC), that can run at any address in memory. 

Advantage: Memory requirements of the program are reduced. A DLL is loaded into memory only once, whereas more than one application may use a single DLL at the moment, thus saving memory space. Application support and maintenance costs are also lowered.

Differences between static and dynamic linking in operating systems are:

  Static Linking  Dynamic Linking
 
Definition  The process of combining all necessary library routines and external references into a single executable file at compile-time.  The process of linking external libraries and references at runtime, when the program is loaded or executed.
 
Linking Time  Occurs at compile-time.  Occurs at runtime.
 
File Size  Generally larger file size, as all required libraries are included in the executable.  Smaller file size, as libraries are linked dynamically at runtime.
 
Flexibility  Less flexible, as any updates or changes to the libraries require recompilation and relinking of the entire program.  More flexible, as libraries can be updated or replaced without recompiling the program.
 
Performance  Faster program startup and direct execution, as all libraries are already linked.  Slightly slower program startup due to the additional linking process, but overall performance impact is minimal.
 
Examples  Executables with file extensions like .exe, .elf, .a, .lib, etc.  Executables with file extensions like .dll, .so, .dylib, etc.
 

Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads