开发者

is it possible to load a shared library on a shared memory?

I have a server and a client written in C. I try to load a shared library in the server and then pass library function pointers to the client. This way I can change the library without have to compile the client.

Because of every process has its own separate memory space, I wonder if开发者_开发知识库 it is possible to load a shared library on a shared memory, pass the function pointers and map the shared memory on the client and then make the client execute the code of the library loaded by the server.


By definition shared library is shared, so two processes will use same physical memory for library's code segment. So instead of making up some dodgy schemes you can just pass the names of the library and function from server to client and client will get function address using dlopen() + dlsym().

Please note that in this case there will be two copies of data segments (if library has some global or static variables), e.g. if server sets some static variable inside the library function, it's value won't change for the client.


You could, perhaps, if the OS doesn't call making shared memory also executable a security problem (which it probably should).

But you don't need to. And, you should not be passing pointers to other processes. Instead, give the functions names or code points in your protocol. The shared library mechanism will mean that there is only one copy of the library in memory, unless the OS decides not to share for some reason. If the OS decides not to share, the possible reasons for that will usually cause your pointer passing trick to fail also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜