最近在移植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,表示