I am trying to understand the following code:
int main(int argc, char **argv)
{
int pid1,pid2;
if((pid1=fork())<0)
{
printf("Error bla bla");exit(1);
}
else printf("A");
if((pid2=fork())<0)
{
printf("Error bla bla");exit(2);
}
if(pid1==0)printf("B\n");
if(pid2==0)printf("C\n");
exit(0);
return 0;
}
the output I get looks like this :
A
AC
AB
AB
C
If I change the first print to printf("A\n");
the output is
A
C
A
B
B
C
How do the processes behave in this situation ? I know that the second fork()
gets executed in both the parent process and the first child process but why does the output look like this ?
Also, why does it print the last 3 letters in that specific order ?