i got basic knowledge about creating a single child from a parent using fork(). But when it comes into creating multiple children, i am simply stuck.
I am trying to create two processes from a parent and it would wait for both two processes to finish.
my attempt is as below
Code:
main()
{
int pid,i;
printf("let's start the process,and i am pid%d\n",getpid());
for (i=0;i<=1;i++)
{
pid=fork();
if ((pid>0))
{
wait(NULL);
printf("i am the parent pid %d\n",getpid());
}
else if (pid==0)
printf("i am the child pid %d\n",getpid());
}
return 0;
}
for some reasons, it came up with four processes.
the sample output:
Quote:
let's start the process,and i am pid2859
i am the child pid 2860
i am the child pid 2861
i am the parent pid 2860
i am the parent pid 2859
i am the child pid 2862
i am the parent pid 2859
|
could anyone point out the logic error?
thanks in advance.