我应该在dlopen之前锁定吗?

时间:2017-03-01 07:39:33

标签: c++ c linux

我有一个* .so库,它使用dlopen从系统库中获取一些信息。库可以同时由多个应用程序使用。 也许这是一个愚蠢的问题,但是在我做dlopen之前,我应该聚集库吗?我没有在任何地方找到直接答案。

1 个答案:

答案 0 :(得分:1)

与评论中所说的类似,除非您正在访问可能会改变您的共享资源,否则您不需要信号量(flock)。 (IE。访问共享内存并需要确保该数据的并发性)。动态加载的方式... dlopen()...工作

  

Those two routines are actually simple wrappers that call back into the dynamic linker. When the dynamic linker loads a library via dlopen(), it does the same relocation and symbol resolution it does on any other library, so the dynamically loaded program can without any special arrangements call back to routines already loaded

由于链接的工作方式,GOT / PLT的重定位和修改是在(进程调用dlopen)的内存空间中完成的,而不是映射共享对象的位置。

  

If a hundred processes use a shared library, it makes no sense to have 100 copies of the code in memory taking up space. If the code is completely read-only, and hence never, ever, modified

让共享对象在只读内存中你永远不必担心它们会突然改变你,所以不需要一群人:)!

注意:因为你有一个共享对象链接到其他共享对象...初始共享对象的GOT需要更新/ mod与使用dlopen()加载的库的重定位...但是存储在进程唯一内存空间的ar / w段中,而不是存储在共享对象的内存空间中。

  

the shared library must still have a unqiue data instance in each process...the read-write data section is always put at a known offset from the code section of the library. This way, via the magic of virtual-memory, every process sees its own data section but can share the unmodified code

相关问题