killing the children

Discussion in 'Perl Misc' started by lekonna, Mar 2, 2008.

  1. lekonna

    lekonna Guest

    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
     
    lekonna, Mar 2, 2008
    #1
    1. Advertising

  2. lekonna <> wrote:
    >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
     
    Jürgen Exner, Mar 2, 2008
    #2
    1. Advertising

  3. lekonna

    Guest

    lekonna <> wrote:
    > 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.
     
    , Mar 2, 2008
    #3
  4. lekonna

    C. Lumina Guest

    lekonna wrote:
    > 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
     
    C. Lumina, Mar 3, 2008
    #4
  5. lekonna

    Guest

    "C. Lumina" <> wrote:
    > lekonna wrote:
    > > 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.


    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.
     
    , Mar 3, 2008
    #5
  6. lekonna

    C. Lumina Guest

    wrote:
    > "C. Lumina" <> wrote:
    >> lekonna wrote:
    >>> 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.

    >
    > Talk about unfortunate choice of words!


    AK! Geeze that was a horrible one on my part. (I of course meant to
    write "reaped".)

    -CL
     
    C. Lumina, Mar 3, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Lindsey
    Replies:
    1
    Views:
    1,526
    Steve W. Jackson
    Sep 22, 2003
  2. Laura Conrad

    Killing children

    Laura Conrad, Aug 6, 2004, in forum: Python
    Replies:
    15
    Views:
    739
    Laura Conrad
    Aug 8, 2004
  3. loorthu
    Replies:
    0
    Views:
    682
    loorthu
    May 20, 2008
  4. Chris Angelico
    Replies:
    6
    Views:
    489
    Dan Stromberg
    Dec 7, 2011
  5. Oliver

    killing my children

    Oliver, Jun 7, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    182
    Nick Howes
    Jun 13, 2004
Loading...

Share This Page