in reply to Killing a Forked Subprocess

Update: If signal selection is the real problem as merlyn and dws are pointing out, the below code might be overkill or just totally off the mark.


On *nix, you might be able to use setpgrp() to send a kill signal to the process group, not just the forked process

## pseudocode, so won't do any good as it is if( $i_am_the_child ) { setpgrp(); `some_cmd`; } else { ## parent sleep $sleepamt; ## I think 15 is SIGTERM. please confirm on ## on your system kill -15, getpgrp( $child_pid ); }

While I think the above idiom is correct, I suggest you read perldoc -f kill and setpgrp to make sure that that's what you want... ;)

Replies are listed 'Best First'.
Re: Re: Killing a Forked Subprocess
by IlyaM (Parson) on Mar 21, 2002 at 20:20 UTC
    I think 15 is SIGTERM. please confirm on your system

    Just use symbolic names for signals and don't worry about numbers:

    kill 'SIGTERM', getpgrp( $child_pid );

    --
    Ilya Martynov (http://martynov.org/)

      Wouldn't the symbolic name be 'TERM', not 'SIGTERM' ? And also, since you're sending the signal to a process group, don't you need to give it a negative signal value?

      =item kill LIST Sends a signal to a list of processes. The first element of the list must be the signal to send. Returns the number of processes successfully signaled. $cnt = kill 1, $child1, $child2; kill 9, @goners; Unlike in the shell, in Perl if the I<SIGNAL> is negative, it kills process groups instead of processes. (On System V, a negative I<PROCE +SS> number will also kill process groups, but that's not portable.) That means you usually want to use positive not negative signals. You may +also use a signal name in quotes. See L<perlipc/"Signals"> for details.
        Wouldn't the symbolic name be 'TERM', not 'SIGTERM'?

        I've just tested it. Actually both of them work. At least on perl 5.6.1.

        And also, since you're sending the signal to a process group, don't you need to give it a negative signal value?

        Oops. I'm sorry, I missed that. I don't know if it is allowed to write kill '-SIGHUP', .... If not still constants from POSIX.pm can be used instead of numbers:

        use POSIX qw(SIGHUP); kill -(SIGHUP), ....;

        --
        Ilya Martynov (http://martynov.org/)

Re: Re: Killing a Forked Subprocess
by Super Monkey (Beadle) on Mar 21, 2002 at 19:32 UTC
    Thanks for you suggestion. I had tried something similar but was unsuccessful because I didn't use the <nobr>getpgrp($child_pid)</nobr> part. It seems to be the solution I was looking for.