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


Groups > linux.debian.user > #262601

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

From jleonard@slimy.com (Jon Leonard)
Newsgroups linux.debian.user
Subject Re: 12.2: fork() causing getline() to repeat stdin endlessly
Date 2023-10-23 16:20 +0200
Message-ID <Hs3WF-igI-5@gated-at.bofh.it> (permalink)
References <HrZgl-fhL-5@gated-at.bofh.it> <HrVmp-cVb-1@gated-at.bofh.it> <Hs3jX-hNC-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Mon, Oct 23, 2023 at 09:31:11AM -0400, Greg Wooledge wrote:
> 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.

More specifically, fork() does not play nicely with stdio buffering.

For performance reasons, stdio tends to read and write in chunks, reducing
the number of read() and write() calls.  Some data is stored in the stdio
buffers, and that data is getting copied to both processes in the fork()
call.

If you want to mix fork() and stdio, be sure to flush buffers before the
call to fork.  Depending on the task, it may be easier to use the underlying
read() and write() calls.

Jon Leonard 

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