This is how to control the number of children. I'll let you work out calling execl()
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int foo(const char *whoami)
{
printf("I am a %s. My pid is:%d my ppid is %d\n",
whoami, getpid(), getppid() );
return 1;
}
int main(int argc, char **argv)
{
int n=atoi(argv[1]);
int i=0;
int status=0;
if (n==0) n=2;
printf("Creating %d children\n", n);
foo("parent");
for(i=0;i<n;i++)
{
pid_t pid=fork();
if (pid==0) /* only execute this if child */
{
foo("child");
exit(0);
}
wait(&status); /* only the parent waits */
}
return 0;
}
to prevent the child from spawning another child, kill that child before it loops!
in other words,don't ever let it loop the for( ; ; ), or add if (pid !=0 ) before the fork () call