在linux device driver中使用内核计时器

本文介绍了Linux内核中的Timer机制,包括其在设备轮询、定期消息发送等场景的应用,以及如何初始化和设置内核计时器,如setup_timer、mod_timer等API的使用。同时强调了在回调函数执行的中断上下文中需要注意的限制。
摘要由CSDN通过智能技术生成

一、Linux 内核中的 Timer

在Linux中,内核通过定时器中断来跟踪时间流。这些定时器中断是使用系统的定时硬件以固定的定时器间隔生成的。每次发生定时器中断时,内部内核计数器的值都会递增。计数器在系统启动时初始化为 0,因此它表示自上次启动以来的时钟滴答次数。

二、内核计时器的使用场景

当您想在一段时间后执行某些操作时,内核计时器是您的选择之一。这些计时器用于根据时钟滴答声在将来的特定时间安排函数的执行,并可用于各种任务。

  • Polling a device by checking its state at regular intervals when the hardware can’t fire interrupts.
  • The user wants to send some messages to another device at regular intervals.
  • Send an error when some action didn’t happen in a particular time period.
  • Etc.

三、内核计时器的API

1、核心数据结构

We need to include the <linux/timer.h> (#include <linux/timer.h>) in order to use kernel timers. Kernel timers are described by the timer_list structure, defined in <linux/timer.h>:

struct timer_list {
    /* ... */
    unsigned long expires;
    void (*function)(unsigned long);
    unsigned long data;
};

The expires field contains the expiration time of the timer (in jiffies).
On expiration, function() will be called with the given data value.

2、Initialize / Setup Kernel Timer

There are multiple ways to Initialize / Setup Kernel Timer. We’ll see one by one.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值