# Include int main (void) {int P1; putchar ('x'); While (p1 = fork () =-1); If (p1 = 0) printf ("B"); else printf ("A"); printf ("Y ");}
The result is not xxbyay, but xbyxay. Why ..
Because the standard output is line buffered
The real branch of the program starts with the code after fork, and the first X is actually in the buffer (at this time there is no sub-process parent process concept ), when fork is used, the child process also copies a parent
The buffer of the process, so the sub-process will get X. Xby must be connected, because the default output is line.
Buffered, there is no output in case of carriage return. The buffer is cleared only when the process exits. Similarly, xay must be together.
The sequence of xay and xby is uncertain.
If you perform fflush first, there will be less X.
[Code]
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
Int main (void)
{
Int P1;
Putchar ('x ');
Fflush (stdout );
// Setbuf (stdout, null );
While (p1 = fork () =-1 );
If (p1 = 0)
Printf ("B ");
Else
Printf ("");
Printf ("Y ");
Return 0;
}
[/Code]
The result is
Xbyay
In other words, because stdout is line buffered by default, by must be connected together (only flush when the sub-process exits ), the same is true for ay. As for AY, the order of by is not necessarily