I'm having some difficulty with forking and waiting for my processes to finish. At first I inserted a big 'sleep' after my wait, but after fiddling with the source I was able to duplicate my issue in a much smaller snippet of code. Bear with me:
#! /usr/bin/perl -w -I. use strict; ###################################################################### +########## # DATA ###################################################################### +########## # Standard Modules use Data::Dumper; my $pid = ''; + # For forking. my $templog = ''; my @smokearr = ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); # Run ls over and over. #--------------------------------------------------------------------- +- for ( my $index = 0 ; $smokearr[$index] ; $index++ ) { $templog = "./$smokearr[$index].log"; my $syscmd = "./smoke.sh > $templog 2>&1"; fail("fatal: cannot fork: $!") unless defined( $pid = fork() ) +; # Run sort as a child and exit if ( !$pid ) { print STDERR "Running [$syscmd] ($$)\n"; system($syscmd) == 0 or print STDERR "fatal: system \"$syscmd\" failed: +$? See log $templog\n"; exit 0; } } # Wait for the child processes to finish. #---------------------------------------- wait; # Check each log file for smoke.pl #--------------------------------------------------------------------- +--- for ( my $index = 0 ; $smokearr[$index] ; $index++ ) { $templog = "./$smokearr[$index].log"; my $syscmd = "grep smoke.pl $templog"; print STDERR "Running [$syscmd] ($$)\n"; system($syscmd) == 0 or print STDERR "fatal: system \"$syscmd\" failed: $? See l +og $templog\n"; } exit 0;
The contents of smoke.sh:
#!/bin/sh cat smoke.txt |sort | uniq -c

As you can see, smoke.sh is trivial, but it has pipes.

The contents of smoke.txt is just a long text listing with smoke.pl being a line in there.

When I run my app, smoke.sh runs, wait gets the returned children but I get several failures in the log.

My theory is that smoke.sh returns before the piped grandchildren (I originally just had a cat without the pipes, but the error didn't occur). I also get the error if I put $syscmd = "cat smoke.txt |sort |uniq -c > $templog 2>&1"; directly.

So how can I get around this? I do a fork call 11 times writing to 11 log files and then I'm grepping those log files for a certain result and I get errors and this is presumably because the app I'm calling forks a writing thread.

$ uname -a AIX nevd1 1 5 000DA5AF4C00 unknown unknown AIX $ perl --version This is perl, v5.6.0 built for aix

Any and all help is greatly appreciated.


In reply to Fork Wait and probably grandchildren by Discordanian

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.