私は Linux でのプロセス管理を学んでおり、次の C プログラムを実装してみました。その出力には 15 個の PID (4 個の一意の PID) が出力されました。プロセス ファミリー ツリーを理解しようとしましたが、PID が何度も印刷された理由を理解するのに本当に役立ちませんでした。http://u.cs.biu.ac.il/~linraz/os/OS2.pdf、http://www.ibm.com/developerworks/aix/library/au-unixprocess.htmlを含むいくつかのリンクをたどりました、fork() の後、最初に実行するのは親か子か? . しかし、私は解決策を見つけることができませんでした。誰かがこの問題を理解するのを手伝ってくれたら、とても助かります。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
printf ( "Parent:%d Child: %d\n",getppid(),getpid()); // To print the PIDs of the parent process and the child process
fork(); //System call to spawn a child process
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
fork();
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
fork();
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
return 0;
}