如果不要求很精确的话,用 alarm() 和 signal() 就够了
代码:
- /*
- * Example for alarm.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- void sigalrm_fn(int sig)
- {
- /* Do something */
- printf("alarm!\n");
- alarm(2);
- return;
- }
- int main(void)
- {
- signal(SIGALRM, sigalrm_fn);
- alarm(2);
- /* Do someting */
- while(1) pause();
- }
阅读(1077) | 评论(0) | 转发(0) |