Try to change
printf("hello\n");
to
printf("hello\n"); fflush(stdout);
Here's the problem of buffering. For performance reasons, FILE descriptors do not always output immediately after they are written. Instead, they accumulate text in the internal buffer.
There are three buffering modes, unbuffered, row buffered, and buffered block. Unbuffered pens are always written immediately (stderr does not load). Linear buffered descriptors wait until the buffer is full or a newline ( '\n' ) is printed (stdout is string buffering if it refers to a terminal). Blocked buffered descriptors wait until the buffer is full (stdout is a block buffer if it is not a terminal).
When your helloworld program starts, stdout goes to the channel, not to the terminal, so it is configured as a buffer. Therefore, printf calls simply store the text in memory. Since the buffer does not fill up, it only turns red when stdout closes, which in this case occurs when the program exits.
But by the time the program exited, file descriptor 1 (stdout) had been restored to refer to the parent source stdout, and not to the channel. Thus, buffered output ends up being written to the original output.
fflush forcibly writes buffer text.
source share