Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get address information from library to be shared among all processes?

In Understanding the Linux Kernel, 3rd edition, it says:

Shared libraries are especially convenient on systems that provide file memory mapping, because they reduce the amount of main memory requested for executing a program. When the dynamic linker must link a shared library to a process, it does not copy the object code, but performs only a memory mapping of the relevant portion of the library file into the process’s address space. This allows the page frames containing the machine code of the library to be shared among all processes that are using the same code. Clearly, sharing is not possible if the program has been linked statically. (page 817)

I am interested in this, want to write a small program in C to verify, given two pids as input such as two gedit processes, and then get the address information from page frames to be shared. Does anyone know how to do it? From that book, I think the bss segment and text segment address from two or more gedit processes are same, is that correct?

like image 397
Francis Avatar asked Dec 30 '15 07:12

Francis


People also ask

Are shared libraries shared between processes?

Shared code is loaded into memory once in the shared library segment and shared by all processes that reference it. The advantages of shared libraries are: Less disk space is used because the shared library code is not included in the executable programs.

What does shared library contain?

A shared library or shared object is a file that is intended to be shared by multiple programs. Symbols used by a program are loaded from shared libraries into memory at load time or runtime.

What is the purpose of using shared libraries?

Shared Libraries are the libraries that can be linked to any program at run-time. They provide a means to use code that can be loaded anywhere in the memory. Once loaded, the shared library code can be used by any number of programs.

How are shared libraries created?

To create a shared library in C++ using G++, compile the C++ library code using GCC/ G++ to object file and convert the object file to shared (. SO) file using gcc/ g++. The code can be used during the compilation of code and running executable by linking the . SO file using G++.


1 Answers

It is not the text and bss sections of your gedit (or whatever) that have the same address, but the content of the libc.so shared library - and all other shared libraries used by the two gedit processes.

This, as the quoted text says, allows the shared library to be ONE copy, and this is the main benefit of the shared library in general.

bss is generally not shared - since that is per process data. text sections of two processes running the same executable, in Linux, will share the same code.

Unfortunately, the proof of this would be to look at the physical mapping of pages (page at address X in process A is at physical address Y, and page for address X in process B is also at physical address Y) within the processes, and that's, as far as I know, not easily available without groking about inside the OS kernel.

like image 92
Mats Petersson Avatar answered Oct 13 '22 00:10

Mats Petersson