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 <[email protected]> wrote:
> From: Michael Alipio <[email protected]>
> Subject: killing a forked process
> To: "begginers perl.org" <[email protected]>
> 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: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/