博客
关于我
C/C++:多进程使用dlopen、dlsym、dlclose装载动态库
阅读量:208 次
发布时间:2019-02-28

本文共 2345 字,大约阅读时间需要 7 分钟。

C/C++:多进程使用dlopen、dlsym、dlclose装载动态库

曾经我天真地以为,动态库是装载到内存(操作系统)中,如果有多个进程同时dlopen同一个动态库,应当是在OS中仅仅有一份动态库实例,当然,动态库中全局变量也是独一份.

实际上动态库是被装载到了不同的进程空间中,不同进程同一时刻打开相同的动态库,使用的是不同的动态库实例.

看下下面的例子就知道啦.

count.h

#ifndef _COUNT_H#define _COUNT_H#include 
int count;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int get();void inc();#endif

count.c

#include "count.h"int get(){    return count;}void inc(){    pthread_mutex_lock(&mutex);    count++;    pthread_mutex_unlock(&mutex);}

main.c

#include 
#include
#include
#include
#include
#define NUM 1000#define LIBPATH "/home/test1280/libcount.so"void *ThreadRun(void *arg){ void *handler = dlopen(LIBPATH, RTLD_LAZY); if (handler == NULL) { printf("ERROR:%s:dlopen\n", dlerror()); return; } void (*inc)() = (void (*)())dlsym(handler, "inc"); if (inc == NULL) { printf("ERROR:%s:dlsym\n", dlerror()); return; } int (*get)() = (int (*)())dlsym(handler, "get"); if (get == NULL) { printf("ERROR:%s:dlsym\n", dlerror()); return; } int i = 0; for (; i < NUM; i++) { inc(); usleep(1000*1000); printf("INFO:PID(%d):%d\n", getpid(), get()); } dlclose(handler);}int main(){ pthread_t tid; pthread_create(&tid, NULL, ThreadRun, NULL); printf("create Thread OK!!!\n"); while (1); return 0;}
[test1280@localhost ~]$ gcc -fPIC -c count.c[test1280@localhost ~]$ gcc -shared count.o -o libcount.so[test1280@localhost ~]$ gcc -o main main.c -ldl -lpthread

如何验证呢?

可以开两个终端,隔一段时间开始执行程序:

在终端A中先执行main程序,输出如下:

[test1280@localhost ~]$ ./maincreate Thread OK!!!INFO:PID(5645):1INFO:PID(5645):2INFO:PID(5645):3INFO:PID(5645):4INFO:PID(5645):5INFO:PID(5645):6INFO:PID(5645):7INFO:PID(5645):8INFO:PID(5645):9INFO:PID(5645):10INFO:PID(5645):11

在终端A中执行main后有输出,代表线程启动啦,然后呢,已经正常打开了动态库以及对动态库中全局变量进行了Update.

这个时候在终端B中再次做个main,构建个新的进程,如果两个进程使用的是同一个动态库实例,则,第二个(终端B)的main进程应当是可以看到终端A中的main进程(或许不应该说终端X中的XX进程,所有进程都是在操作系统中的…这么说其实不合适,这里只是为了表明下不同进程)对动态库实例的修改的…

在终端B中先执行main程序,输出如下:

[test1280@localhost ~]$ ./maincreate Thread OK!!!INFO:PID(5689):1INFO:PID(5689):2INFO:PID(5689):3INFO:PID(5689):4INFO:PID(5689):5INFO:PID(5689):6

你看,不一样吧,又从头开始啦。

以上测试说明:

在操作系统中,同一时刻不同进程装载相同的动态库到各自进程的进程空间中,是创建不同的动态库实例的,各个进程有各自的空间,各自的动态库实例…

Just Do It!

转载地址:http://gtgs.baihongyu.com/

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>