关于共享库和线程特定数据的问题 [英] A question on shared library and thread specific data

查看:53
本文介绍了关于共享库和线程特定数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题参考 makefile 中的 gdb 和 valgrind.我找到了上一个问题中指出的分段错误的原因,现在我不知道如何解决这个问题.很抱歉,我将无法发布代码,因为它分布在多个文件中,因此往往非常庞大.

This question is in reference to gdb and valgrind within a makefile. I found the reason of segmentation fault as pointed out in the previous quetion and I now don't know how to solve the issue. I am sorry i shall not be able to post the code as it spreads across multiple files and hence tends to be quite huge.

查看 Makefile

looking at the Makefile

all: clients.so simulator backup     
    LD_PRELOAD=/home/Juggler/client/clients.so ./simulator 
backup: backup.c libclient.a  
    gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl 
simulator: simulator.c libclient.a
    gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread 

libclient.a: libclient.o client.o  
    ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
   gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
   ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
   gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
   gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread

模拟器启动多个线程,每个线程首先使用 libclient.a 中的函数到达 client.c,在那里通过套接字连接到服务器.每个线程都通过不同的套接字连接到服务器,并使用 pthread_keycreate 及其函数系列来保持特定于线程的套接字值.来自每个线程的其他调用(打开、读取等)通过共享库 client.so 进行预加载,然后到 client.c 以通过线程特定的套接字向服务器发送消息.

Simulator starts a number of threads and each thread first uses functions in libclient.a to reach client.c where connection is made to a server thru sockets. Each thread is connected to the server via a different socket and have used the pthread_keycreate and its family of functions to keep the socket value thread specific. Other calls from each thread(open,read etc) goes through the shared library client.so that was preloaded and then to client.c to send message to server through the thread specific socket.

现在,问题是从通过 libclient.a 路由的线程到服务器的任何消息都使用线程特定的套接字,但任何通过 client.so 路由的消息都会在 send() 上遇到分段错误.我尝试打印出套接字描述符,它在第一个路由中打印,但在第二个路由中没有打印......在打印语句中出现段错误.

Now, the problem is any message to server from a thread that takes the route through libclient.a uses the thread-specific socket but any message that takes the route through clients.so shared library encounters a segmentation fault on send(). I tried printing out the socket descriptor and it prints in the first route but not the second...segfault occurs in the print statement.

线程特定的数据和共享库是否有效?

Does thread-specific data and shared library work?

谢谢

段错误中和周围的代码

int call_execute(char *argcalls[])
{
  int *saved_socket;
  saved_socket = (int*)pthread_getspecific(key_to_sockfd);
   .....
  n = send(*saved_sockfd,argcalls[i],strlen(argcalls[i]),0);//segfault here.argcalls[i] not giving segfault

  ...

}

很乐意澄清.call_execute 在发生段错误时从 client_invoke 调用.但是当它从 libclient.c 调用时它工作正常.

Would be happy to clarify. call_execute is called from client_invoke when the segfault occurs. But when it is called from libclient.c it works fine.

推荐答案

是的,TSS 和共享库在原则上是有效的,但有一个警告:根据我在 mingw 交叉编译中遇到类似问题的经验,我假设您的 pthread 库还必须动态链接以为线程特定数据提供公共存储位置.我认为 gcc 通常不是这种情况,但我不确定.两个建议:

Yes, TSS and shared libraries work in principle, with one caveat: From my experience with similar issues in mingw-cross-compiling, I assume that your pthread library must also be linked dynamically to provide a common storage location for the thread-specific data. I don't think this is the case normally for gcc, but am not sure about that. Two suggestions:

  1. 尝试使用 -shared-libgcc 进行链接.
  2. 将分配套接字的代码重新定位到第三个库中和/或将其完全保留在 client.so 中,仅传播指向结构的指针.

这篇关于关于共享库和线程特定数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆