Linux的定时器在服务端程序设计中,与时间有关的常见任务有:
Linux 时间函数Linux 的计时函数,用于获得当前时间:
定时函数,用于让程序等待一段时间或安排计划任务:
一般情况下 获取当前时间常用 gettimerofday,因为它的精度是1us,并且在x86平台上它是用户态实现的,没有系统调用和上下文切换的开销。 定时函数中:
实现下面是用timer_create实现的一个定时器:
gcc -o timer1 timer1.c -lpthread -lrt 以及一个用setitimer实现的定时器 setitimer中的第一个参数有三类: 这里说得很清楚。 ITIMER_REAL decrements in real time, and delivers SIGALRM upon expiration. ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration. ITIMER_PROF decrements both when the process executes and when the system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.
参考:http://www.cnblogs.com/Solstice/archive/2011/02/06/1949555.html |
|