please look at following code.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
fork();
printf(".");
fflush(stdout);
fork();
printf(",");
return 0;
}
the output is :
..,,,,
this is ok for me,
but if I remove fflush(stdout) from the above program then the output should be(as per my understanding).
...,.,.,.,
the statement of fork() is : the statements immediately after fork() are copied into both parent and child.
what I understand is :
after the first fork() there are two process (assume it as process p and process c) we have
the code in process p and c is :
printf(".");
fork();
printf(",");
now , suppose the first statement of process p and c is executed , so the output will be .
..
now the fork() statement comes to execute .
so , after executing fork() , our processes are like p , pc ,c ,cc.
the code in each of p , pc , c and cc is
printf(",");
we do not flush the stdout so printf(".") is still there in each of the buffer .
so each process will print .,
so the output is
.,.,.,.,
my question is :
1) where is previous .. ?
i.e. as per my explanation the output should be
...,.,.,.,