Hi,

I'm trying to launch and external program using fork.



my $pid = fork()

if ($pid == 0){

system ("top");
exit (0);
}else{
waitpid ($pid, 0)
}

print "External Program died!";


The problem is "top", or the program i'm actually going to run always detaches 
to the child perl process I have forked. If I open another terminal while my 
script is running and kill the child's PID, (usually the one with higher number 
because there are two perls running), the external program still keeps running..

How do i tell it to stick to the child process so that when i kill the child, 
the external program will die. Is there a way to capture the PID of the 
external program? I tried using "open" to launch it but open quickly exits the 
program after assigning it to a variable, e.g my $extern_pid = open TOP, "top 
|" or die $!;


 




--- On Mon, 4/13/09, Michael Alipio <daem0n...@yahoo.com> wrote:

> From: Michael Alipio <daem0n...@yahoo.com>
> Subject: killing a forked process
> To: "begginers perl.org" <beginners@perl.org>
> Date: Monday, April 13, 2009, 4:49 PM
> Hi,
> 
> 
> My program looks like this:
> 
> my $pid = fork ();
> if ($pid == 0){
>   exec ("top");
> }else{
>    my $time = 0
>    while (<1>){
>      sleep 1;
>      $time++;
>      if ($time == 10){
>          kill 9, $pid;
>      }
>    }
> }
> 
> 
> That is, after running the forked "top" process
> for 10 seconds, the main program will end it.
> 
> Any idea why it's not working for me?
> 
> Thanks
> 
> 
> 
> 
>       
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/


      

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