【发布时间】:2015-05-12 11:53:16
【问题描述】:
我是一名 CS 本科生,刚刚了解了虚拟内存。我用下面的程序做了一个实验。
#include<stdio.h>
int ready0;
int main(void) {
int ready;
printf("ready0 at %p, ready at %p. \n", (void*)&ready0, (void*)&ready);
}
我认为由于程序只处理虚拟内存,因此程序应该认为它是机器上运行的唯一进程。我还查看了反汇编代码,看起来非常确定。因此,如果我多次运行该程序,结果应该是相同的。然而,实验表明情况并非如此。为什么实验与我的预期不同?是什么导致我每次运行程序时结果都不同?
如果您有兴趣,这里有几个在 Mac OS X Yosemite 上的实验结果。
$ ./sp
ready0 at 0x102b18018, ready at 0x7fff5d0e876c.
$ ./sp
ready0 at 0x107c09018, ready at 0x7fff57ff776c.
$ ./sp
ready0 at 0x10aa9c018, ready at 0x7fff5516476c.
$ ./sp
ready0 at 0x10d56d018, ready at 0x7fff5269376c.
$ ./sp
ready0 at 0x10da1c018, ready at 0x7fff521e476c.
$ ./sp
ready0 at 0x109aff018, ready at 0x7fff5610176c.
$ ./sp
ready0 at 0x107c31018, ready at 0x7fff57fcf76c.
$ ./sp
ready0 at 0x10fab1018, ready at 0x7fff5014f76c.
【问题讨论】:
-
你能发布结果吗?对我来说,它每次都显示相同的地址。
ready0、ready或两者的地址是否不同?它们不在同一个存储区域。 -
没有法律规定地址必须相同。使用什么地址将受到过程中之前发生的事情的强烈影响。此外,分配给流程的段可能因流程的一个实例而异。
-
我更新了,是的,它们都不同。
标签: c linux unix operating-system