Linux定时器使用教程(Linux定时器命令)

在Linux中,我们可以使用内核定时器或高精度定时器(hrtimers)来实现定时任务。

图片[1]-Linux定时器使用教程(Linux定时器命令)-不念博客

以下是使用这些定时器的示例:

内核定时器示例

#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
static struct timer_list my_timer;
static void my_timer_callback(struct timer_list *t) {
printk(KERN_INFO "Timer callback executed\n");
mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 重启定时器
}
static int __init my_init(void) {
printk(KERN_INFO "Initializing timer\n");
timer_setup(&my_timer, my_timer_callback, 0);
mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 设置定时器超时时间
return 0;
}
static void __exit my_exit(void) {
printk(KERN_INFO "Removing timer\n");
del_timer(&my_timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Kernel Timer Example");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>

static struct timer_list my_timer;

static void my_timer_callback(struct timer_list *t) {
    printk(KERN_INFO "Timer callback executed\n");
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 重启定时器
}

static int __init my_init(void) {
    printk(KERN_INFO "Initializing timer\n");

    timer_setup(&my_timer, my_timer_callback, 0);
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 设置定时器超时时间

    return 0;
}

static void __exit my_exit(void) {
    printk(KERN_INFO "Removing timer\n");
    del_timer(&my_timer);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Kernel Timer Example");
#include <linux/init.h> #include <linux/module.h> #include <linux/timer.h> static struct timer_list my_timer; static void my_timer_callback(struct timer_list *t) { printk(KERN_INFO "Timer callback executed\n"); mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 重启定时器 } static int __init my_init(void) { printk(KERN_INFO "Initializing timer\n"); timer_setup(&my_timer, my_timer_callback, 0); mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 设置定时器超时时间 return 0; } static void __exit my_exit(void) { printk(KERN_INFO "Removing timer\n"); del_timer(&my_timer); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Kernel Timer Example");

高精度定时器(hrtimers)示例

#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ktime.h>
#define TIMER_INTERVAL_NS (1000 * 1000 * 1000) // 1 second
static struct hrtimer my_hrtimer;
enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer) {
printk(KERN_INFO "Hrtimer callback executed\n");
hrtimer_forward_now(timer, ns_to_ktime(TIMER_INTERVAL_NS)); // 重启定时器
return HRTIMER_RESTART;
}
static int __init my_init(void) {
ktime_t ktime;
printk(KERN_INFO "Initializing hrtimer\n");
ktime = ktime_set(0, TIMER_INTERVAL_NS);
hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
my_hrtimer.function = my_hrtimer_callback;
hrtimer_start(&my_hrtimer, ktime, HRTIMER_MODE_REL);
return 0;
}
static void __exit my_exit(void) {
printk(KERN_INFO "Removing hrtimer\n");
hrtimer_cancel(&my_hrtimer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Hrtimer Example");
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ktime.h>

#define TIMER_INTERVAL_NS (1000 * 1000 * 1000) // 1 second

static struct hrtimer my_hrtimer;

enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer) {
    printk(KERN_INFO "Hrtimer callback executed\n");
    hrtimer_forward_now(timer, ns_to_ktime(TIMER_INTERVAL_NS)); // 重启定时器
    return HRTIMER_RESTART;
}

static int __init my_init(void) {
    ktime_t ktime;

    printk(KERN_INFO "Initializing hrtimer\n");

    ktime = ktime_set(0, TIMER_INTERVAL_NS);
    hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    my_hrtimer.function = my_hrtimer_callback;
    hrtimer_start(&my_hrtimer, ktime, HRTIMER_MODE_REL);

    return 0;
}

static void __exit my_exit(void) {
    printk(KERN_INFO "Removing hrtimer\n");
    hrtimer_cancel(&my_hrtimer);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Hrtimer Example");
#include <linux/hrtimer.h> #include <linux/init.h> #include <linux/module.h> #include <linux/ktime.h> #define TIMER_INTERVAL_NS (1000 * 1000 * 1000) // 1 second static struct hrtimer my_hrtimer; enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer) { printk(KERN_INFO "Hrtimer callback executed\n"); hrtimer_forward_now(timer, ns_to_ktime(TIMER_INTERVAL_NS)); // 重启定时器 return HRTIMER_RESTART; } static int __init my_init(void) { ktime_t ktime; printk(KERN_INFO "Initializing hrtimer\n"); ktime = ktime_set(0, TIMER_INTERVAL_NS); hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); my_hrtimer.function = my_hrtimer_callback; hrtimer_start(&my_hrtimer, ktime, HRTIMER_MODE_REL); return 0; } static void __exit my_exit(void) { printk(KERN_INFO "Removing hrtimer\n"); hrtimer_cancel(&my_hrtimer); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Hrtimer Example");
© 版权声明
THE END