getitimer, setitimer [linux]_[interval timer]

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_REAL
   
   This timer counts down in real (i.e., wall clock) time.  At each expiration, a SIGALRM signal is generated.

   ITIMER_VIRTUAL
   
   This 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_PROF
   
   This  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 myTimer
	myTimer.it_interval.tv_sec = 1; // period time setting
	myTimer.it_interval.tv_usec = 100;
	myTimer.it_value.tv_sec = 1; // delay time setting
	myTimer.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");
}

结果展示(后面还有, 图示不完全)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值