I wrote the following program and executed in terminal. But I am confused that the program isn't displaying the results as wait behavior is described on internet. I copied this code from a book.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
pid_t childid;
if(argc!=2)
{
printf("Please an argument at terminal\n");
return 0;
}
int i,n = atoi(argv[1]);
for(i=0;i<n;i++)
{
childid=fork();
if(childid==0)
{
//sleep(20);
break;
}
}
while(wait(NULL)>0);
printf("i= %d\n Process ID %d\n Parent ID %d\n Child ID %d\n",i,getpid(),getppid(),childid);
return 0;
}
Should't the parent process execute the last "printf" statment n time, where n is the number of children created.. What happens when the parent calls while(wait(NULL)>0); and the child is still in excution?
Thanks
