Hi,

I have this code:

die "Could not fork command1!" unless defined (my $command1_pid = fork);
if ($command1_pid == 0){
  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);
      die "Could not fork command3" unless defined (my $command3_pid = fork);

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

    }
}



That is, run external_prog1 and watch the output.. as soon as it sees a 
particular pattern, run two other external commands at the same time. However, 
with the above code, I keep getting duplicated external_prog2 process everytime 
i run the script. I need to track both PIDs of external_prog2 and 3 as I need 
to kill them later in the code. However, i'm having trouble running them at the 
same time.
if I will put a "kill 15, $command3_pid" right after the first waitpid, i will 
surely kill one of those two command 2 process. I tried several places to put 
"die" lines and "waitpids" but I couldn't get it right. It's either they don't 
run at the same time or they will run but command 2 has a twin brother.

Any idea how to solve this chicken and egg problem?




      

-- 
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