简体   繁体   English

程序具有相同的虚拟地址空间时会怎样?

[英]What are the cases when a program has the same virtual address space

If you ran a program foo.c in two different terminals, and printed the address of the local variable being executed. 如果您在两个不同的终端上运行了一个程序foo.c,并打印了正在执行的局部变量的地址。 They would be same. 他们会是一样的。 However, in context of forking and executing say for example inside shell, I run a program such foo.c . 但是,例如在shell内执行并执行分支的情况下,我运行了foo.c这样的程序。 It will create an exact same copy of the shell and then execute foo.c . 它将创建完全相同的Shell副本,然后执行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? 而且,如果程序递归调用自己,该递归调用的相同变量是否仍具有相同的地址空间,并且该程序如何在其自身的地址空间内增长?

If you ran a program foo.c in two different terminals, and printed the address of the local variable being executed. 如果您在两个不同的终端上运行了一个程序foo.c,并打印了正在执行的局部变量的地址。 They would be same. 他们会是一样的。

Not necessarily, modern operating systems use address space layout randomization , meaning memory addresses can (and do) change from one execution to the next. 不一定,现代操作系统使用地址空间布局随机化 ,这意味着内存地址可以(并且确实)从一个执行更改为另一个执行。

in context of forking and executing say for example inside shell, I run a program such foo.c . 在例如在shell中进行分叉和执行说的情况下,我运行了foo.c这样的程序。 It will create an exact same copy of the shell and then execute foo.c . 它将创建完全相同的Shell副本,然后执行foo.c。 Will they have the same virtual address space. 他们将拥有相同的虚拟地址空间。

No, each process has its own virtual address space. 不,每个进程都有自己的虚拟地址空间。 The addresses of variables might look the same, but writing to a local variable in one process has no impact on the other process (unless you've explicitly shared memory) 变量的地址可能看起来相同,但是在一个进程中写入局部变量对另一进程没有影响(除非您已显式共享内存)

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? 而且,如果程序递归调用自己,该递归调用的相同变量是否仍具有相同的地址空间,并且该程序如何在其自身的地址空间内增长?

Research the difference between processes and threads to get a better understanding of what is going on here. 研究进程和线程之间的差异,以更好地了解这里发生的情况。 If a program forks , the child process has a separate adress space. 如果程序派生 ,则子进程将具有单独的地址空间。 If a function calls itself within a program, it will execute in the same address space but local variables will be separate in each stack frame. 如果函数在程序中调用自身,它将在相同的地址空间中执行,但是局部变量在每个堆栈帧中都将分开。 Global (or static) variables will be at the same memory address across function calls. 跨函数调用,全局(或静态)变量将位于相同的内存地址。

If you ran foo.c in two different terminals and printed the address of a local variable, they would display the same. 如果在两个不同的终端中运行foo.c并打印了局部变量的地址,则它们将显示相同的内容。 HOWEVER, they are two different variables which happen to have the same value. 但是,它们是两个具有相同值的不同变量。

They do not point to the same area in memory. 它们不指向内存中的同一区域。

Forking from a shell would also get the process to have two separate and distinct variables within two different areas of virtual memory. 从外壳进行分叉还将使该过程在虚拟内存的两个不同区域中具有两个单独且不同的变量。

In fact, this is the case if you spawn a process from foo.c 实际上,如果您从foo.c中产生一个进程,就是这种情况

If you wish to have shared memory between two process, you need to spawn threads or use shared memory . 如果希望在两个进程之间有共享内存,则需要生成线程或使用共享内存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM