在同一个进程中多次dlopen同一个so库,返回的句柄值是一样的
共享库例子代码(hell.c):
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int g_count = 0;
int add_count()
{
g_count +=1;
char buff[64] = {0};
int fd;
fd = open("so.txt", O_RDWR | O_APPEND);
// printf("g_count= %d\r\n", g_count);
sprintf(buff, "g_count= %d\r\n", g_count);
write(fd, buff, 64);
sync();
sleep(1);
return g_count;
}
APP1例子代码(app1.c)
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
void *handle;
int (*fun_han)();
int cnt = 0;
int fd;
char buf[64] = {0};
fd = open("test.txt", O_RDWR | O_APPEND);
// printf("[%d]\r\n", __LINE__);
// printf("hello world\n");
#if 1
handle = dlopen("/home/yang/exam/hell.so", RTLD_LAZY | RTLD_GLOBAL);
if(NULL == handle){
printf("[%d]%s\n",__LINE__, dlerror());
}
// printf("[%d]\r\n", __LINE__);
fun_han = (int (*)())dlsym(handle,"add_count");
if(NULL == handle){
printf("[%d]%s\n",__LINE__, dlerror());
}
for(;;){
cnt = fun_han();
// printf("[%d]app1 cnt = %d\r\n",__LINE__, cnt);
sprintf(buf, "app1 cnt = %d\r\n", cnt);
write(fd, buf, 64);
sync();
}
dlclose(handle);
#endif
return 0;
}
APP2例子代码
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
void *handle;
int (*fun_han)();
int cnt = 0;
int fd;
char buf[64] = {0};
fd = open("test.txt", O_RDWR | O_APPEND);
// printf("[%d]\r\n", __LINE__);
// printf("hello world\n");
#if 1
handle = dlopen("/home/yang/exam/hell.so", RTLD_LAZY | RTLD_GLOBAL);
if(NULL == handle){
printf("[%d]%s\n",__LINE__, dlerror());
}
// printf("[%d]\r\n", __LINE__);
fun_han = (int (*)())dlsym(handle,"add_count");
if(NULL == handle){
printf("[%d]%s\n",__LINE__, dlerror());
}
for(;;){
cnt = fun_han();
// printf("[%d]app1 cnt = %d\r\n",__LINE__, cnt);
sprintf(buf, "app2 cnt = %d\r\n", cnt);
write(fd, buf, 64);
sync();
}
dlclose(handle);
#endif
return 0;
}
编译:编译共享库:
gcc -o libhell.so -fPIC -shared -g hell.c
编译应用程序
gcc -o app1 -ldl libhell.so app1.cgcc -o app2 -ldl libhell.so app2.c
添加动态库路径
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
或在/etc/ld.so.conf文件中添加动态库所在的目录
执行程序:
./app1 &.
/app2 &
查看结果:
so.txt内容:
g_count= 1
g_count= 2
g_count= 3
g_count= 4
g_count= 5
g_count= 6
g_count= 1
g_count= 7
g_count= 2
g_count= 8
g_count= 3
g_count= 9
g_count= 4
g_count= 10
g_count= 11
g_count= 12
g_count= 13
g_count= 14
test.txt内容:
app1 cnt = 1
app1 cnt = 2
app1 cnt = 3
app1 cnt = 4
app1 cnt = 5
app1 cnt = 6
app2 cnt = 1
app1 cnt = 7
app2 cnt = 2
app1 cnt = 8
app2 cnt = 3
app1 cnt = 9
app1 cnt = 10
app1 cnt = 11
app1 cnt = 12
app1 cnt = 13