tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ 2 simple questions about fork

A little split problem ▻

Need help on Perl and fork()

In article <5bh6kn$1vf@moonpie.cs.umd.edu>,
Steve Xue Han <xhan@cs.umd.edu> wrote:
>The Perl script reads a text file, and for each line of the file, forks
>another process to do something with that line.

Eek!  Are you sure this is really the best way to design the program?
For one thing, it's not going to scale very well.  Anyway...

>                                                 To keep track of what is
>going on, the script writes to a log file.  It looks very straightforard
>to me, but it generates the weirdest behavior.

Probably because a child inherits its parent's stdio buffers.  If the
buffers are not empty, both processes will eventually output them,
resulting in duplicated output.

Try setting $| = 1 on your OUTFILE (see select in the perlfunc man
page); this will tell Perl to flush the stdio buffers after every write
to the file handle.

Tim.
--
Tim Goodwin   | "A language needs both intestines and guts." -- Larry Wall

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: Need help on Perl and fork()
Date: 15 Jan 1997 15:02:53 GMT
Organization: UUNET PIPEX
Message-ID: <5birit$anp@join.news.pipex.net>
References: <5bh6kn$1vf@moonpie.cs.umd.edu>

△ comp.lang.perl △

◅ 2 simple questions about fork

A little split problem ▻