Im new at process creation etc..so probably a basic question.
Im creating a fixed number of child processes, each doing nothing but printing their pid. The problem is in the output that i get. Have a look:
int main(){
pid_t pid=0;
int i=0,status=0;
for(i=0;i<3;i++){
pid=fork();
switch(pid){
case 0:{ //Child
printf("\nChild pid: %d",getpid());
exit(0);
break;
}
case -1: {//Error
printf("Error occured in fork");
exit(1);
break;
}
default:{
printf("\nParent id: %d",getpid());
printf("\nIts child id: %d",pid);
wait(NULL);
}
}
Output:
Child pid: 1450
Parent id: 1445
Its child id: 1450
Child pid: 1455Its child id: 1450
Parent id: 1445
Its child id: 1455
Child pid: 1460Its child id: 1455
Parent id: 1445
Its child id: 1460
The problem is I dont know why only the second print statement of the parent process is appearing and not the first, if any at all. I know im not waiting for my child processes to end( frankly i dont know how i would do that), but if the parent does execute before ending its child processes, why arent both its print statements appearing, and why is the \n being ignored in that line too.
Any help would be greatly appreciated.
Thx.
Update:If i replace wait(NULL) with printf("\n%d\n",wait(NULL)) It gives me a perfect output, without stray prints. Any idea what couldve fixed it? After all they both do the same thing.
The problem is that there are several processes writing to the same file (your console) at the same time without any concurrency control or any lock. That, and that the ttys are strange creatures, at best, make weird things happens. Over that, remember that
printfis buffered.Your output should be read this way:
You could try redirecting the output to a file, and see if not being a tty makes any difference.
Anyway, to do it the right wat, you should use any mechanism that guarantees multiprocess correctness.
UPDATE
Yes, now I see it. You have the ‘\n’ at the beginning of your printing lines, not at the end, as is usual. Stdout is normally line-buffered, that means that the buffer is flushed to the device when it sees a ‘\n’. And since you have them at the beginning there is always one line in the buffer waiting for output.
Now, when you
forkyour process the output buffer gets duplicated and the last line from the parent is printed by the child (why it is printed after, and not before, is still a mystery to me).Anyway, the new
printfyou added has a ‘\n’ at the end, so it flushes the buffer,forkfinds it empty and all goes fine. You could as well callfflush(stdout)but it is cumbersome.The morale of the story is: “When you
printffor debugging purposes always put a\nat the end of every line or you can get partial, mixed contents.