当子进程从终端接收到SIGINT时,我遇到一些来自system()
的奇怪返回值。从Perl脚本parent.pl
解释我使用system()
作为子进程运行另一个Perl脚本,但我也需要通过shell运行子,所以我使用了system 'sh', '-c', ...
形式..因此,子的父母变成了sh
进程和sh
进程的父级变为parent.pl
。另外,为了避免sh
进程收到信号,我将其捕获。当使用SIGINT默认处理程序时从系统()返回值
例如,parent.pl
:
use feature qw(say);
use strict;
use warnings;
for (1..3) {
my $res = system 'sh', '-c', "trap '' INT; child$_.pl";
say "Parent received return value: " . ($res >> 8);
}
其中child1.pl
:
local $SIG{INT} = "DEFAULT";
sleep 10;
say "Child timed out..";
exit 1;
child2.pl
:
local $SIG{INT} = sub { die };
sleep 10;
say "Child timed out..";
exit 1;
和child3.pl
是:
eval {
local $SIG{INT} = sub { die };
sleep 10;
};
if ([email protected]) {
print [email protected];
exit 2;
}
say "Child timed out..";
exit 0;
如果我运行parent.pl
(命令行),然后按CTRL-C中止每个子进程,输出结果是:
^CParent received return value: 130
^CDied at ./child2.pl line 7.
Parent received return value: 4
^CDied at ./child3.pl line 8.
Parent received return value: 2
现在,我想知道为什么我得到一个返回值情况1为130,并且情况2的返回值为4.
此外,在这种情况下,确切知道"DEFAULT"
信号处理程序的功能是很好的。
注:如果我更换sh
与bash
(和陷阱SIGINT
代替INT
在bash
)相同的值返回。
参见:
'errno' 4是'EINTR',“中断的系统调用”。当信号进入时,“睡眠”设置了此项。 – ikegami