开发者

Example for kernel timer Implementation in Linux (in kernel 2.6.32) [closed]

开发者 https://www.devze.com 2023-01-22 21:56 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Closed 9 years ago.

  • Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
  • Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question 开发者_如何学Python

Could you please give any basic example of a kernel timer (start_ktimer) implementation in Linux?


The best I can tell, Linus never incorporated the ktimer patch into the main line of development. Note that the patch does contain examples of using start_ktimer (see fs/exec.c). If you specifically want to use ktimers, you will need to port the patch circa kernel version 2.6.13 forward to the 2.6.32 kernel.

On the other hand, if all you need is a timer mechanism, the standard kernel timer API may work. For a good discussion of how to use this API as well as examples, see Chapter 7 of the Linux Device Drivers book, specifically, the section titled "The Timer API" (page 198). In this case, the equivalent of start_ktimer() is add_timer().


Here is an example... :) Simple and easy... Source code available:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <asm/param.h>

struct timer_list exp_timer;

static void do_something(unsigned long data)
{
        printk(KERN_INFO "Your timer expired and app has been called\n");       
}

static int __init tst_init(void)
{       
        int delay = 300;

        printk(KERN_INFO "Init called\n");

        init_timer_on_stack(&exp_timer);

        exp_timer.expires = jiffies + delay * HZ;
        exp_timer.data = 0;
        exp_timer.function = do_something;

        add_timer(&exp_timer);

        return 0;
}

static void __exit tst_exit(void)
{       
        del_timer(&exp_timer);  
        printk(KERN_INFO "Exit called\n");
}

module_init(tst_init);
module_exit(tst_exit);

MODULE_LICENSE("GPL");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号