【发布时间】:2013-06-11 07:46:26
【问题描述】:
如果一个进程和它的fork有不同的数据副本,那为什么它们的指针是一样的呢?
在下面的示例中,如果 count 在父进程和子进程之间共享,我们将看到 count: 2。但是,计数不共享。但是,为什么&count 在父进程和子进程中返回相同的值呢?
输出:
count: 1 0x7fff5a617510
count: 1 0x7fff5a617510
程序:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid;
int count = 0;
pid = fork();
count++;
printf("count: %d %p \n", count, &count);
return 0;
}
【问题讨论】:
-
这是虚拟寻址。您的进程看到的地址与物理地址不直接对应。
-
有没有办法查看物理地址?
-
嗯。我的输出是:
error: 'new_count' undeclared (first use in this function)。 :) -
这里已经回答了这个问题! stackoverflow.com/questions/7253659/…
-
@luqmaan 没必要,重复的问题可能会让下一个人更容易找到答案。
标签: c pointers process operating-system fork