现在的位置: 首页 > 综合 > 正文

在 kernel中如何定时, 变量 HZ 和 jiffies

2013年06月08日 ⁄ 综合 ⁄ 共 765字 ⁄ 字号 评论关闭

 

HZ      定义了时钟中断的频率,即每秒钟时钟中断的次数

jiffies  记录了自启动后,时钟中断发生的次数

 

例如:

unsigned long timeout
= jiffies + (3*HZ)

;
     while (hwgroup->busy) {
     /* ... */
     if (time_after(jiffies, timeout)

) {
     return -EBUSY;
     }
     /* ... */
}
return SUCCESS;

 

timeout这个值,被赋值成了当前的时钟中断数目 加上 3秒钟时钟中断的次数。

当time_after成功返回时,表示已经发生了这么多次的时钟中断,即过了三秒钟。

 

 

if (stream->rescheduled) {
ehci_info(ehci, "ep%ds-iso rescheduled " "%lu times in %lu
            seconds/n", stream->bEndpointAddress, is_in? "in":
            "out", stream->rescheduled,     ((jiffies – stream->start)/HZ)

);
}

 

而这段代码则计算出了 开始到现在 所经历的时间,以秒为单位。 

 

 

 

time_after用的是忙等,这样对cpu资源时钟浪费,用schedule_timeout()可以将cpu让给其他人使用

unsigned long timeout = jiffies + HZ;

set_current_state(TASK_UNINTERRUPTIBLE);
schedule_timeout(timeout); /* Allow other parts of the kernel to run */

 

这样就可以让出一秒钟给别人。  详见schedule_timeout()。

 



抱歉!评论已关闭.