笔记
1.sleep()实现定时器,精度为1s
2.usleep()实现定时器,精度为1us
3.select实现定时器
通过timeval 中的tv_sec 和tv_usec 设置定时间隔
4.通过alarm()产生SIGALRM 信号,捕捉SIGALRM 信号实现定时器,alarm参数为定时时长
设置信号处理函数signal(SIGALRM , proc);
5.用settimer实现定时器
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));
which决定产生哪种信号:
ITIMER_REAL:以系统真实的时间来计算,它送出SIGALRM信号
ITIMER_VIRTUAL:以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号
ITIMER_PROF:以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号
这里用ITIMER_REAL,产生SIGALRM信号,同样使用信号处理函数signal(SIGALRM , proc)
6.使用RTC实现定时器,精度较高
位于/dev/rtc
用例
/* timer.c */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>
#include <time.h>
#include <linux/rtc.