Does a separate process have a separate copy of the Shared Static variable or shared copy?

I am trying to understand the basic concept of shared memory. I am trying to create a shared library that has one function and one STATIC array variable. I want to access a variable of a static array through a function of this 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 created a shared library object (libfoo.so) following the instructions from the shared library

Now my questions

1> If I access foo () from two different programs (program1 and program2), will there be a separate copy of the foo () function for program1 and program2?

2> will a separate copy of the DATA static array appear for program1 and program2?

3> Will they be loaded into the same physical memory cell? If a static DATA array is loaded separately, is there a way to force it to load into the same memory location?

4> Is there a way to find where the DATA static array is stored for program1 and program2?

I am using gcc under linux. Any help would be greatly appreciated. Thanks in advance.

+7
c linux shared-memory shared-libraries
source share
2 answers

There is only one well-defined way of exchanging memory between processes: Shared memory . Access to this memory from different processes requires proper synchronization of access to this region (by the processes themselves). Without access synchronization, this would lead to desasters in a multi-tasking OS such as UNIX / Linux / Windows.

Answers in detail:

If I access foo () from two different programs (program1 and program2), will there be a separate copy of the foo () function for program1 and program2?

Not. When you link your program to a shared library, the function code will be shared between several programs associated with this library.

will there be a separate copy of the static DATA array for program1 and program2?

Yes. No data will be transmitted.

Will they be loaded into the same physical memory location? If a static DATA array is loaded separately, is there a way to force it to load into the same memory location?

No (but read about shared memory above)

Is there any way to find where the DATA static array is stored for program1 and program2?

Yes, you can find it, for example, using GDB. But programs cannot learn about each other.

+4
source share

Read Advanced Linux and wikipages programming in virtual memory, address space , processes , paging , thrashing , ELF , ASLR

See also Drepper Document: How to Write Shared Libraries

Please note that application code running in a specific process does not care about physical memory, it uses only virtual memory. The Linux kernel freely moves data in physical memory (for example, swap or swap on some pages). You do not have to worry about where (in physical RAM) your data sitting is located (and this can change at any time, so from the point of view of the application code this question makes no sense).

The shared object must be compiled as position-independent code using the -fPIC GCC flag (otherwise it will contain too much relocation ). Then, the machine function code (for example, your foo ) will usually be located in a read-only shared text segment, and the RAM containing it, if any, will be shared between several processes.

Try the following command

 cat /proc/self/maps 

which shows the virtual address space of the process executing the cat . Some segments of the memory are shared, others a private copy ...

Also read execve (2) - to run the 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) (for dynamically loading the plugin) and proc (5) - for querying the kernel about various things. Note that you can use mlock (2) to lock memory in RAM (avoiding replacing 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, for example, in shared objects or in executable software). Therefore, of course, if a process modifies some static data, this change is local to the process and is not displayed to other processes running the same executable file or the same shared library. To exchange data with some other process, use mmap (2) with MAP_SHARED (and probably PROT_WRITE ) or Posix shared memory shm_overview (7) ...

By the way, this is a very common question. I am sure I repeated the same things several times in StackOverflow.

+4
source share

All Articles