yanbin's Blog

比 alarm() 分辨率更高的 timer: setitimer()

getitimer, setitimer - get or set value of an interval timer
 
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
 
seitimer() 设置三個 process 的内部定时器中的某一個, getitimer() 获得这三個定时器中某一個信息。
每一个 process 都有三个不同类型的定时器,which 的值对应: 
ITIMER_REAL  /* 运行在 real time, 定时器到期时提交  SIGALRM 信号 和 alarm()
                           * 提交给进程的是同一个信号。
                          */
ITIMER_VIRTUAL // 运行在 process 运行时,定时器到期时提交 SIGVTALRM 信号
ITIMER_PROF // 运行在 process 运行或在后台运行时,定时器到期时提交 SIGPROF 信号
 
struct itimerval {
       struct timeval it_interval; /* next value */
       struct timeval it_value; /* current value */
};
 
struct timeval {
       time_t tv_sec; /* seconds */
       suseconds_t tv_usec; /* microseconds */
};
 
0.  設置 it_value.tv_sec > 0 || it_value.tv_usec > 0 timer 開始運行,
    並且在固定頻率減少 it_value.tv_sec 和/或 it_value.tv_usec 的值。
    可以說 it_value 是 timer 的剩餘值。
1. 當 it_value.tv_sec == 0 && it_value.tv_usec == 0 時, timer expire。
    直接設置 it_value.tv_sec == 0 && it_value.tv_usec == 0 那麼 timer expire。
    NOTE: 對 timer it_value 的檢查可能會在一定的時間之後才會到來,
               所以不能假設清空了 it_value 就會 timer expire.
2. 一個 timer period 到期時,用 it_interval 的值初始化 it_value 開始下一個週期,
    如果 it_interval.tv_sec > 0 || it_interval.tv_usec > 0 話。
    那麼用 it_interval 的值初始化 it_value 開始下一個 timer period
3. 可以同時清空 it_interval 和 it_value 可以讓 timer 停止運行。




Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee