Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Killing process group run with IPC::Open3

by ikegami (Pope)
on Feb 03, 2012 at 00:06 UTC ( #951562=note: print w/replies, xml ) Need Help??

in reply to Killing process group run with IPC::Open3

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

$ perl 2 perl -e'system("sleep 4; echo My work is done")' timed out sent kill to 0 My work is done $?=0 $ perl 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 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.

Replies are listed 'Best First'.
Re^2: Killing process group run with IPC::Open3
by pm_sanchay (Initiate) on Feb 03, 2012 at 05:52 UTC
    Hi ikegami,
    That's a good point and infact I tried exec first (that's why use of setpgrp).  But problem is I also need to capture <stdin> in my app. Here is example:
    > cat
    print "continue? "; $yesno=<STDIN>; print "--$yesno";
    > perl 5 perl -e 'system("perl")'
    continue? y
    alarmed out
    sent kill signal to 1
    > perl 5 perl -e 'system("perl")'
    continue? y
    Child exit status: 0 signal: 0
    Is there any way to map <STDIN> in ?

      Capture an input? Your terminology makes no sense to me. What do you mean by "capture <stdin>"?

      Whatever it is you want to do, if you know how to do it with open3, keep doing it that way. There's nothing special about "" on that side of things.

      PS — Please don't use <pre>. At all. Place code, computer text, inputs, outputs, etc in <code></code> or <c></c> tags. And use <p> at the start of paragraphs.

      So I ran a few tests. It seems that the child can't read from the parent's STDIN. I presume this is the problem to which you are referring.

      man 2 setpgrp explains

      At any time, one (and only one) of the process groups in the session can be the foreground process group for the terminal; the remaining process groups are in the background.

      I don't know what a session is, but adding setsid() appears to solve the problem.

        My bad for using pre. I will be more careful from now.

        Yes, I meant "child can't read from the parent's STDIN".


        does appear to work. But it has another issue - I don't understand what happens if a KILL signal is sent to parent (example cntl-c)? Will have to understand what setsid does.

        Thanks for your help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951562]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-03-23 20:58 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (296 votes). Check out past polls.