On Thu, Apr 16, 2009 at 08:32, Michael Alipio <daem0n...@yahoo.com> wrote:
snip
>
> die "Could not fork command1!" unless defined (my $command1_pid = fork);

I am going to number the processes here.  1 will be the first child, 1.1
will be the first child's child.

> if ($command1_pid == 0){

Only the child will have this be true, so right now these lines of code
will be executed by 1

>  open EXTERNAL_PROG1, "external_prog1 |" or die "Can't run external_prog1";
>
>  while (<EXTERNAL_PROG1>){
>    if (/pattern/){
>      die "Could not fork command2" unless defined (my $command2_pid = fork);

You fork again, so the next few lines will be executed by 1 and 1.1

>      die "Could not fork command3" unless defined (my $command3_pid = fork);

1 forks and now has two children 1.1 and 1.2
1.1 forks and has a child 1.1.1

>
>    if ($command2_pid == 0){
>       `external_prog2`
>     }

That condition is true for 1

>    }
>     waitpid($command2_pid,0);
>
>    if ($command3_pid == 0){
>      `external_prog3`;
>    }

This condition is true for 1.2 and 1.1.1

>     waitpid($command3_pid, 0);
>
>    }
> }
snip
> Any idea how to solve this chicken and egg problem?
snip

After every fork you must should check the pid and route the parent
and child on different tracks in the code.

Other things I noticed while looking at your code,
    * backticks should be used to capture the STDOUT of an external
      program, not just run it, use system instead.  If you want to
      prevent the program from printing to the screen say
          system "$command > /dev/null 2>&1";
    * old bareword style filehandles are bad, use lexical file handles
      instead
    * the two argument version of open is dangerous, use the three
      argument version instead:
          open my $lexical_fh, "-|", "external_prog1" or die "etc";

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to