Creating multiple processes in UNIX

Hello,
I'd like to write a program that creates multiple child processes in C. For instance, I begin with one process, this process creates only one child. Then the child process creates its own child etc.
I try to do it in a way so that the inital process awaits the ending only of its son but not of the other process. Can you help me to do that?

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>

int main()
{
int N=3;
int i=0;

printf("Creating %d children\n", N);
printf("PARENT PROCESS\nMy pid is:%d \n",getpid() );
for(i=0;i<N;i++)
{
pid_t pid=fork();
if(pid < 0)
{
perror("Fork error\n");
return 1;
}
else if (pid==0) /* child */
{
printf("CHILD My pid is:%d my parent pid is %d\n",getpid(), getppid() );
}
else /* parrent */
{
wait(NULL);
//printf("PARENT My pid is:%d my parent pid is %d\n",getpid(), getppid() );
exit(0);
}

}
return 0;
}
wait() blocks.

You'll eventually need to use waitpid().
Last edited on
Topic archived. No new replies allowed.