killing the children

L

lekonna

Hi guys,

i'm having a bit of a problem here with the exec functionality. I seem
to be unable to kill the children of my process. Hope you can help me
with this one.

I'm doing a client-server setup where the server requests the clients
to start instances of a certain application and now the bugger doesn't
seem to die properly. The code in nutshell is here, i'll eliminate my
crappy message handling and just quote the handler funcs:

<start block>
$pid = fork;
if ( $pid == 0 )
{
exec( $my_command,$my_args) or die "can't do it Jim!";
} else {
$jobs{$pid} = $my_args;
}
</start block>

<kill block>
foreach ( keys( $jobs ) )
{
kill 9,$_;
}
delete %jobs;
</kill block>

What happens is that the exec starts the program properly and returns
a pid. the kill however seems to be unable
to do anything to the program that is executed, it just keeps
running.

Am i actually execing a command shell instance that then starts the
program as child and my kill hangs there?

At the moment coding in Win32 environment but the code should port to
linux/osX as well.

Br,
Lekonna
 
J

Jürgen Exner

lekonna said:
i'm having a bit of a problem here with the exec functionality. I seem
to be unable to kill the children of my process.

Am i actually execing a command shell instance that then starts the
program as child and my kill hangs there?

Well, there's your problem. Your kill() is killing the shell (which at that
time is probably dead already anyway) but not the children of the shell.

You need a way to identify/kill the grandchildren of the perl process,
maybe by killing the process tree of the shell or have the shell return the
ID of it's own children or something like that.

jue
 
X

xhoster

lekonna said:
Hi guys,

i'm having a bit of a problem here with the exec functionality. I seem
to be unable to kill the children of my process. Hope you can help me
with this one.

I'm doing a client-server setup where the server requests the clients
to start instances of a certain application and now the bugger doesn't
seem to die properly. The code in nutshell is here, i'll eliminate my
crappy message handling and just quote the handler funcs:

You should include enough of the other stuff to make it actually runnable.


my $my_command="perl";
my @my_args=('-le', 'warn $$; sleep 2; warn $$');
$pid = fork;
if ( $pid == 0 )
{
exec( $my_command,@my_args) or die "can't do it Jim!";
} else {
$jobs{$pid} = $my_args[0];
}
sleep 1;
foreach ( keys( %jobs ) )
{
warn "killing $_";
kill 9,$_;
}
sleep 3;
__END__

On Linux, the second warn $$ never gets executed because that child has
already been killed. On Windows, sometimes it works that way and sometimes
it doesn't. An example of one of the times it doesn't:

H:\perl_misc>perl kill.pl
2364 at -e line 1.
killing -3872 at kill.pl line 16.
2364 at -e line 1.


What happens is that the exec starts the program properly and returns
a pid. the kill however seems to be unable
to do anything to the program that is executed, it just keeps
running.

Am i actually execing a command shell instance that then starts the
program as child and my kill hangs there?

You shouldn't be, since you are using the list form of exec. On Windows,
lords knows what might be going on.
At the moment coding in Win32 environment but the code should port to
linux/osX as well.

Good luck with that.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
C

C. Lumina

lekonna said:
Hi guys,

i'm having a bit of a problem here with the exec functionality. I seem
to be unable to kill the children of my process. Hope you can help me
with this one.

I must say, perhaps you could of chosen a better wording for the subject
line!

Best way I take care of situations like this is to maintain a list of
any children created by the parent and make sure they get raped properly
on exit.

-CL
 
X

xhoster

C. Lumina said:
I must say, perhaps you could of chosen a better wording for the subject
line!

Best way I take care of situations like this is to maintain a list of
any children created by the parent and make sure they get raped properly
on exit.

Talk about unfortunate choice of words!

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top