你問,如果你有
$SIG{ALRM} = sub {
alarm(15*60);
call_stored_proc();
};
alarm(15*60);
和call_stored_proc
需要超過15分鐘會發生什麼。你爲什麼不試試呢?
perl -e'
use feature qw(say);
my $slow = 2;
my $done = 0;
sub call_stored_proc {
say sprintf "[%s] %s: %s", time, "call_stored_proc", "enter";
sleep($slow ? 8 : 2);
say sprintf "[%s] %s: %s", time, "call_stored_proc", "leave";
$done = 1 if !$slow;
--$slow;
}
$SIG{ALRM} = sub {
say sprintf "[%s] %s: %s", time, "SIGALRM hander", "enter";
say sprintf "[%s] %s: %s", time, "SIGALRM hander", "alarm set for ".(time+5);
alarm(5);
call_stored_proc();
say sprintf "[%s] %s: %s", time, "SIGALRM hander", "leave";
};
say sprintf "[%s] %s: %s", time, "[root]", "alarm set for ".(time+5);
alarm(5);
sleep(1) while !$done;
'
輸出:
[1474490009] [root]: alarm set for 1474490014
[1474490014] SIGALRM hander: enter
[1474490014] SIGALRM hander: alarm set for 1474490019
[1474490014] call_stored_proc: enter
[1474490022] call_stored_proc: leave
[1474490022] SIGALRM hander: leave
[1474490022] SIGALRM hander: enter
[1474490022] SIGALRM hander: alarm set for 1474490027
[1474490022] call_stored_proc: enter
[1474490030] call_stored_proc: leave
[1474490030] SIGALRM hander: leave
[1474490031] SIGALRM hander: enter
[1474490031] SIGALRM hander: alarm set for 1474490036
[1474490031] call_stored_proc: enter
[1474490033] call_stored_proc: leave
[1474490033] SIGALRM hander: leave
正如你所看到的,call_stored_proc
不中斷。 SIGALRM被抑制,直到SIGALRM處理程序返回。
請注意,如果您想獲得不同的結果,您可以確實揭露SIGALRM。 – ikegami
感謝ikegami,精彩的模擬! – Shashi