关于alarm函数
#include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); signal(SIGALRM,handler);让内核做好准备,一旦接受到SIGALARM信号,就执行 handler alarm(5); }/*这段函数的执行时间不计算在for循环的时间之内*/ main() { int i; handler(); for(i=1;i<21;i++){ printf("sleep %d ...\n",i); sleep(1); } } 结果:刚开始在main中执行一次,然后每隔5秒执行一次handler() Hello sleep 1 ... sleep 2 ... sleep 3 ... sleep 4 ... sleep 5 ... Hello sleep 6 ... sleep 7 ... sleep 8 ... sleep 9 ... sleep 10 ... Hello sleep 11 ... sleep 12 ... sleep 13 ... sleep 14 ... sleep 15 ... Hello sleep 16 ... sleep 17 ... sleep 18 ... sleep 19 ... sleep 20 ... Hello