为什么不死于警报信号处理程序中而杀死进程? [英] Why doesn't die in alarm signal handler kill the process?

查看:105
本文介绍了为什么不死于警报信号处理程序中而杀死进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Perl系统调用指定超时限制?

From How can I specify timeout limit for Perl system call?

eval { 
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required 
    alarm $timeout; 
    $nread = sysread SOCKET, $buffer, $size; 
    alarm 0; 
}; 
if ($@) { 
    die unless $@ eq "alarm\n";   # propagate unexpected errors 
    # timed out 
} 
else { 
    # didn't 
} 

如果发生超时,则sub { die "alarm\n" };应该导致进程结束.我想我听不懂die.此 http://www.cs.cf.ac.uk/Dave/PERL/node111.html 说:"die()函数用于退出脚本并显示一条消息供用户阅读."但是,对于上述脚本,该脚本将在#超时时间内处理代码. sysread也将继续工作.我有一个perl脚本,它休眠了30秒,而不是sysread.我的超时设置为10秒.如预期的那样,执行了#超时中的代码,但脚本继续休眠.任何输入均受到赞赏

If a timeout happens, should sub { die "alarm\n" }; cause the end of a process. I guess I am not able to understand die. This http://www.cs.cf.ac.uk/Dave/PERL/node111.html says that "The die() function is used to quit your script and display a message for the user to read". However, in the case of the script above, the script will process the code in #timed out. Also sysread continues to work. Instead of sysread, I had a perl script that slept for 30 seconds. My timeout was set to 10 seconds. As expected, the code in #timed out is executed but the script continued to sleep.Any inputs appreciated

推荐答案

die 不会导致在过程结束时,它将引发异常.

die doesn't cause the end of a process, it throws an exception.

现在,如果什么都没有捕获到异常,则该异常结束了一个过程,但是您可以使用代码来捕获这个非常例外.

Now, if nothing catches an exception, that ends a process, but you have code in place to catch this very exception.

该过程不会结束,因为您明确阻止它结束.

The process doesn't end because you explicitly prevent it from ending.

由于您不清楚所要获得的行为,因此可能还有另一种可能性:您正在使用Windows版本的Perl.

Since you're not very clear on what behaviour you are getting, there could be another possibility: That you are using a Windows build of Perl.

alarm是Unix系统调用.因为Windows没有信号,所以非常目的(经过一定时间后发送信号)在Windows上是没有意义的.

The alarm is a Unix system call. It's very purpose (sending a signal after a certain amount of time has passed) makes no sense on Windows since Windows doesn't have signals.

Perl在某种程度上模拟了alarm,但仅以非常有限的方式. sleep很可能是唯一可由alarm中断的操作.否则,仅在语句之间检查超时.

Perl emulates alarm to some extent, but only in a very limited manner. sleep could very well be the only operation that's interruptable by alarm. Otherwise, the timeout is only checked between statements.

因此它不会中断sysread,但是一旦sysread返回,Perl就会注意到超时已到期,然后模拟一个信号.

So it won't interrupt sysread, but once sysread returns, Perl notices the timeout expired and emulate a signal then.

这篇关于为什么不死于警报信号处理程序中而杀死进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆