Your Mother has asked for the wisdom of the Perl Monks concerning the following question:

I recently gave some advice about negating the PID of process to kill its children: Re: open (to read) and kill a pipe to a pipe. According to the perldoc for kill, I was wrong. And the OP in that thread reported that doing it the way I suggested didn't work but trying per the doc did work, on Windows no less. But the "wrong" way seems to work on *nix too. Anyone know why?

system("sleep 1234 &"); my ( $pid ) = qx{ pgrep -f sleep | xargs ps | grep "1234" | grep -v grep } =~ /(\d+)/; print "Check me\n\tps $pid\n"; # kill 9 => $$; # Leaves sleep process alive. # kill -9 => $$; # Kills sleep process. kill 9 => -$$; # Also kills sleep process.
(Updated title to include "kill.")

Replies are listed 'Best First'.
Re: Kill negated PIDs v negated SIGs
by psini (Deacon) on Aug 27, 2010 at 19:15 UTC

    From perldoc -f kill:

    Unlike in the shell, if SIGNAL is negative, it kills process groups instead of processes. (On System V, a negative PROCESS 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.

    So the wrong way is right on *nix.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Kill negated PIDs v negated SIGs
by ikegami (Patriarch) on Aug 27, 2010 at 23:12 UTC

    kill(2)

    That extension is not possible on Windows because it uses negative PIDs for the pseudo-processes created by fork.

      Win32 doesn't kill process groups--the concept doesn't really exist, though Job objects are a close analogy.

      However, since (I think) 5.10, kill with a negative signal id (ala SysV), will terminate the identified process(es), and all descendant of that (those) process ids.

      Whilst it hasn't always been available through documented APIs, Windows (since NT4) has always retained knowledge of the parent pid (ppid) that started any given process. If you have ProcessExplorer and use the "Show Process Tree" view, it is the ppid that is used to organise the hierarchy.

      Hence, if a negative signal is used, for a given process id, it is possible to determine what (if any) processes were start by that id, and recursively kill them, along with any children they have.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.