getitimer, setitimer [linux]_[interval timer]
头文件
#include <sys/time.h>
函数原型
int getitimer(int which, struct itimerval *curr_value);
? int setitimer(int which, const struct itimerval *new_value, ? struct itimerval
*old_value);
时钟类型
man原文
ITIMER_REALThis timer counts down in real (i.e., wall clock) time. At each expiration, a SIGALRM signal is generated.ITIMER_VIRTUALThis timer counts down against the user-mode CPU time consumed by the process. (The measurement includes CPU time consumed by all threads in the process.) At each expiration, a SIGVTALRM signal is generated.ITIMER_PROFThis timer counts down against the total (i.e., both user and system) CPU time consumed by the process. (The measurement includes CPU time consumed by all threads in the process.) At each expiration, a SIGPROF signal is generated
解析
-
真实时钟 ITIMER_REAL
以系统真实的时间计算, 每次触发返回 SIGALRM 信号
-
虚拟时钟 ITIMER_VIRTUAL
该进程在用户状态下的时间计算, 每次触发返回 SIGVTALRM信号
-
实用时钟 ITIMER_PROF
以系统空间和用户空间的时间计算, 每次触发返回 SIGPROF 信号
参数解析
-
witch参数 指定三种时钟类型 的一种
-
结构体指针 curr_value
struct itimerval { struct timeval it_interval; /* Interval for periodic timer 每it_interval周期发送signal*/struct timeval it_value; /* Time until next expiration 在第一次调用时钟后, 延时it_value时间触发时钟*/ };
-
结构体 timerval
struct timeval { time_t tv_sec; /* seconds 秒*/ suseconds_t tv_usec; /* microseconds微秒*/ };
-
代码举例
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>int main(){
void wakeUP(int);
// int getitimer(int which, struct itimerval *curr_value);
// int setitimer(int which, const struct itimerval *new_value,
// struct itimerval *old_value);signal(SIGALRM, wakeUP);struct itimerval myTimer;// initiante myTimermyTimer.it_interval.tv_sec = 1; // period time settingmyTimer.it_interval.tv_usec = 100;myTimer.it_value.tv_sec = 1; // delay time settingmyTimer.it_value.tv_usec = 100;setitimer(ITIMER_REAL,&myTimer, NULL ) ;// note the point while (1){
printf("about to sleep ~\n ");pause();}return 1;}void wakeUP(int signum){
printf("wake up ! man !\n");
}