次のようなCファイルがあります。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
printf ("The PID is %d\n", (int) getpid ());
child_pid = fork ();
if (child_pid != 0)
{
printf ("this is the parent process, with PID %d\n",
(int)getpid());
printf ("the child's PID is %d\n", (int) child_pid);
}
else
printf ("this is the child process, with PID %d\n",
(int)getpid());
return 0;
}
次のような階層を生成するように変更する必要があります
parent (0)
|
+---> child (1)
|
+---> child (2)
|
+----> child (3)
|
+----> child (4)
|
+----> child (5)
|
基本的に、2番目の子がそれぞれ2つの新しい子を作成するツリー構造。私が理解している限り、私がfork()プロセスを実行するとき、各プロセスは同時に実行されます。fork()ステートメントにaを追加するifと、親だけが新しいフォークを作成するため、機能し、プロセス0〜2が正しく作成されるようです。しかし、私はプロセス2を1ではなくフォークにする方法がわかりません。何かアイデアはありますか?