6

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?

Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
Francis
  • 91
  • 3
  • 1
    You can verify this by inspecting the `/proc` filesystem, some info in an Q&A I answered here: http://unix.stackexchange.com/questions/33381/getting-information-about-a-process-memory-usage-from-proc-pid-smaps – Mat Dec 30 '15 at 07:46
  • proc/pid/maps possibly – Pradeep Goswami Dec 30 '15 at 12:55

2 Answers2

2

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

Look at the contents of /proc/*/maps.

Paulo1205
  • 918
  • 4
  • 9