Timers and Deferred Work
Timers and deferred work are essential tools in the Linux kernel for scheduling tasks that should not be performed directly in interrupt context. Kernel timers let you schedule functions to run after a specified delay, while workqueues allow you to defer work to be executed later in process context. Both mechanisms help you keep interrupt handlers fast and responsive by moving time-consuming or blocking operations outside the critical interrupt path.
timer_example.c
1234567891011121314151617181920212223242526272829303132#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/timer.h> static struct timer_list my_timer; void timer_callback(struct timer_list *timer) { pr_info("Timer callback function called.\n"); } static int __init timer_init(void) { pr_info("Initializing timer example module.\n"); timer_setup(&my_timer, timer_callback, 0); mod_timer(&my_timer, jiffies + msecs_to_jiffies(2000)); return 0; } static void __exit timer_exit(void) { del_timer_sync(&my_timer); pr_info("Timer example module exited.\n"); } module_init(timer_init); module_exit(timer_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Example Author"); MODULE_DESCRIPTION("Simple Linux kernel timer example.");
Deferred work with workqueues is another way to handle tasks that should not run in interrupt context. While timers are useful for scheduling one-off or periodic actions after a delay, workqueues are designed for deferring work that may need to sleep or take longer to complete. In the timer example above, you could use a workqueue to perform additional processing after the timer fires, ensuring that any lengthy operation happens outside of interrupt context.
workqueue_example.c
1234567891011121314151617181920212223242526272829303132333435#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/workqueue.h> static struct workqueue_struct *my_wq; static struct work_struct my_work; void work_handler(struct work_struct *work) { pr_info("Workqueue handler running in process context.\n"); } static int __init wq_init(void) { pr_info("Initializing workqueue example module.\n"); my_wq = create_singlethread_workqueue("my_wq"); INIT_WORK(&my_work, work_handler); queue_work(my_wq, &my_work); return 0; } static void __exit wq_exit(void) { flush_workqueue(my_wq); destroy_workqueue(my_wq); pr_info("Workqueue example module exited.\n"); } module_init(wq_init); module_exit(wq_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Example Author"); MODULE_DESCRIPTION("Simple Linux kernel workqueue example.");
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 3.85
Timers and Deferred Work
Deslize para mostrar o menu
Timers and deferred work are essential tools in the Linux kernel for scheduling tasks that should not be performed directly in interrupt context. Kernel timers let you schedule functions to run after a specified delay, while workqueues allow you to defer work to be executed later in process context. Both mechanisms help you keep interrupt handlers fast and responsive by moving time-consuming or blocking operations outside the critical interrupt path.
timer_example.c
1234567891011121314151617181920212223242526272829303132#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/timer.h> static struct timer_list my_timer; void timer_callback(struct timer_list *timer) { pr_info("Timer callback function called.\n"); } static int __init timer_init(void) { pr_info("Initializing timer example module.\n"); timer_setup(&my_timer, timer_callback, 0); mod_timer(&my_timer, jiffies + msecs_to_jiffies(2000)); return 0; } static void __exit timer_exit(void) { del_timer_sync(&my_timer); pr_info("Timer example module exited.\n"); } module_init(timer_init); module_exit(timer_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Example Author"); MODULE_DESCRIPTION("Simple Linux kernel timer example.");
Deferred work with workqueues is another way to handle tasks that should not run in interrupt context. While timers are useful for scheduling one-off or periodic actions after a delay, workqueues are designed for deferring work that may need to sleep or take longer to complete. In the timer example above, you could use a workqueue to perform additional processing after the timer fires, ensuring that any lengthy operation happens outside of interrupt context.
workqueue_example.c
1234567891011121314151617181920212223242526272829303132333435#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/workqueue.h> static struct workqueue_struct *my_wq; static struct work_struct my_work; void work_handler(struct work_struct *work) { pr_info("Workqueue handler running in process context.\n"); } static int __init wq_init(void) { pr_info("Initializing workqueue example module.\n"); my_wq = create_singlethread_workqueue("my_wq"); INIT_WORK(&my_work, work_handler); queue_work(my_wq, &my_work); return 0; } static void __exit wq_exit(void) { flush_workqueue(my_wq); destroy_workqueue(my_wq); pr_info("Workqueue example module exited.\n"); } module_init(wq_init); module_exit(wq_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Example Author"); MODULE_DESCRIPTION("Simple Linux kernel workqueue example.");
Obrigado pelo seu feedback!