概述
全局变量jiffies表示自系统启动以来系统产生的嘀嗒数。当启动时,内核初始化该变量为0。每次时钟中断就会增1,所以系统运行时间可以计算为:jiffies/HZ秒。
jiffies变量定义如下:
extern unsigned long volatile jiffies;
将jiffies转换为秒:(jiffies / HZ)。将秒换算为jiffies:(seconds*HZ)。
jiffies比较相关的宏:
#define time_after(unknown, known) ((long)(known) - (long)(unknown) < 0)
#define time_before(unknown, known) ((long)(unknown) - (long)(known) < 0)
#define time_after_eq(unknown, known) ((long)(unknown) - (long)(known) >= 0)
#define time_before_eq(unknown, known) ((long)(known) - (long)(unknown) >= 0)
使用例子:
unsigned long timeout = jiffies + HZ/2; | /* timeout in 0.5s */ |
/* ... */ |
|
if (time_before(jiffies, timeout)) { |
|
/* we did not time out, good ... */ |
|
} else { |
|
/* we timed out, error ... */ |
|
} |
|
代码参考:
typedef struct {
unsigned long jiffies;
}os_time_t;
static inline osal_result os_clock_get_time(os_time_t *time){
time->jiffies = jiffies;
return OSAL_SUCCESS;
}
static inline osal_result os_clock_get_time_diff_msecs( os_time_t *time,
unsigned long *msecs)
{
*msecs = (jiffies - time->jiffies) * 1000 / HZ;
return OSAL_SUCCESS;
}
static inline osal_result os_clock_get_time_diff_secs( os_time_t *time,
unsigned long *secs)
{
*secs = (jiffies - time->jiffies) / HZ;
return OSAL_SUCCESS;
}
最后
以上就是俭朴钢铁侠为你收集整理的内核变量——Jiffies的全部内容,希望文章能够帮你解决内核变量——Jiffies所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复