I ended up using setvbuf(stdin, NULL, _IONBF, 0) in the parent process and that seems to have fixed the actual program I was having trouble with.
On Mon, Oct 23, 2023 at 10:19 AM Thomas Schmitt <scdbac...@gmx.net> wrote: > Hi, > > it helps to do > fflush((stdout); > after each printf(), or to run before the loop: > setvbuf(stdout, NULL, _IONBF, 0); > > So it is obvious that the usual output buffering of printf() causes the > repetitions of text. > > The loop does not do any extra cycles, as i could confirm by inserting > a stderr message after ++line_num: > > fprintf(stderr, "line_num= %d\n", line_num); > > While the buffered printf() repeats its lines, the stderr message shows > no repetitions but counts nicely up to the end. > > (I assume gdb disables buffering and thus suppresses the stdout > repetitions.) > > > Have a nice day :) > > Thomas > >