编写一个程序(库),实现定时器(计时器)的功能,它能为用户提供在同一进程中多次使用的定时器。这里要求用信号来实现。 示例代码如下: #include <stdio.h> #include <time.h> #include <sys/time.h> #include <stdlib.h> #include <signal.h> static int time_count = 0; static struct itimerval oldtv; void set_timer() { struct itimerval itv; itv.it_interval.tv_sec = 1; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = 1; itv.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &itv, &oldtv); } void signal_handler(int m) { time_count ++; printf("%d/n", time_count); } int main() { signal(SIGALRM, signal_handler); set_timer(); while(1){ if(time_count==5) { printf("time_count = 5/n"); time_count=0; } } return 1; }
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/opensource/191466.html