如何在 Linux 内核设备驱动程序中使用定时器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10812858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:34:14  来源:igfitidea点击:

How to use timers in Linux kernel device drivers?

clinuxtimerlinux-kerneldriver

提问by user1395806

I want to implement a counter in Linux device drivers which increments after every fixed interval of time. I want to do this with the help of timers. A sample code snippet would be very useful.

我想在 Linux 设备驱动程序中实现一个计数器,它在每个固定的时间间隔后递增。我想在计时器的帮助下做到这一点。示例代码片段将非常有用。

回答by dwalter

Have a look at following article IBM Developerworks: Timers and Lists

查看以下文章IBM Developerworks:计时器和列表

There is a small example of how to use Linux kernel timers (included it here for convenience, comments are from myself, removed printk messages)

有一个关于如何使用 Linux 内核定时器的小例子(为了方便起见,将其包含在此处,评论来自我自己,已删除 printk 消息)

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>

MODULE_LICENSE("GPL");

static struct timer_list my_timer;

void my_timer_callback( unsigned long data )
{
     /* do your timer stuff here */
}

int init_module(void)
{
  /* setup your timer to call my_timer_callback */
  setup_timer(&my_timer, my_timer_callback, 0);
  /* setup timer interval to 200 msecs */
  mod_timer(&my_timer, jiffies + msecs_to_jiffies(200));
  return 0;
}

void cleanup_module(void)
{
  /* remove kernel timer when unloading module */
  del_timer(&my_timer);
  return;
}

回答by betabandido

Depending on what you exactly want to do, you can directly use jiffiesto measure time, as it has been suggested in the comments. You can also use kernel timers, and given the information in your question, they seem to be a better fit.

根据您确切想要做什么,您可以直接使用jiffies来测量时间,正如评论中所建议的那样。您还可以使用内核计时器,根据您问题中的信息,它们似乎更合适。

The kernel timers API is quite intuitive:

内核定时器 API 非常直观:

#include <linux/timer.h>
struct timer_list {
        /* ... */
        unsigned long expires;
        void (*function)(unsigned long);
        unsigned long data;
};

void init_timer(struct timer_list *timer);
struct timer_list TIMER_INITIALIZER(_function, _expires, _data);

void add_timer(struct timer_list * timer);
int del_timer(struct timer_list * timer);

So you would just need to define a timer function and then initialize and start the timer.

所以你只需要定义一个定时器函数,然后初始化并启动定时器。

You have several sources to further learn about this topic:

您有几个来源可以进一步了解此主题:

  • Understanding the Linux Kernel. This book is a sort of bible for the kernel. It is somehow outdated in some areas, but still a really good source of information.
  • Linux Device Drivers. This is a very useful book when developing device drivers. There is an online version too here. The chapter dealing with time, timers, etc. is chapter 7. This book may be also a bit outdated since it is from 2005 too.
  • Linux Kernel Development. I have not checked this book, but the good point is that it is much newer (from 2010), so you may find some updated information compared to the previous two books.
  • 了解 Linux 内核。这本书是内核的一本圣经。它在某些方面已经过时了,但仍然是一个非常好的信息来源。
  • Linux 设备驱动程序。在开发设备驱动程序时,这是一本非常有用的书。有一个在线版本过这里。处理时间、定时器等的章节是第 7 章。这本书也可能有点过时了,因为它也是 2005 年的。
  • Linux 内核开发。我没有检查过这本书,但好处是它更新了很多(从 2010 年开始),因此与前两本书相比,您可能会发现一些更新的信息。