in reply to Killing children's of children

On Unix systems you can create groups of processes and send a signal to every process in a group by calling kill() with a negative signal number. Have a look at the documentation for setpgrp() and getpgrp().

For a quick overview of process groups, have a look at this link: UNIX Signals and Process Groups.

Replies are listed 'Best First'.
Re^2: Killing children's of children
by almut (Canon) on Jul 17, 2008 at 13:30 UTC

    I'd second what pc88mxer suggested. — Here's a demo snippet illustrating the idea (Linux version):

    #!/usr/bin/perl my $sid = getppid; # for the 'ps' process selection only if (my $pid = fork()) { sleep 1; # wait somewhat until tree of children is set up # show related processes system "ps f -s $sid -o pid,ppid,pgrp,sid,cmd"; # kill process group after timeout sleep 10; kill -9, $pid; wait; print "killed children\n"; # show remaining processes system "ps f -s $sid -o pid,ppid,pgrp,sid,cmd"; # do something else... sleep 10; } elsif (defined $pid) { # create new process group setpgrp; # run something (consisting of several processes) that hangs system 'bash -c "cat; echo"'; exit; } else { die "couldn't fork"; }

    Sample output:

    $ ./698259.pl PID PPID PGRP SID CMD 2963 2960 2963 2963 bash 4074 2963 4074 2963 \_ /usr/bin/perl ./698259.pl 4075 4074 4075 2963 \_ /usr/bin/perl ./698259.pl 4076 4075 4075 2963 | \_ bash -c cat; echo 4077 4076 4075 2963 | \_ cat 4078 4074 4074 2963 \_ ps f -s 2963 -o pid,ppid,pgrp,sid,cmd killed children PID PPID PGRP SID CMD 2963 2960 2963 2963 bash 4074 2963 4074 2963 \_ /usr/bin/perl ./698259.pl 4079 4074 4074 2963 \_ ps f -s 2963 -o pid,ppid,pgrp,sid,cmd

    As you can see in the PGRP column, the three child processes in question all belong to the same process group (here 4075).

    The idea should in principle even be portable to Windows (never tried it myself though...). As BrowserUk recently noted, that would be Win32::CreateProcess with CREATE_NEW_PROCESS_GROUP.