Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Libc shared library loaded in memory and shared amongst processes?

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

like image 379
Aditya Mertia Avatar asked Apr 21 '11 07:04

Aditya Mertia


People also ask

How do libraries share memory between processes?

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 and how do they work?

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.

What is the difference between static libraries and shared libraries?

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.

What happens if a shared library fails to load?

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.


1 Answers

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.

like image 82
linuxbuild Avatar answered Oct 05 '22 23:10

linuxbuild