admin管理员组

文章数量:1794759

【Linux内幕】schedule

【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、加入讨论

本文标签: 内幕Linuxschedule