Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Timers and Deferred Work | Interrupts, Timing, and Concurrency
C Device Drivers Basics

bookTimers 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

timer_example.c

copy
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

workqueue_example.c

copy
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.");
question mark

Which mechanism allows scheduling work to run outside interrupt context?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookTimers 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

timer_example.c

copy
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

workqueue_example.c

copy
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.");
question mark

Which mechanism allows scheduling work to run outside interrupt context?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 5
some-alt