定时器小结 setitimer/getitimer Or timer_settime/timer_gettime

    最近在移植Python至VxWorks,小小菜鸟遇到了粉多麻烦,其中定时器相关的就可独占一章。

    首先,对于定时器中使用的数据结构,在Python中使用的数据结构为itimerval,通过查阅VxWorks手册及源码可知,VxWorks支持的数据结构为itimerspec,如下:

    itimerval结构在UNIX/LINUX中定义,定义如下:

Struct itimerval{

Struct timeval it_interval; /* nextvalue:下一次触发所需的时间*/

Struct timeval it_value;  /*current value:目前距离触发时间点剩余的时间*/

};

Struct timeval{

Long tv_sec; /* seconds */

Long tv_usec; /*microseconds*/

};

    查阅VxWorks源码后认为,该数据结构与VxWorks中的itimespec结构意义相同,该结构在time.h中定义,VxWorks中定义如下:

structitimerspec

{

    struct timespecit_interval;        /* timer period(reload value) */

    struct timespecit_value;                /*timer expiration */

};

structtimespec

{                                           /*interval = tv_sec*10**9 + tv_nsec */

    time_t tv_sec;                        /*seconds */

    long tv_nsec;                        /*nanoseconds (0 - 1,000,000,000) */

};

    修改完毕并编译APP通过后下载时出现问题,提示_setitimer, _getitimer符号出错,原来python代码中定时器使用的是setitimer和getitimer函数,显然,这两个函数在VxWorks系统中是不支持的,不仅手册中没有该函数介绍,就连VxWorks源码中说明setitimer的地方实际使用的也是timer_settime函数。

1 setitimer()/getitimer()

Setitimer/getitimer为Linux的API,并非C语言的标准库。函数定义为:

#include <sys/time.h>

int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value));

intgetitimer(int which, struct itimerval *curr_value);

其中,

getitimer获取which指定的定时器的值并填入curr_value字段中。

setitimer设定指定的定时器,即设置vlaue的值。各参数说明如下:

第一个参数:which为定时器类型,setitimer支持3种类型的定时器:

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

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

decrements only when the process is executing, anddelivers SIGVTALRM upon expiration

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

decrements both when the process executes and when thesystem is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, this timer is usually usedto profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.

第二个参数:new_value,表示

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Linux 中,`setitimer` 和 `getitimer` 是用于设置和获取定时器的系统调用。 `setitimer` 调用用于设置定时器,并在定时器超时时发送信号。它的原型如下: ```c int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value); ``` 其中,`which` 表示要设置的定时器类型,可以是 `ITIMER_REAL`、`ITIMER_VIRTUAL` 或 `ITIMER_PROF`;`new_value` 是一个指向 `itimerval` 结构的指针,表示新的定时器设置;`old_value` 是一个指向 `itimerval` 结构的指针,用于存储之前的定时器设置。 `getitimer` 调用用于获取定时器的当前值。它的原型如下: ```c int getitimer(int which, struct itimerval *curr_value); ``` 其中,`which` 表示要获取的定时器类型,可以是 `ITIMER_REAL`、`ITIMER_VIRTUAL` 或 `ITIMER_PROF`;`curr_value` 是一个指向 `itimerval` 结构的指针,用于存储当前的定时器值。 `itimerval` 结构体用于表示定时器的时间值,包含两个成员变量: ```c struct timeval { time_t tv_sec; /* 秒数 */ suseconds_t tv_usec; /* 微秒数 */ }; struct itimerval { struct timeval it_interval; /* 定时器周期 */ struct timeval it_value; /* 定时器初始值 */ }; ``` 其中,`it_interval` 表示定时器的周期时间,即定时器超时后重新启动定时器的时间间隔;`it_value` 表示定时器的初始值,即定时器首次超时的时间。 总之,`setitimer` 和 `getitimer` 是用于操作定时器的系统调用,在编写周期性任务或定时器相关的程序时非常有用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值