親プロセスを 2 つの子プロセスに分割しようとしています。1 つ目は、指定された数値の階乗を計算します。それI'm child 2!
が完了すると、最初の子は階乗の計算にかかった時間を出力します。最初の子を分割してその仕事をさせることは、うまく機能しています。しかし、2番目の子供に何かをさせるには見えません。私が間違っていることは何か分かりますか?
#include <stdio.h>
#include <time.h>
//#include </sts/types.h>
#include <unistd.h>
//prototypes
int rfact(int n);
int temp = 0;
main()
{
int n = 0;
long i = 0;
double result = 0.0;
clock_t t;
printf("Enter a value for n: ");
scanf("%i", &n);
pid_t pID = fork();
if (pID ==0)//child
{
//get current time
t = clock();
//process factorial 2 million times
for(i=0; i<2000000; i++)
{
rfact(n);
}
//get total time spent in the loop
result = ((double)(clock() - t))/CLOCKS_PER_SEC;
//print result
printf("runtime=%.2f seconds\n", result);
}
else if(pID < 0)
{
printf("fork() has failed");
}
else //parent
{
//second fork for child 2
pid_t pID2 = fork();
if (pID2 == 0)
{
execl("child2.o","child2", 20, NULL);
}
else if (pID2 < 0)
{
printf("fork() has failed");
}
else
{
waitpid(0);
}
waitpid(0);
}
}
//factorial calculation
int rfact(int n)
{
if (n<=0)
{
return 1;
}
return n * rfact(n-1);
}
ここに child2.c があります:
#include <stdio.h>
void main()
{
printf("I'm child 2!");
}
さて、私は日食に問題がありました。私はそれを落として、両方の .c ファイルを再コンパイルしました。execl を使用して child2.o を指定しましたが、まだ何もしていません。