unix 下常见的处理信号
代码如下:
no name default action description
1 sighup terminate process terminal line hangup
2 sigint terminate process interrupt program
3 sigquit create core image quit program
4 sigill create core image illegal instruction
5 sigtrap create core image trace trap
6 sigabrt create core image abort program (formerly sigiot)
7 sigemt create core image emulate instruction executed
8 sigfpe create core image floating-point exception
9 sigkill terminate process kill program
10 sigbus create core image bus error
11 sigsegv create core image segmentation violation
12 sigsys create core image non-existent system call invoked
13 sigpipe terminate process write on a pipe with no reader
14 sigalrm terminate process real-time timer expired
15 sigterm terminate process software termination signal
16 sigurg discard signal urgent condition present on socket
17 sigstop stop process stop (cannot be caught or ignored)
18 sigtstp stop process stop signal generated from keyboard
19 sigcont discard signal continue after stop
20 sigchld discard signal child status has changed
21 sigttin stop process background read attempted from control terminal
22 sigttou stop process background write attempted to control terminal
23 sigio discard signal i/o is possible on a descriptor (see fcntl(2))
24 sigxcpu terminate process cpu time limit exceeded (see setrlimit(2))
25 sigxfsz terminate process file size limit exceeded (see setrlimit(2))
26 sigvtalrm terminate process virtual time alarm (see setitimer(2))
27 sigprof terminate process profiling timer alarm (see setitimer(2))
28 sigwinch discard signal window size change
29 siginfo discard signal status request from keyboard
30 sigusr1 terminate process user defined signal 1
31 sigusr2 terminate process user defined signal 2
perl的信号处理原理
perl 提供了%sig 这个特殊的默认hash.调用需要使用到系统保留全局hash数组%sig,即使 用'$sig{信号名}'截取信号,相当于,在perl程序中出现这个信号时,执行我们自己定义某段代码(子函数)的地址值(定义信号响应函数),这代码就是截取这个信息后要执行的结果了.
举个sigalrm例子,也就是超时处理:
代码如下:
my $timeout = 10 ;
eval {
local $sig{alrm} = sub { die alarm\n }; # \n required
alarm $timeout; #如果到了$timeout时间就会去执行上面的sub
sleep 15;
print if timeout ,this will not print;
alarm 0; #恢复到默认的状态
};
if ($@) {
die unless $@ eq alarm\n; #可能捕获的不是超时,是其他错误,就die吧
print timeout \n ;
}
else {
print not timeout;
}
这里要说一下perl的错误捕获机制
代码如下:
eval {
open(fh,”domains.txt”) or die “can't open files,$!”;
};
捕获异常
代码如下:
if($@){#出现异常}
else{#无异常,打印文件内容
while(){
…
}
close fh;
}
如果eval块中的程序有语法错误、运行时错误或者遇到die语句,eval将返回undef。错误码被保存在$@中。
2025国考·省考课程试听报名