如何在Linux内核设备驱动程序中使用计时器? [英] How to use timers in Linux kernel device drivers?
本文介绍了如何在Linux内核设备驱动程序中使用计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Linux设备驱动程序中实现一个计数器,该计数器在每个固定的时间间隔后递增.我想在计时器的帮助下做到这一点.一个示例代码片段将非常有用.
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内核计时器的小示例(为方便起见,在此处包括了注释,来自我本人,删除了printk消息)
There is a small example of how to use Linux kernel timers (included it here for convenience, comments are from myself, removed printk messages)
#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;
}
这篇关于如何在Linux内核设备驱动程序中使用计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文