是否不同的进程共享的静态变量或普通副本复印件独立? [英] Does different process has seperate copy of Shared Static variable or common copy?

查看:210
本文介绍了是否不同的进程共享的静态变量或普通副本复印件独立?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解基本的共享存储的概念。我试图创建具有一个功能,一个静态数组变量的共享库。我想通过共享库的函数访问静态数组变量。

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.

下面是我的共享库

     //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;    
    }

我已经从下面的共享库

现在我的问题是

1>如果我访问从两个不同的程序(程序1和Program2中)富(),会不会有富()函数的单独副本program1的和Program2中?

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>将在那里program1的和Program2中为静态数据的阵列<独立的复制/ STRONG>

2> will there be separate copy of static DATA array for program1 and program2 ?

3>,他们将加载到同一个物理存储位置?如果静态数据组单独加载有没有办法迫使/使其加载到相同的内存位置?

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>有没有办法找到其中的静态数据的阵列存储的程序1和Program2中?

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

我使用的linux下的gcc。任何帮助将提前高度AP preciated .Thanks。

I am using gcc under linux. Any help will be highly appreciated .Thanks in advance.

推荐答案

阅读<一高级Linux编程并wikipages HREF =htt​​p://en.wikipedia.org/wiki/Virtual_memory相对=nofollow>虚拟内存,的地址空间处理,的分页,的颠簸 ELF ,的 ASLR

Read Advanced Linux Programming and wikipages on virtual memory, address space, processes, paging, thrashing, ELF, ASLR

阅读也 Drepper的文章:如何写共享库

注意在某些过程,应用code运行不关心的物理内存的,它只使用的虚拟内存的。该 Linux内核是自由移动在物理内存中的数据(如换出或交换在某些页) 。你不应该关心在哪里(物理RAM)是你坐的数据(这可以在任何时候从应用code点变化,因此,这个问题没有任何意义)。

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).

一个共享的对象应该被编译为位置无关code 使用 -fPIC GCC标志(否则,它将包含太多的搬迁)。然后,一个功能的机器code(如您的)通常会坐在一个共享的只读文本段 - 以及含有它,如果任何的RAM,在几个进程间共享。

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.

试试下面的命令

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...

另请阅读的execve(2)-to 启动程序并设置其地址节省空间,叉(2) - 创建一个新的porocess-,的mmap(2)-to 更改地址与空间(由动态链接程序 LD-Linux中使用( 8)),的dlopen(3)(动态加载插件)和 PROC(5) - 查询有关各种东西 - 内核。请注意,您可以使用 MLOCK(2)内存锁定RAM(避免交换吧),如果你有root权限。

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.

每个进程都有自己的地址空间,特别是有它自己的数据段(为静态或全局的extern 变量,例如在共享对象或在程序的可执行文件)。所以,当然,如果一个进程改变一些静态数据,变化是本地的过程,而不是其他进程可见运行相同的可执行文件或相同的共享库。与其他进程共享数据,使用的mmap(2) MAP_SHARED (也许 PROT_WRITE )或POSIX共享内存的 shm_overview(7) ...

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)...

顺便说一句,这是一个非常普遍的问题。我敢肯定我已经回答StackOverflow上几次几乎同样的事情。

这篇关于是否不同的进程共享的静态变量或普通副本复印件独立?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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