I'm guessing the process to which you send a "negative signal" must be the head of a process group. Testing corroborates.

$ perl test.pl 2 perl -e'system("sleep 4; echo My work is done")' timed out sent kill to 0 My work is done $?=0 $ perl test.pl 2 perl -MPOSIX -e'setpgrp(0,0); system("sleep 4; echo M +y work is done")' timed out sent kill to 1 $?=15

You could solve your problem by using the following:

eval{ local $SIG{ALRM} = sub {die "alarm\n"}; alarm $timeout; $cpid = open3("<&STDIN", ">&STDOUT", ">&STDERR", '-'); if (!$cpid) { # Child setpgrp(0,0); exec(@ARGV) or die "exec: $!"; } waitpid($cpid, 0); alarm 0; };
$ perl fixed.pl 2 perl -e'system("sleep 4; echo My work is done")' timed out sent kill to 1 $?=15

This code works with older version of Perl (e.g. Perl 5.10), but I'm getting an "illegal seek" error with newer version of Perl. I'll look into it. Especially since I think it might be in the code I wrote for open3 X_X.

PS — open3(...) or die $!; is wrong. open3 dies on error rather than returning false.


In reply to Re: Killing process group run with IPC::Open3 by ikegami
in thread Killing process group run with IPC::Open3 by pm_sanchay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.