Perl:阻塞信号没有按应有的方式延迟 ->提供测试代码

发布于 2024-08-06 07:53:38 字数 999 浏览 4 评论 0原文

在我正在编写的 Perl 脚本中,我遇到了一个问题,我阻止了 INT 和 QUIT 信号,在反引号内执行进程,然后取消阻止 INTQUIT 信号。我想防止 INTQUIT 到达子进程并杀死它。

该脚本在代码中的正确位置成功地阻塞和解除阻塞,但是,它不会延迟信号并执行我在阻塞模式下配置的处理程序,正如许多参考文献所说的那样。我知道它正在解除阻止,因为我可以在阻止或解除阻止命令之前或之后发送 SIGINT 并且它会受到尊重。

操作系统:Linux 2.6.30 Perl版本:5.8.8

代码片段:

#!/usr/local/bin/perl

use POSIX qw(:signal_h);

$SIG{'INT'} = 'gracefulExit';
sub gracefulExit { print "Caught Signal = GOOD\n"; exit; }

print "Recieving a SIGINT works here\n";
sleep 5;
my $sigset = POSIX::SigSet->new;
my $blockset = POSIX::SigSet->new(SIGINT);
sigprocmask(SIG_BLOCK, $blockset, $sigset) or die "dying at block...\n";
print "Recieving a SIGINT doesn't work here [GOOD!] and is NOT delayed [WHY!?].\n";
`/bin/sleep 5`;
sigprocmask(SIG_UNBLOCK, $blockset) or die "dying at unblock...\n";
print "Recieving a SIGINT works here again [GOOD!]\n";
sleep 5;
print "Exited without receiving a signal\n";

In a Perl script I'm writing I'm having a problem where I block the INT and QUIT signals, execute a process within backticks, and then unblock the INT and QUIT signals. I want to prevent an INT or a QUIT from reaching the child process and killing it.

The script successfully blocks and unblocks at the right points in the code, however, it doesn't DELAY the signal and execute the handlers I have configured while it's in blocking mode as many references say it should. I know it's unblocking because I can send a SIGINT before or after the block or unblock command and it is respected.

OS: Linux 2.6.30
Perl version: 5.8.8

Code snippets:

#!/usr/local/bin/perl

use POSIX qw(:signal_h);

$SIG{'INT'} = 'gracefulExit';
sub gracefulExit { print "Caught Signal = GOOD\n"; exit; }

print "Recieving a SIGINT works here\n";
sleep 5;
my $sigset = POSIX::SigSet->new;
my $blockset = POSIX::SigSet->new(SIGINT);
sigprocmask(SIG_BLOCK, $blockset, $sigset) or die "dying at block...\n";
print "Recieving a SIGINT doesn't work here [GOOD!] and is NOT delayed [WHY!?].\n";
`/bin/sleep 5`;
sigprocmask(SIG_UNBLOCK, $blockset) or die "dying at unblock...\n";
print "Recieving a SIGINT works here again [GOOD!]\n";
sleep 5;
print "Exited without receiving a signal\n";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔落墨 2024-08-13 07:53:38

我几乎可以肯定 Randal 在上面的评论中指出了这一点,但正确的测试是使用 strace 检查以验证您期望发生的信号系统调用是否确实存在。更一般地说,在系统调用级别工作时,您希望避免与旨在抽象它们的库函数混合。

I'm all but certain Randal nailed this in a comment above, but the proper test is to check with strace to verify that the signal syscalls you expect to be happening actually are. And more generally, when working at the system call level, you want to avoid mixing with library functions that are intended to abstract them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文