博客
关于我
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/

你可能感兴趣的文章
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>