我使用一个内核计时器来安排一个函数周期性地运行(一旦初始化计时器并调用该函数,计时器将在该函数中重新安排)。它是作为设备驱动程序内核模块的一部分发生的,所需的行为是在系统启动(模块加载)后触发并直接运行该函数。问题是,计时器触发的功能在系统引导之后立即启动,但在启动后大约5分钟后启动(虽然我可以确认模块已经加载,并且代码执行足够早地通过AddiSimter)。
这是我对计时器的初始化:

// Allocate memory for the timer
struct timer_list* pTimer = (struct timer_list*)vmalloc(sizeof(struct timer_list));
// step 1: Initialising function for the timer
init_timer(pTimer);
// step 2: set timer fields as needed
pTimer->data = (unsigned long)Data;
pTimer->function = ((void(*)(unsigned long))start_routine);
pTimer->expires = -1; // fire immediately
// step 3: register timer
add_timer(pTimer);

,其中start_例程是要运行的函数。然后,在这个函数中,我重新安排计时器:
/* reschedule the timer */
pTimer->expires = jiffies + 1; // fire every 4msec
add_timer(pTimer);

整个过程运行良好,但在系统启动后的最初大约5分钟内不会。
就像我写的,我可以看到在系统启动后通过计时器初始化部分执行的代码(并调用add_timer),但是由于某种原因,函数start_例程在启动后的最初5分钟内被阻塞。之后,开始按预期调用start_例程。在系统启动后,大约5分钟延迟开始调用计时器功能的原因是什么?

最佳答案

pTimer->expires = -1; // fire immediately

没有一个jiffies值有特殊的含义。
这段代码告诉内核在jiffies计数器达到-1值时运行计时器。
在您的计算机上,计数器恰好从-75000开始,因此在启动后5分钟达到值-1
pTimer->expires = jiffies + 1; // fire every 4msec

此注释是错误的,除非配置频率设置为250。

10-07 19:32
查看更多