Trying to split a parent process into two children. The first will calculate factorials of a given number. The second will just say I'm child 2!
When that's done, the first child will output the time taken to calculate the factorials. Getting the first child to split and do its job is working just fine. However, I can't see to get the second child to do anything. Any idea what I'm doing wrong?
#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);
}
Here's child2.c:
#include <stdio.h>
void main()
{
printf("I'm child 2!");
}
Alright, so, I was having problems with eclipse. I dropped it and recompiled both .c files. I used execl to point to child2.o, but it's still not doing anything