关于share library共享数据的问题!

RomanticProgrammer 2007-03-18 09:47:40
我发现在share library中的全局变量,静态全局变量都是私有的,也就是说无法在多个加载这个库的进程中共享.我用google搜了一下,发现也有人遇到我这个问题,请问有人知道如何解决吗?
也就是说,如何实现进程A修改了一个共享库中定义的变量x,使另外一个也加载了这个共享库的进程中的x也发生变化.问题的英文描述如下:
Hi,

I wrote a shared library and I know the code of the
shared library will be shared between different
applications. However, the global data of the library
cannot be shared.

For example, the code of the shared library looks
like

// myshare.c
#include <stdio.h>

int global = 5;

void printGlobal()
{
printf("Global=%d\n",global);
}

void setGlobal(int g)
{
global = g;
}

I used the following command to create the library
$gcc -fPIC -shared -o libmyshare.so myshare.c

The user application 1 links with it
//u1.c
#include <stdio.h>

extern void setGlobal(int);

int main()
{
setGlobal(50);
return 0;
}

I used the following command to create u1
$gcc -o u1 u1.c -L. -lmyshare

The user application 2 links with it too
//u2.c
#include <stdio.h>

extern void printGlobal(int);

int main()
{
printGlobal();
return 0;
}

I used the following command to create u2
$gcc -o u2 u2.c -L. -lmyshare

Then, I run u2 and u1
$u2
Global=5
$u1
$u2
Global=5

It seems that from u1, setGlobal(50) does not affect
the
print out of u2, which means, when u1 and u2 link with
the shared library libmyshare.so, there are two
copies
of the global data in libmyshare.so. So the code is
shared,
but not the global data.

My question is: is there any way to share the global
data
within a shared library, i.e, both code and data can
be shared?

(I know I can use shared memory, but I wonder if we
can
do it using special compiler settings.)
...全文
569 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangqm0629 2007-04-08
  • 打赏
  • 举报
回复
朽木厉害
ToIP 2007-04-06
  • 打赏
  • 举报
回复
按照我的理解两个进程使用一个共享库的时候只有.text段是共享的,也就是说只共享只读部分,比如 可执行 代码和符号表,这样同时使用同一个共享库的进程才可以互不干扰,保证正确性。而全局或静态变量应该是在.bss或者.data段的,所以连接到不同的程序中时会有不同的copy。

这是只有一个全局变量的共享库:
/*
* file: a.c
*
* target: liba.so
* command: cc -fPIC -shared -o liba.so a.c
*/
int extern_var_in_so = 0;

这是调用上面共享库的可执行文件:
/*
* file: main.c
*
* target: main.o
* command: cc -c main.c -o main.o
*
* target: main
* command: cc main.o -L. -la -o main
*/
extern int extern_var_in_so;
int local_var_in_main = 0;

int main()
{
return extern_var_in_so + local_var_in_main;
}

在使用cc -c main.c -o main.o编译完main.c时,用nm main.o查看main.o:
U extern_var_in_so
00000000 B local_var_in_main
00000000 T main
可以看到,本地变量local_var_in_main的位置是在bss段;
而外部变量extern_var_in_so还是没有定位(undefined),需要在链接之后定位;

使用cc main.o -L. -la -o main命令把main.o, liba.so链接为可执行文件main,用nm main查看:
...
08049618 B extern_var_in_so
...
08049620 B local_var_in_main
...
注意到这里extern_var_in_so和local_var_in_main都在bss段里,没有什么区别。

以上是我个人理解,不一定准确,也许还是错误的,不过还是希望对你能有帮助。
wangqm0629 2007-03-26
  • 打赏
  • 举报
回复
在两个不同的程序里相同的链接共享库,必然会出现楼主试验的情形!为什么呢
因为两个程序有自己独立的地址空间,当然在数据区也会有各自的Global存储,很明显,修改某一个Global变量,是不会引起其他程序内Global变量的值。
wangqm0629 2007-03-25
  • 打赏
  • 举报
回复
有不放入共享内存的方法吗?
为什么会出现这种情形?
dabaocsdn 2007-03-20
  • 打赏
  • 举报
回复
看<<Unix环境高级编程>>吧,里面有详细的关于共享内存使用的描述
ToIP 2007-03-19
  • 打赏
  • 举报
回复
用共享内存或者文件应该实现这个目的。关于共享内存可以参考一下相关POSIX文档,我不熟。
playmud 2007-03-19
  • 打赏
  • 举报
回复
把全局变量放入共享内存。
yeah920 2007-03-19
  • 打赏
  • 举报
回复
学习,帮顶
RomanticProgrammer 2007-03-19
  • 打赏
  • 举报
回复
没有人知道吗?

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧