区别:
使用create_singlethread_workqueue创建工作队列即使对于多CPU系统,内核也只负责在一个cpu上创建一个worker_thread内核线程;而使用create_workqueue创建工作队列对于多CPU系统,内核将会在每个CPU上创建一个worker_thread内核线程,使得线程处理的事务能够并行化.
代码:
worker_thread.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
//工作以队列结构组织成工作队列(workqueue),其数据结构为workqueue_struct,
static struct workqueue_struct *test_wq = NULL;
//把推后执行的任务叫做工作(work),描述它的数据结构为work_struct
static struct work_struct work;
/*
*定义工作队列调用函数
*/
void work_func(struct work_struct *work){
while(1) {
printk(KERN_ERR "-----%s-----\n",__func__); //printk可以在多处理器上同时被调用
}
}
static int __init test_init(void){
/*创建工作队列workqueue_struct,该函数会为cpu创建内核线程*/
test_wq = create_singlethread_workqueue("test_wq");
/*初始化工作work_struct,指定工作函数*/
INIT_WORK(&work,work_func);
/*将工作加入到工作队列中,最终唤醒内核线程*/
queue_work(test_wq, &work);
while(1) {
mdelay(1000);
printk(KERN_ERR "-----%s-----\n",__func__);
}
return 0;
}
static void __exit test_exit(void){
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhao");
Makefile:
MODULE_NAME :=worker_thread
obj-m :=$(MODULE_NAME).o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
项目附带学习实验,详细理论后面有时间补充总结!