私は1つの親プロセスと3つの子プロセスを持つプログラムを持っています。そして、私はすべての子プロセスの実行時間を計算したいと思います。子プロセスfork()のプロセス実行時間を計算する
int run_time[3]; // Variable to save the running time of the children process
time_t start[3]; // Variable to help measure the actual time
for (i = 0; i < 3; i++)
{
if(fork() == 0) // 3 Children process running
{
start[i] = time(NULL); // Start time of child process
usleep(1000000);
run_time[i] = time(NULL) - start[i]; // Calculate run time
printf("Running time: %d from child\n",run_time[i]);
exit(0);
}
}
for (i2 = 0; i2 < 3; i2++) // Waiting all 3 children process finish
waitpid(-1, NULL, 0);
for (i3 = 0; i3 < 3; i3++) // Printing out run time of children from parent process
printf("Running time: %d from parent\n",run_time[i3]);
私は、私もグローバル変数とポインタ(私が試した)で、親プロセスに子プロセス(私のコードでrun_time[]
)から計算データを保存することができないことを知っているように。 pipe()
を使用している方法は1つだけです。このようなもの:int fd[2]
、次にpipe(fd)
しかし、1つ以上の子プロセスにpipe()
を使用することはできません。子プロセスの実行時間を計算する別の方法があたかもpipe()
を使用していないかのように私は望んでいますか?そして、複数の子プロセスにどのようにpipe()
を使用することができますか?
をあなたにたくさんありがとうございました。私のコードが動作します:D。私は依然として質問したいと思います:なぜあなたはそのようなexit()を書いていますか?どのように役立ちますか? WIFEXITED(ステータス)は何を意味しますか? – Someonewhohaveacat