内核workqueue
2017-01-13 18:00
218 查看
/* *任务队列的运用,有两种方式 *1。创建工作队列,初始化一个任务,添加任务,然后调度 *2。初始化一个任务,然后调度该任务 */ #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/workqueue.h> struct test_work { char name[20]; struct work_struct work; }u_data[2] = {{"mode 1 work", 0},{"mode 2 work", 0}}; static struct workqueue_struct *workqueue = NULL; static void work_func(struct work_struct *work) { struct test_work *t_work = container_of(work, struct test_work, work); printk(KERN_EMERG "work name is %s\n", t_work->name); } static int __init work_init(void) { INIT_WORK(&u_data[0].work, work_func); schedule_work(&u_data[0].work); /*添加系统默认的workqueue中,调度运用*/ workqueue = create_workqueue("work_q"); /*创建自己的workqueue, 建了一个内核线程*/ INIT_WORK(&u_data[1].work, work_func); queue_work(workqueue, &u_data[1].work); /*添加到指定的workqueue, 并调用运行*/ return 0; } static void __exit work_exit(void) { destroy_workqueue(workqueue); } MODULE_LICENSE("GPL"); module_init(work_init); module_exit(work_exit);
Makefile:
CUR=$(shell pwd)KERNEL_DIR=/lib/modules/$(shell uname -r)/buildobj-m=work_q.oall:make -C $(KERNEL_DIR) M=$(CUR) modulesclean:make -C $(KERNEL_DIR) M=$(CUR) clean