I want to understand how Libc shared library loaded in memory and shared amongst processes? Is there one libc instance loaded in memory and shared amongst all processes or is it each libc instance in memory for each process. I am unclear about how is libc shared between processes.
Thanks Aditya
If the library is built with -fPIC (as it almost certainly should be), the vast majority of pages are exactly the same for every process that loads it. Your processes a and b may well load the library at different logical addresses, but those will point to the same physical pages: the memory will be shared.
What Are Shared Libraries? A library is a file that contains compiled code and data. Libraries in general are useful because they allow for fast compilation times (you don’t have to compile all sources of your dependencies when compiling your application) and modular development process.
Static Libraries are linked into a compiled executable (or another library). After the compilation, the new artifact contains the static library’s content. Shared Libraries are loaded by the executable (or other shared library) at runtime.
If loading of any shared library fails, the application won’t run. A dynamic loader examines application’s dependency on shared libraries. If these libraries are already loaded into the memory, the library address space is mapped to application virtual address space (VAS) and the dynamic linker does relocation of unresolved symbols.
The one instance of libc is shared among all the processes. See "The Inside Story On Shared Libraries And Dynamic Loading" article:
Specifically, because libraries mostly consist of executable instructions and this code is normally not self-modifying, the operating system can arrange to place library code in read-only memory regions shared among processes (using page-sharing and other virtual memory techniques). So, if hundreds of programs are running and each program includes the same library, the operating system can load a single shared copy of the library’s instructions into physical memory. This reduces memory use and improves system performance.
See also "Dissecting shared libraries" article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With