data visibility in different shared library in linux
Suppose there is a program which will dynamically load two shared library and there is a message allocated in library A. I want to know if it is possible for library B to use that message.
Edit: Since it is just in the design stage, there is no "real code".
in shared li开发者_Python百科brary A:
void process()
{
msg_ptr = new message();
send(msg_ptr); //send the msg address to library B
}
in shared library B:
void process()
{
recv(msg_ptr); // at this point how can library B access the msg address
}
(Edit): Ok, the question is somewhat confusing because the send
and recv
imply inter-process communication and it seems not to be the case, so:
Short answer: Yes, it does not matter which library, static or dynamic, does the allocation. It is always process-wide.
Shared library means the code is stored on disk once, but does not mean anything at all (that would have effect on usage) at runtime.
Allocation does not care whether it's done from library or application code. Each process has just one heap from which it does all allocations. This heap is accessible by all code running in context of that process and never accessible by any other process (even if using the same code). In fact, the operator new
itself lives in another shared library (libstdc++.so
) and it just calls malloc
from yet another shared library (libc.so
). And each shared library appears only once in each process, even when more than one other shared library depends on it (this may in some corner cases be different on Windows).
At runtime, pointer in one address space is never ever valid in address space in another process, so passing pointers in messages between processes makes no sense, ever. It is perfectly OK between different threads of the same process though.
精彩评论