我怎样才能在C两个程序之间共享库 [英] How can I share library between two program in c

查看:108
本文介绍了我怎样才能在C两个程序之间共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在两个不同的程序用C使用相同的库函数(即OpenSSL库)进行计算。我怎样才能确保这两个程序中使用一个公共库,用于库的一个副本被装入的共享主内存并都计划访问从该内存位置的库计算?

I want to use same library functions (i.e. OpenSSL library ) in two different programs in C for computation. How can I make sure that both program use a common library , means only one copy of library is loaded into shared main memory and both program access the library from that memory location for computation?

例如,当第一个程序访问的计算库被加载到主存储器和当第二个程序要在稍后访问它,它就会从缓存中(已经被第一个程序加载)访问数据,<强缓存>不可以从主内存中试。

For example, when 1st program access the library for computation it is loaded into cache from main memory and when the 2nd program wants to access it later , it will access the data from cache ( already loaded by 1st program), not from main memory again.

我使用Linux下GCC。任何解释或指针将是非常美联社preciated。

I am using GCC under Linux. Any explanation or pointer will be highly appreciated .

推荐答案

阅读Drepper的文章如何写共享库程序库方法文档

Read Drepper's paper How to Write Shared Libraries and Program Library HowTo

要进行一次,编译code作为位置无关$ C $ ç,如:

To make one, compile your code as position independent code, e.g.

 gcc -c -fPIC -O -Wall src1.c -o src1.pic.o
 gcc -c -fPIC -O -Wall src2.c -o src2.pic.o

然后将其链接到一个共享对象

then link it into a shared object

 gcc -shared src1.pic.o src2.pic.o -lsome -o libfoo.so

您可以链接共享库 -lsome 进入另一个 libfoo.so

you may link a shared library -lsome into another one libfoo.so

在内部,动态链接 ld-linux.so(8 )使用的mmap(2)(和会做在动态链接时一定搬迁),哪些事项的的inode 的。内核将使用文件系统缓存来避免读取两次不同进程使用的共享库。另请参见 linuxatemyram.com

Internally, the dynamic linker ld-linux.so(8) is using mmap(2) (and will do some relocation at dynamic link time) and what matters are inodes. The kernel will use the file system cache to avoid reading twice a shared library used by different processes. See also linuxatemyram.com

使用例如 LDD(1),的 PMAP(1)和的 PROC(5)。另请参见的dlopen(3)。尝试

Use e.g. ldd(1), pmap(1) and proc(5). See also dlopen(3). Try

cat /proc/self/maps

要了解的地址空间:// EN。 wikipedia.org/wiki/Virtual_memory相对=nofollow>虚拟内存的所用的过程运行命令;不是 ELF 的共享库进程间共享,只有一些片段,其中的文本段 ...

to understand the address space of the virtual memory used by the process running that cat command; not everything of an ELF shared library is shared between processes, only some segments, including the text segment...

这篇关于我怎样才能在C两个程序之间共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆