简体   繁体   English

如何从库中获取地址信息以在所有进程之间共享?

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

In Understanding the Linux Kernel, 3rd edition , it says: 在《 了解Linux内核》第3版中 ,它说:

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) (第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. 我对此很感兴趣,想用C语言编写一个小程序进行验证,给定两个pid作为输入,例如两个gedit进程,然后从要共享的页面框架中获取地址信息。 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? 从那本书中,我认为来自两个或多个gedit过程的bss段和文本段地址是相同的,对吗?

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. 具有相同地址的不是gedit(或其他内容)的textbss部分,而是libc.so共享库的内容以及两个gedit进程使用的所有其他共享库。

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. bss通常不共享-因为这是每个进程的数据。 text sections of two processes running the same executable, in Linux, will share the same code. 在Linux中,运行相同可执行文件的两个进程的text部分将共享相同的代码。

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. 不幸的是,这样做的证明是要查看进程内页面的物理映射(进程A中地址X的页面在物理地址Y,进程B中地址X的页面也在物理地址Y),并且据我所知,如果不深入OS内核,就很难获得。

查看/proc/*/maps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM