Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #262598
| From | Greg Wooledge <greg@wooledge.org> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: 12.2: fork() causing getline() to repeat stdin endlessly |
| Date | 2023-10-23 15:40 +0200 |
| Message-ID | <Hs3jX-hNC-9@gated-at.bofh.it> (permalink) |
| References | <HrZgl-fhL-5@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
On Mon, Oct 23, 2023 at 11:15:22AM +0200, Thomas Schmitt wrote: > 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. Yes, it looks like a buffering issue to me as well. Or, more generally, some kind of weird interaction between buffered stdio FILEs and the underlying Unix file descriptors, when new processes are being fork()ed. > The loop does not do any extra cycles, as i could confirm by inserting Here's another interesting observation: using the original program with only the argc/argv change, there is different behavior depending on whether stdin points to a file, or a pipe. unicorn:~$ seq 1 2 | ./foo 1 num_read: 2 at line 1 2 num_read: 2 at line 2 unicorn:~$ seq 1 2 > bar unicorn:~$ ./foo < bar 1 num_read: 2 at line 1 2 num_read: 2 at line 2 2 num_read: 2 at line 3 unicorn:~$ Clearly there are different mechanisms in play based on whether stdin is a regular file or a pipe. I can't fully explain this result, though. On Mon, Oct 23, 2023 at 12:59:38AM -0400, tom kronmiller wrote: > Without the fork/exit/waitpid block of code, it prints the input > successfully. With that code included, it goes into an infinite loop > printing the first 78 lines of input, Every second pass over the input,the > 78th line is garbled. > 77 77 77 77 77xx > 78 1 1xx The 78th line looks like it's truncated after 16 characters. > num_read: 30 at line 6433 > 2 2 2 2 2xx > > The input file "lines" is (all lines 52 charactures + \n == 53 characters): 53 * 77 = 4081 Add the 16 characters of the truncated line and you're right around the magic number 4096, which is very likely the buffer size. So, even if I can't explain every detail, this is another strong clue that stdio buffering is involved somehow.
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 10:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 11:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 15:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly jleonard@slimy.com (Jon Leonard) - 2023-10-23 16:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-23 16:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 17:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly gene heskett <gheskett@shentel.net> - 2023-10-23 18:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly <tomas@tuxteam.de> - 2023-10-23 20:10 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-23 21:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 22:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 22:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 22:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 23:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-24 07:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-24 18:10 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-24 21:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly conover@panix.com (John Conover) - 2023-10-25 01:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly jleonard@slimy.com (Jon Leonard) - 2023-10-25 04:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-25 04:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly <tomas@tuxteam.de> - 2023-10-25 06:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-25 07:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-25 08:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-23 18:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Stefan Monnier <monnier@iro.umontreal.ca> - 2023-10-24 05:00 +0200
csiph-web