
【Linux内幕】schedule
1、前言
在许多情况下,设备驱动程序不需要有自己的工作队列。如果我们只是偶尔需要向队列中提交任务,则一种更简单、更有效的办法是使用内核提供的共享的默认工作队列。但是,如果我们使用了这个默认的工作队列,则应该记住我们正在和他人共享该工作队列。这意味着,我们不应该长期独占该队列,即:不能长时间休眠,而且我们的任务可能需要更长的时间才能获得处理器。
2、schedule_work使用步骤:1、定义相关数据 static struct work_struct jiq_work; 2、编写要提交到工作队列中的函数 static_void jiq_print_wq(struct work_struct *work) { … } 3、完成数据的初始化工作 INIT_WORK(&jiq_work, jiq_print_wq); 4、将任务提交到工作队列 schedule_work(&jiq_work);
3、示例 #include <Linux/module.h> #include <linux/init.h> #include <linux/workqueue.h> static struct workqueue_struct *wq = NULL; static struct work_struct work; static void work_handler(struct work_struct *data) { printk(KERN_ALERT"work handler function"); } static int __init test_init(void) { wq = create_singlethread_workqueue("my_workqueue"); if(!wq) goto err; INIT_WORK(&work, work_handler); queue_work(&work); return 0; err: return -1; } static void __exit test_exit(void) { destroy_workqueue(wq); } MODULE_LICENSE("GPL"); module_init(test_init); module_exit(rest_exit); schedule_work(&jiq_work); 4、加入讨论👁️ 阅读量:0
© 版权声明:本文《【Linux内幕】schedule》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686479670a264803.html。