I am trying to understand the fundamental of shared memory concept. I trying to create a shared library having one function and one STATIC array variable. I want to access static array variable through the function of that shared library.
Here is my shared library
//foo.c
#include <stdio.h>
static int DATA[1024]={1 ,2 ,3 ,...., 1024};
inline void foo(void)
{
int j, k=0;
for(j=0;j<1024;j++)
{
k=DATA[j];
}
k+=0;
}
I have created shared library object (libfoo.so) by following instructions from shared library
Now my questions are
1> If I access foo() from two different program ( program1 and program2), will there be separate copy of foo() function for program1 and program2 ?
2> will there be separate copy of static DATA array for program1 and program2 ?
3> Will they load into same physical memory location ? If the static DATA array is loaded separately, is there any way to force/make it loaded into same memory location ?
4> Is there any way to find where the static DATA array is stored for program1 and program2 ?
I am using gcc under linux. Any help will be highly appreciated .Thanks in advance.
There is just one well defined way to share memory between processes: Shared Memory. Accessing this memory from different processes requires proper synchronizing the access to that region (by the processes itself). Without synchronizing the access it would lead to a desaster on a multitasking OS like UNIX/Linux/Windows.
Answers in detail:
If I access foo() from two different program ( program1 and program2), will there be separate copy of foo() function for program1 and program2 ?
No. When linking your program against a shared library, the code of function will being shared between multiple programs linked against that library.
will there be separate copy of static DATA array for program1 and program2 ?
Yes. No data will being shared.
Will they load into same physical memory location ? If the static DATA array is loaded separately, is there any way to force/make it loaded into same memory location ?
No (But read about Shared Memory above)
Is there any way to find where the static DATA array is stored for program1 and program2 ?
Yes, you can find it out using GDB for example. But the programs can't find out about each other.
Read Advanced Linux Programming and wikipages on virtual memory, address space, processes, paging, thrashing, ELF, ASLR
Read also Drepper's paper: How to write shared libraries
Notice that application code running in some process don't care about physical memory, it only uses virtual memory. The Linux kernel is free to move data in physical memory (e.g. swap out or swap in some pages). You should not care where (in physical RAM) is your data sitting (and that can change at any time, so from the point of view of application code, the question does not make any sense).
A shared object should be compiled as position independent code using the -fPIC
GCC flag (otherwise, it would contain too much relocation). Then, the machine code of a function (like your foo
) will usually sit in a shared read-only text segment - and the RAM containing it, if any, is shared between several processes.
Try the following command
cat /proc/self/maps
which shows you the virtual address space of the process running that cat
command. Some of the memory segments are shared, others are a private copy...
Also read execve(2) -to start a program and set its address space-, fork(2) -to create a new porocess-, mmap(2) -to change the address space- (used by the dynamic linker ld-linux(8)), dlopen(3) (to dynamically load a plugin), and proc(5) -to query the kernel about various things-. Notice that you might use mlock(2) to lock memory into RAM (avoiding swapping it) if you have root privileges.
Each process has its own address space, in particular has its own data segments (for static
or global extern
variables, e.g. in shared objects or in the program executable). So of course if a process change some static
data, the change is local to the process and is not visible to other processes running the same executable or the same shared library. To share data with some other process, use mmap(2) with MAP_SHARED
(and probably PROT_WRITE
) or Posix shared memory shm_overview(7)...
BTW, this is a very common question. I'm quite sure I have answered nearly the same things on StackOverflow several times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With