如何在 Linux 内核设备驱动程序中使用定时器? [英] How to use timers in Linux kernel device drivers?

查看:27
本文介绍了如何在 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.

推荐答案

看看下面的文章 IBM Developerworks:计时器和列表

有一个关于如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆