linux - Perl 5.8: possible to get any return code from backticks when SIGCHLD in use -
when chld signal handler used in perl, uses of system , backticks send chld signal. system , backticks sub-processes, neither wait nor waitpid seem set $? within signal handler on suse 11 linux. there any way determine return code of backtick command when chld signal handler active?
why want this? because want fork(?) , start medium length command , call perl package takes long time produce answer (and executes external commands backticks , checks return code in $?), , know when command finished can take action, such starting second command. (suggestions how accomplish without using sigchld welcome.) since signal handler destroys backtick $? value, package fails.
example:
use warnings; use strict; use posix ":sys_wait_h"; sub reaper { $signame = shift @_; while (1) { $pid = waitpid(-1, wnohang); last if $pid <= 0; $rc = $?; print "wait()=$pid, rc=$rc\n"; } } $sig{chld} = \&reaper; # system can made work not using $?, instead using system return value $rc = system("echo hello 1"); print "hello \$?=$?\n"; print "hello rc=$rc\n"; # backticks, when need output, cannot made work?? @io = `echo hello 2`; print "hello \$?=$?\n"; exit 0;
yields -1 return code in places might try access it:
hello 1 wait()=-1, rc=-1 hello $?=-1 hello rc=0 wait()=-1, rc=-1 hello $?=-1
so cannot find anywhere access backticks return value.
this same issue has been bugging me few days now. believe there 2 solutions required depending on have backticks.
if have backticks inside child code:
the solution put line below inside child fork. think statement above "if turn off chld handler around backticks might not signal if child ends" incorrect. still callback in parent when child exits because signal disabled inside child. parent still gets signal when child exits. it's child doesn't signal when child's child (the part in backticks) exits.
local $sig{'chld'} = 'default'
i'm no perl expert, have read should set chld signal string 'ignore' did not work in case. in face believe may have been causing problem. leaving out appears solve problem guess same setting default.
if have backticks inside parent code:
add line reaper function: local ($!, $?);
what happening reaper being called when code inside backticks completes , reaper setting $?. making $? local not set global $?.
Comments
Post a Comment