3

プロセスとそのフォークに異なるデータのコピーがある場合、それらのポインターが同じなのはなぜですか?

以下の例では、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;
}
4

1 に答える 1