博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux用户态定时器用法以及犯错总结【转】
阅读量:7223 次
发布时间:2019-06-29

本文共 2176 字,大约阅读时间需要 7 分钟。

转自:

采样的时候要用到定时器,定时的进行采样。这时候,就会用到setitimer函数了。

1. 要使用setitimer函数,要包含头文件:#include <sys/time.h>

2. 该函数的原型是:int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);

3. 参数:

(1)int which:定时器分以下三种

ITIMER_REAL:decrements in real time, and deliversSIGALRM upon expiration.

以系统真实的时间来计算,它送出SIGALRM信号。

ITIMER_VIRTUAL:decrements only  when  the  process  is  executing,  anddeliversSIGVTALRM upon expiration.

以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。

ITIMER_PROF:decrements  both  when the process executes and when the system is executing on behalf 

of the  process. Coupledwith  ITIMER_VIRTUAL, this timer is usually used to profile the time

 spent by the application in user and  kernel space.  SIGPROF is delivered

以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号。

(2)struct itimerval *new_value,其定义如下:

struct itimerval {  

               struct timeval it_interval; /* 定时器间隔时间 */  
               struct timeval it_value; /* 定时器开始运行延时时间 */  
           };  
struct timeval {  
               long tv_sec; /* 秒 */  
               long tv_usec; /* 微秒 */  
           };  

其中it_value表示设置定时器后间隔多久开始执行定时任务,而it_interval表示两次定时任务之间的时间间隔。

(3)上一次定时器的值,一般置为NULL即可

4. 返回值:成功返回0;失败返回-1,并把错误号写到errno变量中

5. 犯错笔记

5.1

 

[cpp]
  1. ...  
  2. struct itimerval itv;  
  3.   
  4. if (signal(SIGALRM, samplingForComtrade) == SIG_ERR) {  
  5.     DBG("signal samplingForComtrade() bind failed !\n");  
  6.     exit(EXIT_FAILURE);  
  7. }  
  8. itv.it_value.tv_sec = 0;  
  9. itv.it_value.tv_usec = 0;  
  10. itv.it_interval.tv_sec = 0;  
  11. itv.it_interval.tv_usec = 1000;  
  12. if(setitimer(ITIMER_REAL,&itv,NULL) != 0){  
  13.     fprintf(stderr,"setitimer failed,errno = %d\n",errno);  
  14.     exit(EXIT_FAILURE);  
  15. }  
  16. ...  

  如上设置好后samplingForComtrade()函数怎么也不跑起来,查问题查到绝望,最后才发现当你把it_value参数里面的秒和微秒全部设置为0时,定时器是跑不起来的。。。 

settimer工作机制是,先对it_value倒计时,当it_value为零时触发信号,然后重置为it_interval,继续对it_value倒计时,一直这样循环下去。

假如it_value为0是不会触发信号的,所以要能触发信号,it_value得大于0;如果it_interval为零,只会延时,不会定时(也就是说只会触发一次信号)。

5.2

setitimer()函数调用完,定时器就跑一次处理函数,第二次的时候打印出 Alarm clock ,然后程序直接退出。

查到想死才查出原因:原来是由于编译的时候加了编译条件 -std=c99, 会跟定时器冲突,去掉就行了。。。无语!

5.3

使用了定时器后sleep()函数就不好使了,因为sleep函数也是使用的SIGALRM信号。

解决办法是采用其他延时方式,如select等。

【作者】
【出处】
【博客园】
【新浪博客】
【知乎】
【我的作品---旋转倒立摆】
【我的作品---自平衡自动循迹车】
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
你可能感兴趣的文章
BNU OJ 51000 BQG's Random String
查看>>
PAT (Advanced Level) 1044. Shopping in Mars (25)
查看>>
hdu 1531 King
查看>>
***R
查看>>
Linux 源码编译安装mysql
查看>>
取消手机端页面长按图片出现保存或者图片被打开的方法
查看>>
关于图片居中问题
查看>>
并发下的死锁问题
查看>>
Winserver下的Hyper-v “未在远程桌面会话中捕获到鼠标”
查看>>
oracle体系结构基础
查看>>
有关TCP和UDP 粘包 消息保护边界
查看>>
Mono为何能跨平台?聊聊CIL(MSIL)
查看>>
安装scrapy问题:-bash:scrapy:command not found
查看>>
CentOS7 重置root密码
查看>>
博客作业四
查看>>
Scanner 输入---从键盘输入两个数进行相加
查看>>
test
查看>>
说无可说
查看>>
mysql 语句优化
查看>>
SCP 命令参数使用详解(最详细使用指南)
查看>>