<分区>
如果一个进程和它的 fork 有不同的数据副本,那么为什么它们的指针相同?
在下面的示例中,如果计数在父进程和子进程之间共享,我们将看到 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;
}