c - fork() usage not getting the correct output -


i using following code fork execution

#include <stdio.h> #include <sys/types.h>  int main() {     int pid;     pid=fork();     if(pid==0)     {         printf("\n child 1");         pid=fork();         if (pid==0)            printf("\n child 2");     }     return 0; } 

the output assume should child1 child2

instead getting

child1 child2 child1

cannot understand fork behaviour

you need flush stdout:

printf( "\n child 1" ) fflush( stdout ); 

Comments

Popular posts from this blog

delphi - ESC/P programming! -

c++ - error: use of deleted function -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -