in reply to Re: Re: Processes clobbering each other
in thread Processes clobbering each other

You are using open and thus the child's output is not being thrown into STDOUT but to a pipe. Closing all pipes without reading the output will not pipe the sub-process output to the parent's parent since they are on different pipes. You should probably use fork to fork(tm) without redirecting output to a different pipe. I managed to confuse myself with that last phrase so here's an idea:

$| = 1; $pid = open(first_child, "-|"); if ($pid) { while(<first_child>) { if ($_ =~ m/^Error/) { } #do nothin elsif ($_ =~ m/^Hits/) { push (@hits, $_) ; } else { push (@line, $_); } } } else { my @cpic; foreach $i(0..@gets) { $e = $gets[$i]; $c = $searches{$e}{handler}; $p = $FORM_DATA{p}; $q = $FORM_DATA{q}; $u = $searches{$e}{url}; $caller = "perl $c $p $q $u"; $cpid[$i] = fork; unless ($cpid[$i]) { system ($caller); exit; } } exit; }

Big warning! Untested code. I don't understand the task that you are trying to accomplish but I (hope) can now see what's wrong. This might not run but should give you an idea.

BTW: Why don't you use ForkManager to accomplish this task ? It simplifies debugging as it makes your code clearer and gives you more control on your child processes.

Replies are listed 'Best First'.
Re: Re: Re: Re: Processes clobbering each other
by mcogan1966 (Monk) on Nov 25, 2003 at 13:50 UTC
    The code works just fine.

    But I still have the same output problem.

    Lemme give you a little more info here.

    The main program in generating a XML style output that is being "printed" back to a PHP script. The first section of the XML prints just fine(that's just form data given back, captured by the Perl CGI script). The third and final section is just fine as well(that part uses the @line data). It's the middle section, the part that uses @hits, that seems to be the problem. @hits is generated from a line in the called programs that looks something like:

    print "Hits;;SomeName;;$_;;$url\n";
    For some reason, this line is not being printed in some cases when I run multiple forked processes. My concern is that in the main program, I am not setting up to make the 'forked / opened / piped' processes not trip over each other.