Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #262633

Re: 12.2: fork() causing getline() to repeat stdin endlessly

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 23:00 +0200
Message-ID <HsabL-mxq-15@gated-at.bofh.it> (permalink)
References <Hs9IJ-mml-13@gated-at.bofh.it> <Hsa26-mtN-13@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Mon, Oct 23, 2023 at 10:45:44PM +0200, Thomas Schmitt wrote:
> Greg Wooledge wrote:
> > This is what's causing the loop to iterate more times than it should,
> > and to re-process input.
> 
> That's not what i see in my experiments.
> I see stuttering output which first repeats the lines put out so far
> before it adds a new line.
> The getline() loop iterates as often as there are input lines.

Here's what I'm seeing.  First, with a tiny two-line input file, I
see three loop iterations:

unicorn:~$ cat 12
1
2
unicorn:~$ ./foo < 12
1
num_read: 2 at line 1
2
num_read: 2 at line 2
2
num_read: 2 at line 3
unicorn:~$ 

That's the case where I used strace and saw the lseek() in the first
child process (but not in any later child processes, which was even
*more* confusing).

Next, with a larger three-line input file:

unicorn:~$ cat bar
This is one line of a text file containing many lines of text.
This is one line of a text file containing many lines of text.
This is one line of a text file containing many lines of text.
unicorn:~$ ./foo < bar
This is one line of a text file containing many lines of text.
num_read: 63 at line 1
This is one line of a text file containing many lines of text.
num_read: 63 at line 2
This is one line of a text file containing many lines of text.
num_read: 63 at line 3
This is one line of a text file containing many lines of text.
num_read: 63 at line 4
This is one line of a text file containing many lines of text.
num_read: 63 at line 5
This is one line of a text file containing many lines of text.
num_read: 63 at line 6
This is one line of a text file containing many lines of text.
num_read: 63 at line 7
This is one line of a text file containing many lines of text.
num_read: 63 at line 8
[...]
This is one line of a text file containing many lines of text.
num_read: 63 at line 413
This is one line of a text file containing many lines of text.
num_read: 63 at line 414
^C
unicorn:~$ 

I am fairly sure this would repeat indefinitely.  I didn't let it go
more than a few seconds, though.

My system: Debian 12, Linux 6.1.0-13-amd64, gcc (Debian 12.2.0-14) 12.2.0,
libc6:amd64 2.36-9+deb12u3.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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