Hi

I have a perl script that:
   - reads STDIN line by line and for each line:
      - parses the line ( in order to build a list of system commands)
      - writes the line to STDOUT
      - writes the line out to an output file
   - forks a child to run each system command in the list
( the relevant parts of the code are below ) at the completion of this script, I would expect that the input file would have been written as is to STDOUT and the output file... howver I find that the output file contains the input file repeated many times - although it is only written once to STDOUT.
If I take the forking code out ( no pun intended) then I only get once copy of the input file in the output file.
Whats going on here? and why doesnt it happen with STDOUT?

Thanks Kung

open(MFOUT,">mfout.txt"); while (<STDIN>) { print MFOUT $_; print $_; <SNIP> ... stuff that builds up commands in @commands... ... eg 'perl anotherscript.pl < input' </SNIP> } my %running = (); my $max_jobs = 200; my $job_count = 0; while ( @commands || %running ) { if ( @commands && ($job_count < $max_jobs)) { my $command = shift(@commands); my $pid; if ($pid = fork) { print "Parent Pid $$\n"; $running{$pid} = $command; ++$job_count; } else { die "cannot fork: $!" unless defined $pid; print "Child Pid $$ $command\n"; my $rc = system $command; exit($rc>>8); } } else { my $child_pid = wait(); if (! exists $running{$child_pid}) { warn "Reaped unkown process id $child_pid!!"; } elsif ($?) { my $rc = $? >> 8; warn "Process '$child_pid:$running{$child_pid}' errored with retu +rn code '$ rc'"; } print "command '$running{$child_pid}' completed\n"; delete $running{$child_pid}; --$job_count; } }

Edit: chipmunk 2001-05-16


In reply to fork and print.. by Kung

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.