当我跟踪此代码时
void printMsg();
int main() {
signal(SIGPROF,printMsg);
struct itimerval tick;
memset(&tick,sizeof(tick));
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
tick.it_interval.tv_sec = 0;
tick.it_interval.tv_usec = 0;
setitimer(ITIMER_PROF,&tick,NULL);
while(1) {
;
}
return 0;
}
void printMsg() {
printf("%s","Hello World!!\n");
}
1秒后我收到了SIGPROF
信号
...
05:54:10 setitimer(ITIMER_PROF,{it_interval={0,0},it_value={1,0}},NULL) = 0
05:54:11 --- SIGPROF {si_signo=SIGPROF,si_code=SI_KERNEL} ---
...
但是,当我在write(2,"",0)
中添加诸如read(2,0)
或while(1)
的系统调用并再次跟踪时,似乎无法触发SIGPROF
。但是,time(0)
中的while(1)
可以正确触发SIGPROF。
顺便说一句,我使用此代码模拟以下PHP脚本,该脚本忽略了PHP-FPM SAPI下的时间限制,
<?php
set_time_limit(5); // PHP uses setitimer(ITIMER_PROF) to implement this function
while (true) {
flush(); // PHP uses write(<fd>,0) to implement this function
}