pht has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks, I humbly ask for opinions regarding the following matter:
perl -e 'open(my $y,"/usr/bin/sleep 5|");die;'The problem: This program takes five seconds to return to shell prompt despite the die.
I know what's going on -- the perl program waits for the child to terminate, which won't happen until 5 seconds have passed. The prerequisite is that the child does not output anything, otherwise it is likely to die prematurely of borked pipe (cf. /usr/bin/yes -- perl does at least close the file handle before wait()ing).
The big problem corollary: if the child takes forever to exit (eg. /usr/bin/cat /dev/null), so does the perl script.
The question: what are the ways around this? In my reasoning, obviously one must set up a $SIG{__DIE__} handler that forcibly removes the child from existence. I came up only with open2 which returns the pid, that can be then passed to kill 9, $pid. A more esoteric way would be to try to local $SIG{TERM} = 'IGNORE'; kill -15, -$$ or something alike (ie. pop a nuke on all processes in my process group while simultaneously shielding from it).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: open pipe for input and die
by Corion (Patriarch) on Dec 11, 2008 at 09:26 UTC | |
by pht (Acolyte) on Dec 11, 2008 at 09:37 UTC | |
|
Re: open pipe for input and die
by eye (Chaplain) on Dec 13, 2008 at 07:24 UTC |