memory - 当一个程序具有相同的虚拟地址空间时,什么情况

标签 memory mapping virtual-address-space

如果您在两个不同的终端中运行程序 foo.c,并打印正在执行的局部变量的地址。他们会是一样的。然而,在 fork 和执行的上下文中,例如在 shell 内,我运行一个程序,例如 foo.c 。它将创建一个完全相同的 shell 副本,然后执行 foo.c 。他们会有相同的虚拟地址空间吗? 如果一个程序递归调用自身,递归调用的同一个变量是否仍然具有相同的地址空间,这个程序如何在自己的地址空间内增长?

最佳答案

If you ran a program foo.c in two different terminals, and printed the address of the local variable being executed. They would be same.

不一定,现代操作系统使用 address space layout randomization ,这意味着内存地址可以(并且确实)从一次执行到下一次执行发生变化。

in context of forking and executing say for example inside shell, I run a program such foo.c . It will create an exact same copy of the shell and then execute foo.c . Will they have the same virtual address space.

不,每个进程都有自己的虚拟地址空间。变量的地址可能看起来相同,但在一个进程中写入局部变量不会对另一进程产生影响(除非您显式共享内存)

And what if a program recursive calls itself, will the same variable recursively called still have the same address space and how does this program grow inside its own address space?

研究进程和线程之间的区别,以更好地了解这里发生的情况。如果程序 fork ,则子进程有一个单独的地址空间。如果函数在程序中调用自身,它将在相同的地址空间中执行,但局部变量将在每个堆栈帧中分开。全局(或静态)变量在函数调用中将位于相同的内存地址。

关于memory - 当一个程序具有相同的虚拟地址空间时,什么情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054946/

相关文章:

memory - Mathematica NMinimize 遇到内存问题

Python Pandas 合并导致内存溢出

c - 将数组映射到结构时的顺序相反

memory-management - 分配给用户进程的Page在内核虚拟地址空间中的映射

operating-system - 多级页表——分层分页

python - 关于内存地址的变量赋值和修改

templates - 如何在 ElasticSearch 中正确定义 HashMap

mysql - 将数据库的表名称与另一个表中的值映射

c - 0xfff 处的用户空间地址 <something>

c++ - 如何处理在 C++ 中的类之间传递运行时大小的数组